Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 7aaecab

Browse files
author
Chris DaMour
committed
tweaked the speak script so that it works with the newer OAuth token format, couldn't figure out how to run tests opened issue #801
1 parent 47890ea commit 7aaecab

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/scripts/speak.coffee

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
# None
66
#
77
# Configuration:
8-
# HUBOT_MSTRANSLATE_APIKEY
8+
# HUBOT_MSTRANSLATE_CLIENT_ID
9+
# HUBOT_MSTRANSLATE_CLIENT_SECRET
910
#
1011
# Commands:
1112
# speak me <phrase> - Detects the language 'phrase' is written in, then sends back a spoken version of that phrase
@@ -16,32 +17,53 @@
1617
module.exports = (robot) ->
1718
robot.hear /(speak)( me)? (.*)/i, (msg) ->
1819
term = "\"#{msg.match[3]}\""
19-
apiKey = process.env.HUBOT_MSTRANSLATE_APIKEY
20+
clientId = process.env.HUBOT_MSTRANSLATE_CLIENT_ID
21+
clientSecret = process.env.HUBOT_MSTRANSLATE_CLIENT_SECRET
2022
langs = ["en"]
2123

24+
#MS changed their token service, have to use this oauth thing see http://msdn.microsoft.com/en-us/library/hh454950.aspx
25+
tokenService = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
26+
tokenScope = "http://api.microsofttranslator.com"
2227
getLanguagesForSpeak = "http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguagesForSpeak"
2328
detect = "http://api.microsofttranslator.com/V2/Ajax.svc/Detect"
2429
speak = "http://api.microsofttranslator.com/V2/Ajax.svc/Speak"
2530

26-
unless apiKey
27-
msg.send "MS Translate API key isn't set, get a key at http://www.bing.com/developers/appids.aspx"
28-
msg.send "Then, set the HUBOT_MSTRANSLATE_APIKEY environment variable"
31+
unless clientId
32+
msg.send "MS Translate Client ID isn't set, follow steps at http://msdn.microsoft.com/en-us/library/hh454950.aspx"
33+
msg.send "Then, set the HUBOT_MSTRANSLATE_CLIENT_ID environment variable"
2934
return
3035

31-
msg.http(getLanguagesForSpeak)
32-
.query({ appId: apiKey })
33-
.get() (err, res, body) ->
34-
langs = eval(body) unless err
36+
unless clientSecret
37+
msg.send "MS Translate Client Secret isn't set, follow steps at http://msdn.microsoft.com/en-us/library/hh454950.aspx"
38+
msg.send "Then, set the HUBOT_MSTRANSLATE_CLIENT_SECRET environment variable"
39+
return
40+
41+
clientId = encodeURIComponent(clientId)
42+
clientSecret = encodeURIComponent(clientSecret)
3543

36-
msg.http(detect)
37-
.query({appId: apiKey, text: term})
44+
msg.http(tokenService)
45+
.header('Content-Type', 'application/x-www-form-urlencoded')
46+
#can you do this by passing in an object literal? i tried but it didn't work...
47+
.post( "client_id=#{clientId}&client_secret=#{clientSecret}&scope=#{tokenScope}&grant_type=client_credentials") (err, res, body) ->
48+
parsedBody = JSON.parse(body)
49+
if err or parsedBody.error
50+
msg.send "Unable to get token, #{err or parsedBody.error_description}"
51+
return
52+
accessToken = "Bearer " + parsedBody.access_token
53+
msg.http(getLanguagesForSpeak)
54+
.query({ appId: accessToken })
3855
.get() (err, res, body) ->
39-
if err or (langs.indexOf(eval(body)) == -1)
40-
msg.send "Sorry, I can't speak #{err or eval(body)}"
41-
return
42-
lang = eval(body)
56+
langs = eval(body) unless err
4357

44-
msg.http(speak)
45-
.query({ appId: apiKey, text: term, language: lang, format: "audio/wav" })
58+
msg.http(detect)
59+
.query({appId: accessToken, text: term})
4660
.get() (err, res, body) ->
47-
msg.send(eval(body)) unless err
61+
if err or (langs.indexOf(eval(body)) == -1)
62+
msg.send "Sorry, I can't speak #{err or eval(body)}"
63+
return
64+
lang = eval(body)
65+
66+
msg.http(speak)
67+
.query({ appId: accessToken, text: term, language: lang, format: "audio/wav" })
68+
.get() (err, res, body) ->
69+
msg.send(eval(body)) unless err

0 commit comments

Comments
 (0)