2013-08-12 41 views
0

我想做到以下幾點:西納特拉,紅寶石和twilio [促進回調機制]

  1. 從我的手機叫我twilio數

  2. 我twilio號碼識別來電號碼,然後立即掛斷

  3. 我twilio號回調的標識號(我的手機號碼)

  4. 當我拿起時,twilio要我輸入我想撥打的號碼

  5. Twilio收集我想呼叫的號碼的輸入並連接到我。

所以我可以從我的手機撥打廉價國際電話(或漫遊電話)。

到目前爲止,從twilio網站API文檔拍攝,我有:

require 'rubygems' 
require 'sinatra' 
require 'twilio-ruby' 
get '/' do 
account_sid = 'xxxxxxx' 
auth_token = 'zzzzzzz' 
to = params['From'] 
#to = '+447928344246' 
#to = '441903773807' 
from = '442033222327' 
Twilio::TwiML::Response.new do |r| 
r.Hangup 
end.text 
# set up a client to talk to the Twilio REST API 
@client = Twilio::REST::Client.new account_sid, auth_token 
@call = @client.account.calls.create(
:from => from, # From your Twilio number 
:to => to, # To any number 
:timeout => "20", 
# Fetch instructions from this URL when the call connects 
:url => 'https://dl.dropboxusercontent.com/u/85088004/twilio/twilio.xml' 
) 
end 

post '/makecall' do 
warn params.inspect 
account_sid = ' ACaf2b951ae6f7424da036ea9dcd5b0d91' 
auth_token = 'my token' 
@client = Twilio::REST::Client.new account_sid, auth_token 
call = @client.account.calls.create(:url => "https://dl.dropboxusercontent.com/u/85088004/twilio/callback.xml", 
:to => params[:Digits], 
:from => "+442033222327") 
puts call.to 
end 

的twilio.xml,在 '/' 部分的URL文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<Response> 
<Gather action="/makecall" method="POST"> 
<Say timeout="10">Enter the number you wish to call</Say> 
</Gather> 
</Response> 

我得到「抱歉發生了應用程序錯誤。「然後它就掛斷了。

warn params.inspect 

當我檢查heroku日誌時不會產生任何東西。所以我認爲(其中一個)問題是我撥打的號碼的參數沒有通過。

是否有任何其他邏輯或腳本問題似乎很明顯?

問題出在'/ makecall'片段中的URL上嗎?它si:

<?xml version="1.0" encoding="UTF-8"?> 
<Response> 
</Hangup> 
</Response> 

很多,非常感謝!

+0

你爲什麼想切換?我發現ruby與twilio合作非常棒,目前我正在使用它發短信應用程序 –

+0

,這是因爲我不知道如何編寫代碼。如果我能堅持使用紅寶石,那麼我很高興!下面提出的解決方案看起來非常有趣,今天晚些時候我會再試一次。 – user1903663

回答

0

Twilio Developer Evangelist here。

當你發現,這是你自己的號碼,你想返回一些TwiML(XML)來Twilio,這應該足夠了:<Response> <Hangup/> </Response>

你會那麼需要做一個REST API調用來Twilio作新的外呼對自己說:

@client.account.calls.create(to: my_number, from: "+sometiwilionumber", url: "http://example.com/make-other-call-twiml") 

然後使用一些TwiML的URL <Gather>要撥打的號碼,並簡單地<Dial>到數...

你想避免的事情在第一次通話結束之前,Twilio會回覆您。由於您需要首先進行API調用,因此我們需要使用線程來解決它。我不壞和Ruby /西納特拉,但我有穿在這裏沒有專家,但這應該工作:

twilio_call_thread = Thread.new{ 
    sleep 3 #3 Seconds should be plenty, but you may want to experiment. 
    @client.account.calls.create(to: mynumber, from: some_twilio_number, url: "http://example.com/gather-and-dial") 
} 
#Then return the TwiML to hangup... 
"<Response><Hangup/></Response>" 

你撥打你的電話號碼,它創建一個線程,然後進入睡眠狀態。它迴應<Hangup>斷開呼叫。幾秒鐘後,線程醒來,你會得到回電。您需要在該呼叫中使用一些TwiML才能撥打<Gather>以獲得要撥打的電話號碼,然後撥打<Dial>進行實際呼叫。

我是不知道的線程,所以對檢查之前,我擁有了你可以使用其他兩個想法:

使用短信。向您發送Twilio號碼以撥打您想撥打的另一個號碼,然後讓Twilio直接撥打您的號碼和另一個號碼。

其次,只需使用URL,假設您的手機上有數據,只需打開一個URL即可,而不需要初始呼叫或SMS。

噢 - 第三個選項,如果你不想使用線程,或者不適合你:在你的Twilio號碼上設置Status Callback URL(點擊'Optional Voice Settings')。通話完成後,這將被觸發,並且您可以從那裏向您自己撥打新電話。

希望你把它整理好,它會很棒。

+0

,看起來真的很酷的人,謝謝你真的很快反應。今天晚些時候我會再試一次。我會讓你知道結果!特別是如果我仍然有問題。 – user1903663

+0

對,我已經去了。上面有問題的更新代碼。這些問題是作爲內聯評論。我剛剛試圖實現回調,然後我會在聚會和下一個階段工作。非常感謝您對此的建議。 – user1903663

+0

好的。因此,您需要做的下一步是看看Twilio文檔中的。您指定的網址需要轉到您的Sinatra應用程序中的另一條路線,該路線將TwiML返回到的電話號碼。那麼你需要第三條路線。這是收集的行動 - 「」部分,並將生成一個 TwiML到您指定的數字。您應該理想地使用國際格式編號,因爲我個人認爲這是一個很好的做法。所以總是添加+。 – xmjw

0

解決!感謝您從T傳道者的幫助。這裏是解決方案:

require 'rubygems' 
require 'sinatra' 
require 'twilio-ruby' 
account_sid = 'my sid' 
auth_token = 'my token' 

get '/' do 
from = 'my T number' 
to = params[:From] 
Twilio::TwiML::Response.new do |r| 
r.Hangup 
end.text 
sleep 10 
# set up a client to talk to the Twilio REST API 
@client = Twilio::REST::Client.new account_sid, auth_token 
@call = @client.account.calls.create(
:from => from, # From your Twilio number 
:to => to, # To any number 
# Fetch instructions from this URL when the call connects 
:url => 'https://dl.dropboxusercontent.com/u/85088004/twilio/twilio.xml' 
) 
end 

# the xml file. It is VERY IMPORTANT that you have the absolute url in the action="" 

<?xml version="1.0" encoding="UTF-8"?> 
<Response> 
<Gather timeout="20" action="http://dry-journey-9071.herokuapp.com/makecall" method="GET"> 
<Say voice="alice">Enter the destination number, please.</Say> 
</Gather> 
<Say voice="alice">Input not received. Thank you</Say> 
</Response> 

#Back to the app: 

get '/makecall' do 
number = params[:Digits] 
Twilio::TwiML::Response.new do |r| 
r.Dial number ### Connect the caller to Koko, or your cell 
r.Say 'The call failed or the remote party hung up. Goodbye.' 
end.text 
end 

Yay!

相關問題