commit 950a394517a59d38730cf41de9104605ef2ee30d Author: Corey Donohoe Date: Wed Sep 7 22:31:51 2011 -0700 add speak as the first example diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..3c3629e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..f6a297b1 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,20 @@ +Copyright (c) 2011 GitHub Inc. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..f3b53d4c --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +test: deps + @find test -name '*_test.coffee' | xargs -n 1 -t coffee + +dev: generate-js + @coffee -wc --bare -o lib src/*.coffee + +generate-js: + @find src -name '*.coffee' | xargs coffee -c -o lib + +remove-js: + @rm -fr lib/ + +deps: + +.PHONY: all diff --git a/README.md b/README.md new file mode 100644 index 00000000..5b6c4f5e --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +Hubot Speaks! +============= + +Use Microsoft's Language Translation API to link to wav files of +sentences. + +http://www.microsofttranslator.com/dev/ + +Installing +========== + + Add `hubot-speak` to your hubot installation + + diff --git a/package.json b/package.json new file mode 100644 index 00000000..cc262cdc --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "hubot-scripts", + "version": "0.1.0", + "author": "GitHub Inc.", + "keywords": "hubot plugin scripts campfire bot robot", + "description": "Allows you to open in to a variety of scripts", + "licenses": [{ + "type": "MIT", + "url": "http://github.com/github/github/raw/master/LICENSE" + }], + + "repository" : { + "type" : "git", + "url" : "http://github.com/github/hubot-scripts.git" + }, + + "dependencies": { + "hubot": "0.1.0" + }, + + "directories": { + "lib": "./src" + } +} diff --git a/src/scripts/speak.coffee b/src/scripts/speak.coffee new file mode 100644 index 00000000..f227ed23 --- /dev/null +++ b/src/scripts/speak.coffee @@ -0,0 +1,38 @@ +# Allows Hubot to speak many languages. +# +# speak me - Detects the language 'phrase' is written in, then +# sends back a spoken version of that phrase in its native +# language. + +module.exports = (robot) -> + robot.hear /(speak)( me)? (.*)/i, (msg) -> + term = "\"#{msg.match[3]}\"" + apiKey = process.env.HUBOT_MSTRANSLATE_APIKEY + langs = ["en"] + + getLanguagesForSpeak = "http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguagesForSpeak" + detect = "http://api.microsofttranslator.com/V2/Ajax.svc/Detect" + speak = "http://api.microsofttranslator.com/V2/Ajax.svc/Speak" + + unless apiKey + msg.send "MS Translate API key isn't set, get a key at http://www.bing.com/developers/appids.aspx" + msg.send "Then, set the HUBOT_MSTRANSLATE_APIKEY environment variable" + return + + msg.http(getLanguagesForSpeak) + .query({ appId: apiKey }) + .get() (err, res, body) -> + langs = eval(body) unless err + + msg.http(detect) + .query({appId: apiKey, text: term}) + .get() (err, res, body) -> + if err or (langs.indexOf(eval(body)) == -1) + msg.send "Sorry, I can't speak #{err or eval(body)}" + return + lang = eval(body) + + msg.http(speak) + .query({ appId: apiKey, text: term, language: lang, format: "audio/wav" }) + .get() (err, res, body) -> + msg.send(eval(body)) unless err diff --git a/test/scripts/test.coffee b/test/scripts/test.coffee new file mode 100644 index 00000000..f6e3f8dc --- /dev/null +++ b/test/scripts/test.coffee @@ -0,0 +1,17 @@ +# Tests hubot listeners +module.exports = (robot) -> + assert = require 'assert' + + robot.hear /test/i, (msg) -> + msg.send "OK" + + robot.hear /reply/i, (msg) -> + msg.reply "OK" + + robot.hear /random/i, (msg) -> + msg.send msg.random([1,2]).toString() + + robot.hear /http/i, (msg) -> + msg.http('http://127.0.0.1').port(9001) + .get() (err, res, body) -> + msg.send body diff --git a/test/speak_test.coffee b/test/speak_test.coffee new file mode 100644 index 00000000..27d95e79 --- /dev/null +++ b/test/speak_test.coffee @@ -0,0 +1,25 @@ +Tests = require('./tests') +assert = require 'assert' +helper = Tests.helper() + +require('../src/scripts/speak') helper + +process.env.HUBOT_MSTRANSLATE_APIKEY ||= "0xDEADBEEF" + +# start up a danger room for hubt speak +danger = Tests.danger helper, (req, res, url) -> + res.writeHead 200 + res.end JSON.stringify( + {responseData: {results: [ + {unescapedUrl: url.query } + ]}} + ) + +# callbacks for when hubot sends messages +tests = [ + (msg) -> assert.equal "", msg +] + +# run the async tests +danger.start tests, -> + helper.receive 'hubot speak me Ich bin ein Berliner' diff --git a/test/tests.coffee b/test/tests.coffee new file mode 100644 index 00000000..4d539be6 --- /dev/null +++ b/test/tests.coffee @@ -0,0 +1,53 @@ +Robot = require 'hubot/src/robot' +Url = require 'url' + +# A programmer's best friend. +# http://timenerdworld.files.wordpress.com/2010/12/joint-venture-s1e3_1.jpg +# +# Instantiates a test-only Robot that sends messages to an optional callback +# and a @sent array. +exports.helper = -> + new Helper "#{__dirname}/scripts" + +# Training facility built for the Hubot scripts. Starts up a web server to +# emulate backends (like google images) so we can test that the response +# parsing code functions. +exports.danger = (helper, cb) -> + server = require('http').createServer (req, res) -> + url = Url.parse req.url, true + cb req, res, url + + server.start = (tests, cb) -> + server.listen 9001, -> + helper.cb = (messages...) -> + tests.shift() messages... + server.close() if tests.length == 0 + + cb() + + server + +class Helper extends Robot + constructor: (path) -> + super path + @sent = [] + @Response = Helper.Response + + send: (user, strings...) -> + strings.forEach (str) => + @sent.push str + @cb? strings... + + reply: (user, strings...) -> + strings.forEach (str) => + @send user, "#{user.name}: #{str}" + + # modified to accept a string and pass the Robot.Message to super() + receive: (text) -> + user = new Robot.User 1, 'helper' + super new Robot.Message(user, text) + +class Helper.Response extends Robot.Response + http: (url) -> + super(url).host('127.0.0.1').port(9001) +