Skip to content

Instantly share code, notes, and snippets.

@eduardo
Forked from marks/tropo-forvo.rb
Created December 5, 2010 19:26
Show Gist options
  • Save eduardo/729376 to your computer and use it in GitHub Desktop.
Save eduardo/729376 to your computer and use it in GitHub Desktop.
## FORVO PRONOUNCIATION APP FOR TROPO.COM
## by @Skram / [email protected] / blog.marksilver.net
# == TRY IT ==
# SMS: (202) 618-0887
# Jabber: [email protected]
#
# == USAGE ==
# message tropo bot: [a word/phrase/whatever you want to hear in different accents from around the world.]
# example input: gruyere
# output: <text messages and phone call to you>
#
# == INSTALL (100% FREE) ==
# 1. Get an API key from Forvo (or use mine below, within reason please) at http://developer.forvo.com/
# 2. Sign up for an account at http://tropo.com/
# 3. At Tropo.com, make a new scripting app with the following code. Be sure to name it something.rb
# 4. Add a phone number to your Tropo account as well as a Jabber/AIM/Yahoo/Twitter/MSN/etc. IM account for interacting with your app
# 5. SMS or IM a word to one of the channels you created in Step 4. It's that easy.
%w(rubygems open-uri json).each {|lib| require lib}
# Method to convert long MP3 URLs to short URLs we will text/IM to users if requested
def shorten_url(long_url)
open("http://tinyurl.com/api-create.php?url=#{long_url}").read
end
# Method to call and read the pronounciation items to the user
def call_and_say(to,items)
call(to,
:onAnswer => lambda {|new_channel| # new_channel.value is how we will interact with the outbound phone call we just created.
new_channel.value.say "I'm about to read you the results for your query of the word #{PARAMS[:word]}."
new_channel.value.say "Press a key during, or right after, playback to have the result messaged to you."
items.each do |item| # if the user hits a digit on the keypad during playback, we will IM them the details of what they heard like the username, MP3 link, and language
item_ask = new_channel.value.ask("Your word in #{item["langname"]} from someone in #{item["country"]} sounds like #{item["pathmp3"]},",{
:choices => "[1 DIGITS]",
:repeat => 0,
:timeout => 2,
:onChoice => lambda { say "Item Details: #{PARAMS[:word]} pronounced in #{item["langname"]} by #{item["username"]} (#{item["sex"]} from #{item["country"]}). MP3: #{shorten_url(item["pathmp3"])}" } # Here, `say` sends a message to the original channel (either IM or SMS)
})
end
new_channel.value.say "End of results. Application built by mark silver dot net, on the tropo dot com platform, with data from fore vo dot com. Goodbye."
}
)
end
# Forvo API/variable set up.
key = "a7b12f311ca3f2c330065e638dffee47"
url = "http://apifree.forvo.com/action/word-pronunciations/format/json/key/#{key}"
PARAMS = {:word => $currentCall.initialText} # $currentCall.initialText is the initialText from the SMS or IM someone sends to your app.
PARAMS.each{|k,v| url += "/#{k}/#{v}"} # add params (only one right now, but we could add language, etc.) to url
answer
ask(nil,{:choices => "[ANY]"}) # if we dont do this, Tropo will take the $currentCall.initialText as the input for the first ask() verb.
begin # Fire off and receive response from Forvo API
response = JSON.parse(open(URI.escape(url)).read)
rescue
say "Sorry, but your search could not be performed because an error occured. We may have reached our Forvo API limit. Please try again later."
hangup; exit;
end
# Do stuff depending on what the Forvo API sends back to us.
case response["attributes"]["total"]
when 0
say ":( No results found for your search. Try a more general query or record your own pronounciation at http://forvo.com "
when 1..999999 # In the future, maybe we want to show different output depending on how many responses we get.
match_string = "I found #{response["attributes"]["total"]} matches for your search. "
if $currentCall.callerID =~ /1\d{10}/ # if callerID is a US phone number (AKA not an IM screen name), call that #.
say match_string + "Im calling you on #{$currentCall.callerID} to play those pronounciations."
call_and_say("tel:+#{$currentCall.callerID}",response["items"])
else # if callerID isnt a US number, ask who to call
ask(match_string + "What 10-digit US phone number should I call you on? Please use the format ##########",
{ :choices => "[10 DIGITS]",
:repeat => 2,
:timeout => 60,
:onBadChoice => lambda { say "Invalid entry, please try again using XXXXXXXXXX (10 digit) format" },
:onTimeout => lambda { say "I didn't receive a response. What was that 10 digit number you wanted me to call you on?" },
:onChoice => lambda { |phone_num| # do stuff with choice.value
say "OK. Im calling you on #{phone_num.value} to play those pronounciations for you."
call_and_say("tel:+1#{phone_num.value}",response["items"])
}
})
end
else
say "Sorry but something went south with your search. Please check out the blog post for example queries and try again."
end
say "Thank you for using pronounciations by phone by @Skram (marksilver.net) - built on Tropo.com with data from Forvo.com"
hangup
# This is a super easy way to message you, the app admin, of when someone uses your app.
# Also see @akalsey's blog post about using Google Analytics & Tropo at http://blog.tropo.com/2010/06/28/tracking-calls-with-google-analytics/
message("Someone just looked up the word: '#{PARAMS[:word]}'",{:to=> "15128267004", :network => "sms" })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment