-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provide pronunciation in languages #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
# Commands: | ||
# hubot translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out. | ||
# hubot translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional | ||
# hubot pronounce <phrase> in <language> - Provides pronounciation of <phrase> (<language> is optional) | ||
|
||
languages = | ||
"af": "Afrikaans", | ||
|
@@ -124,3 +125,46 @@ module.exports = (robot) -> | |
catch err | ||
msg.send "Failed to parse GAPI response" | ||
robot.emit 'error', err | ||
|
||
|
||
pattern = new RegExp('pronounce ' + | ||
'(.*?)' + | ||
"(?: (?:in )?(#{language_choices}))?$", 'i') | ||
robot.respond pattern, (msg) -> | ||
term = "\"#{msg.match[1]}\"" | ||
origin = if msg.match[2] isnt undefined then getCode(msg.match[2], languages) else 'auto' | ||
|
||
if origin != 'auto' | ||
msg.send "http://translate.google.com/translate_tts?ie=UTF-8&q=#{encodeURIComponent(term)}&tl=#{origin}" | ||
return | ||
|
||
msg.http("https://translate.google.com/translate_a/t") | ||
.query({ | ||
client: 't' | ||
hl: 'en' | ||
multires: 1 | ||
sc: 1 | ||
sl: origin | ||
ssel: 0 | ||
tl: 'en' | ||
tsel: 0 | ||
uptl: "en" | ||
text: term | ||
}) | ||
.header('User-Agent', 'Mozilla/5.0') | ||
.get() (err, res, body) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll want to check for errors here: if err?
robot.emit 'error', err, msg Otherwise, you'd be trying to acccess the body and it wouldn't be defined because of the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool, added below (the try/catch too) |
||
if err | ||
msg.send "Failed to connect to GAPI" | ||
robot.emit 'error', err, res | ||
return | ||
|
||
try | ||
data = body | ||
if data.length > 4 and data[0] == '[' | ||
parsed = eval(data) | ||
language =languages[parsed[2]] | ||
parsed = parsed[0] and parsed[0][0] and parsed[0][0][0] | ||
msg.send "http://translate.google.com/translate_tts?ie=UTF-8&q=#{encodeURIComponent(term)}&tl=#{getCode(language, languages)}" | ||
catch err | ||
msg.send "Failed to parse GAPI response" | ||
robot.emit 'error', err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking this was being too greedy, but
.*?
should be non-greedy.