sinatra,ruby和twilio [促进CALLBACK机制]

时间:2013-08-12 10:17:31

标签: ruby sinatra twilio

我想做以下事情:

  1. 从我的手机拨打我的twilio号码

  2. 我的twilio号码标识收到的号码,然后立即挂断

  3. 我的twilio号码回拨已识别的号码(我的手机号码)

  4. 当我拿起时,twilio要求我输入我想拨打的号码

  5. Twilio收集我要拨打的号码的输入并连接我。

  6. 所以我可以通过手机拨打便宜的国际电话(或漫游电话)。

    到目前为止,取自twilio网站api docs,我有:

    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,在' /' section 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>
    

    我得到了#34;抱歉发生了应用程序错误。&#34;然后它就挂断了。

    warn params.inspect 
    
    当我检查heroku日志时,

    不会产生任何结果。所以我认为(其中一个)问题是我拨号的号码没有通过。

    逻辑或脚本的其他问题是否显而易见?

    问题出在&#39; / makecall&#39;中的网址上。片段?它是:

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

    很多,非常感谢!

2 个答案:

答案 0 :(得分:1)

解决!!感谢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

耶!

答案 1 :(得分:0)

Twilio开发者传播者在这里。

当您检测到它是您自己的号码时,您想要将一些TwiML(XML)返回给Twilio,这应该足够了:<Response> <Hangup/> </Response>

然后,您需要对Twilio进行REST API调用,以便对自己进行新的外拨呼叫:

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

然后使用某些TwiML的网址<Gather>要拨打的号码,只需<Dial>到该号码...

你要避免的事情是让Twilio在第一次通话结束前给你回电话。由于您需要首先进行API调用,我们需要使用线程来绕过它。我对Ruby / Sinatra不错,但我不是这里的线程专家,但这应该有用:

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直接在您的号码和其他号码之间拨打电话。

其次,只需使用一个网址,假设您手机上有数据,您只需打开一个网址,即可在没有初始通话或短信的情况下执行相同操作。

哦 - 还有第三个选项,如果您不想使用线程,或者不适合您:在Twilio号码上设置状态回拨URL(单击“可选语音设置”)。在通话结束后,这会被解雇,然后你就可以对自己进行新的通话。

希望你把它分类,它会很棒。