我希望从第二次通话中获取通话详细信息,然后将它们插入我的数据库。
以下是该场景:对免费电话号码的入站呼叫被路由到第二部电话。所以有两条腿,1)对免费电话号码的入站呼叫,然后2)与第二个号码的连接。
获取FIRST分支的呼叫详细信息的代码是:
get '/hangup' do
user_key = numbers.where(:number => params["To"]).join(:credentials, :user_id => :user_id).get(:user_key)
user_token = numbers.where(:number => params["To"]).join(:credentials, :user_id => :user_id).get(:user_token)
call_sid = params["CallSid"]
call_parent_sid = ["ParentCallSid"]
@sub_account_client = Twilio::REST::Client.new(user_key, user_token)
@subaccount = @sub_account_client.account
call = @subaccount.calls.get(call_sid)
call_sid = call.sid,
call_parent_sid = call.parent_call_sid,
phone_number_id = call.phone_number_sid,
call_from = call.from,
call_to = call.to,
call_start = call.start_time,
call_end = call.end_time,
call_duration = call.duration,
charged_duration = ((call_duration.to_f)/60).ceil
call_price = call.price
call_charged_price = (charged_duration * 0.07)
call_logs.insert(:call_sid => call_sid, :call_parent_sid => call_parent_sid, :phone_number_id => phone_number_id, :call_from => call_from, :call_to => call_to, :call_start => call_start, :call_end => call_end, :call_duration => call_duration, :charged_duration => charged_duration, :call_price => call_price, :call_charged_price => call_charged_price)
end
这在挂机后工作,而status_callback_url是'/ hangup'。但是,如何才能获得呼叫第二站的相同详细信息。我试过如下:
get '/receive' do
destination_number = numbers.where(:number => params["To"]).join(:users, :id => :user_id).get(:primary_number)
user_id = numbers.where(:number => params["To"]).join(:users, :id => :user_id).get(:id)
greeting_url = voicemail.where(:user_id => user_id).get(:voicemail_play_url)
resp = Twilio::TwiML::Response.new do |r|
r.Dial destination_number, :status_callback => '/hangup_second_leg', :status_callback_method => 'GET'
等。
这有效地尝试创建第二个status_callback_url,不用说,它不起作用。
那么,如何获取通话的第二段(甚至第三段)的详细信息并将其打入我的数据库?
Twilio福音传教士......
非常感谢提前。
答案 0 :(得分:0)
Twilio开发者福音传道为您服务!
我刚刚进行了快速测试,你从挂机回调中获得的参数应该包含一个“CallSid”和一个“DialedCallSid”,它们是你通话的两个分支。您可以通过正常调用REST api来获取数据:
get '/hangup' do
call_sid = params["CallSid"]
dialed_call_sid = params["DialedCallSid"]
@sub_account_client = Twilio::REST::Client.new(user_key, user_token)
@subaccount = @sub_account_client.account
inbound = @subaccount.calls.get(call_sid)
outbound = @subaccount.calls.get(dialed_call_sid)
# Update calls in database
end
或者,入站呼叫是参与呼叫上下文的所有其他呼叫的父呼叫。因此,您可以通过以下api调用获取所有子呼叫的详细信息:
@subaccount.calls.list parent_call_sid: params["CallSid"]
# => [<Twilio::REST::Call>, ...]
此外,如果您在挂断中获得ParentCallSid
参数,那么您也可以使用上面的代码从中查找父呼叫和子呼叫。
希望这会有所帮助,请告诉我是否还有其他任何可以帮助的事情。