|
5 | 5 | # None
|
6 | 6 | #
|
7 | 7 | # Configuration:
|
8 |
| -# HUBOT_MSTRANSLATE_APIKEY |
| 8 | +# HUBOT_MSTRANSLATE_CLIENT_ID |
| 9 | +# HUBOT_MSTRANSLATE_CLIENT_SECRET |
9 | 10 | #
|
10 | 11 | # Commands:
|
11 | 12 | # speak me <phrase> - Detects the language 'phrase' is written in, then sends back a spoken version of that phrase
|
|
16 | 17 | module.exports = (robot) ->
|
17 | 18 | robot.hear /(speak)( me)? (.*)/i, (msg) ->
|
18 | 19 | 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 |
20 | 22 | langs = ["en"]
|
21 | 23 |
|
| 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" |
22 | 27 | getLanguagesForSpeak = "http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguagesForSpeak"
|
23 | 28 | detect = "http://api.microsofttranslator.com/V2/Ajax.svc/Detect"
|
24 | 29 | speak = "http://api.microsofttranslator.com/V2/Ajax.svc/Speak"
|
25 | 30 |
|
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" |
29 | 34 | return
|
30 | 35 |
|
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) |
35 | 43 |
|
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 }) |
38 | 55 | .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 |
43 | 57 |
|
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}) |
46 | 60 | .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