Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/google-translate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -124,3 +125,46 @@ module.exports = (robot) ->
catch err
msg.send "Failed to parse GAPI response"
robot.emit 'error', err


pattern = new RegExp('pronounce ' +
'(.*?)' +
Copy link
Contributor

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.

"(?: (?: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) ->
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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