Skip to content

Commit

Permalink
add speak as the first example
Browse files Browse the repository at this point in the history
  • Loading branch information
atmos committed Sep 8, 2011
0 parents commit 950a394
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions 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.
15 changes: 15 additions & 0 deletions 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
14 changes: 14 additions & 0 deletions 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


24 changes: 24 additions & 0 deletions 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"
}
}
38 changes: 38 additions & 0 deletions src/scripts/speak.coffee
@@ -0,0 +1,38 @@
# Allows Hubot to speak many languages.
#
# speak me <phrase> - 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
17 changes: 17 additions & 0 deletions 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
25 changes: 25 additions & 0 deletions 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'
53 changes: 53 additions & 0 deletions 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)

0 comments on commit 950a394

Please sign in to comment.