### @export "requires" begin require "net/http" require "uri" rescue Exception => e say e.message log e.message end ### @export "say-each" def say_each(s, pause_after = 0) length = s.length 0.upto(length-1) do |i| say s[i, 1] if (pause_after > 0) && ((i+1) % pause_after == 0) wait 1000 end end end ### @export "voicemail" def voicemail timestamp_filename = Time.now.strftime("%Y-%m-%d--%H-%M-%S") event = record('Please leave a message.', { :repeat => 0, :bargein => true, :beep => true, :silenceTimeout => 2, :maxTime => 60, :timeout => 5, :recordUser => RECORD_USER, :recordPassword => RECORD_PASSWORD, :recordURI => "ftp://ananelson.com/voicemails/#{timestamp_filename}.wav", :transcriptionOutURI => 'mailto:ana@ananelson.com', :transcriptionID => timestamp_filename}) say "Thank you, we'll make sure Ana gets the message." end ### @export "currency-conversion" def currency_conversion currencies = {'dollars' => 'USD', 'pounds' => 'GBP'} choice = ask("Say the currency you want to convert from now. Available currencies are #{currencies.keys.sort.join(",")}", :choices => "dollars(1, dollar, dollars, bucks, greenbacks), pounds(2, pound, pounds, quid)") if choice.name === "choice" currency = choice.value say "you chose #{currency}" else raise "not a valid choice" end choice = ask "Now key in the amount in #{currency}", :choices => '[DIGITS]' amount = choice.value say "you entered #{amount}" ccy = currencies[currency] uri = URI.parse("http://download.finance.yahoo.com/d/quotes.csv?s=#{ccy}EUR=X&f=sl1d1t1c1ohgv&e=.csv") currency_data = Net::HTTP.get(uri) euro_conversion_rate = currency_data.split(",")[1].to_f euro_amount = euro_conversion_rate * amount.to_i info = "The value of #{amount} #{currency} is #{euro_amount} euro, using a rate of #{euro_conversion_rate}." repeat = true while repeat do say info choice = ask "To repeat this, press 1 now. To return to main menu, press 2 now.", :choices => "1,2" if choice.name === "choice" repeat = (choice.value.to_i == 1) else repeat = false end end end ### @export "listen-to-radio" def listen_to_radio index_page_url = "http://www.archive.org/details/voyageofthescarletqueenotr" series_name = "The Voyage of the Scarlet Queen" episode_url_base = "http://www.archive.org/download/" uri = URI.parse(index_page_url) page_text = Net::HTTP.get(uri) page_text.match(/IAD\.names = \[(.*)\];/) episode_names = $1.split(",").collect {|x| x.gsub('"', '')} page_text.match(/IAD\.mp3s = \[(.*)\];/) episode_urls = $1.split(",").collect {|x| x.gsub('"', '')} prompt = series_name choices = [] episode_names.each_with_index do |s, i| next if i > 9 prompt << " Press #{i+1} for episode #{s}." choices << (i+1).to_s end episode = ask(prompt, :choices => choices.join(",")).value.to_i episode_url = episode_url_base + episode_urls[episode] say "Press 9 to return to main menu." ask(episode_url, :choices => "[DIGITS]") end ### @export "make-valid-choice" def make_valid_choice(choices_hash) prompt = "" choices_hash.sort.each do |k, v| prompt << "To #{v}, press #{k} now. " end choices = choices_hash.keys.collect {|k| k.to_s}.sort.join(",") valid = false choice = nil until valid do event = ask(prompt, {:choices => choices, :mode => 'dtmf'}) if event.name === 'choice' choice = event.value valid = true elsif event.name === 'badChoice' say "Sorry, that is not an available option." else say "unexpected event #{event.name}" end break if $currentCall.state === "DISCONNECTED" end choice end ### @export "conference-call" def join_conference_call(identifier) conference(identifier) end ### @export "main-menu" begin answer wait 1000 finished = false until finished do choice = make_valid_choice( 1 => 'leave a message', 2 => 'do currency conversion', 3 => 'listen to radio', 4 => 'join conference call', 9 => 'disconnect' ) case choice when '1' voicemail() when '2' currency_conversion() when '3' listen_to_radio() when '4' join_conference_call("demo1") when nil, '9' hangup else raise "should not be allowed to have choice " + choice end say "going back to main menu, if you are finished, just hang up" finished = ($currentCall.state === "DISCONNECTED") end log "call finished" hangup rescue Exception => e log e.message say e.message hangup end