Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.59 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # your Twilio authentication credentials
  2. ACCOUNT_SID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  3. ACCOUNT_TOKEN = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYY'
  4.  
  5. # version of the Twilio REST API to use
  6. API_VERSION = '2010-04-01'
  7.  
  8. # base URL of this application
  9. BASE_URL =  "localhost:3000" #production ex: "http://appname.heroku.com/callme"
  10.  
  11. # Outgoing Caller ID you have previously validated with Twilio
  12. CALLER_ID = 'XXX-XXX-XXXX'
  13.  
  14. class CallmeController < ApplicationController
  15.    
  16.     def index
  17.     end
  18.    
  19.     # Use the Twilio REST API to initiate an outgoing call
  20.     def makecall
  21.         if !params['number']
  22.             redirect_to({ :action => '.', 'msg' => 'Invalid phone number' })
  23.             return
  24.         end
  25.        
  26.         # parameters sent to Twilio REST API
  27.         d = {
  28.             'From' => CALLER_ID,
  29.             'To' => params['number'],
  30.             'Url' => BASE_URL + '/hellomoto.xml',
  31.         }
  32.         begin
  33.             account = Twilio::RestAccount.new(ACCOUNT_SID, ACCOUNT_TOKEN)
  34.             resp = account.request(
  35.                 "/#{API_VERSION}/Accounts/#{ACCOUNT_SID}/Calls",
  36.                 'POST', d)
  37.             resp.error! unless resp.kind_of? Net::HTTPSuccess
  38.         rescue StandardError => bang
  39.             redirect_to({ :action => '.', 'msg' => "Error #{ bang }" })
  40.             return
  41.         end
  42.        
  43.         redirect_to({ :action => '', 'msg' => "Calling #{ params['number'] }..." })
  44.        
  45.     end
  46.    
  47.     # TwiML response that says the hellomoto to the caller and presents a
  48.     # short menu: 1. repeat the msg, 2. directions, 3. good bye
  49.     def hellomoto
  50.         @postto = BASE_URL + '/directions.xml'
  51.        
  52.         respond_to do |format|
  53.             format.xml { @postto }
  54.         end
  55.     end
  56.  
  57.     # TwiML response that inspects the caller's menu choice:
  58.     # - says good bye and hangs up if the caller pressed 3
  59.     # - repeats the menu if caller pressed any other digit besides 2 or 3
  60.     # - says the directions if they pressed 2 and redirect back to menu
  61.     def directions
  62.         if params['Digits'] == '3'
  63.             redirect_to BASE_URL + "/goodbye.xml"
  64.             return
  65.         end
  66.        
  67.         if !params['Digits'] or params['Digits'] != '2'
  68.             redirect_to BASE_URL + "/hellomoto.xml"
  69.             return
  70.         end
  71.        
  72.         @redirectto = BASE_URL + '/hellomoto.xml',
  73.         respond_to do |format|
  74.             format.xml { @redirectto }
  75.         end
  76.     end
  77.    
  78.     # TwiML response saying the goodbye message. Twilio will detect no
  79.     # further commands after the Say and hangup
  80.     def goodbye
  81.         respond_to do |format|
  82.             format.xml
  83.         end
  84.     end
  85.    
  86. end