Reputation: 303
I have the following simple callback to handle Twilio calls. I ask the user to press any keypad button to continue. If the call recipient doesn't press a key, the question is asked again. How can I determine if the call recipient hangs up. At present, if the call is answered and then hung-up, My redirect route continues the get hit until I give up asking to press any key. There can be a minute delay before the hangup and the point when my app stops receiving hits.
def response
Twilio::TwiML::Response.new do |r|
r.Gather finishOnKey: '#', numDigits: @num_digits,
timeout: @phone_wait_time,
action: NGROK +
voice_path(@notification.id, @final_question_response) do |g|
g.Say @latest_question.text, voice: 'alice', language: 'en-AU'
end
r.Redirect NGROK +
voice_redirect_path(@notification.id, @final_question_response)
end.to_xml
end
Here's a bit more information on the problem I have and what I'm trying to do. I'll use an example.
1) I make an outgoing call as follows
def call_phone
client = Twilio::REST::Client.new(
Rails.application.secrets.twilio_sid,
Rails.application.secrets.twilio_token
)
call = client.account.calls.create(
from: Rails.application.secrets.twilio_voice_number,
to: @phone_number,
url: NGROK + question_path(@notification.id),
method: 'GET',
statusCallback: NGROK + call_status_path(@notification.id),
statusCallbackEvent: [:initiated, :ringing, :answered, :completed]
)
end
2) My statusCallback
is hit with the usual, initiated
, ringing
etc. The call recipient picks up the phone and the statusCallback
is hit with in-progress
. My question_path
is hit as per the url:
defined in 1 above. After some processing my response is called as follows
def response
Twilio::TwiML::Response.new do |r|
r.Gather finishOnKey: '#', numDigits: @num_digits,
timeout: @phone_wait_time,
action: NGROK +
voice_path(@notification.id, @final_question_response) do |g|
g.Say @latest_question.text, voice: 'alice', language: 'en-AU'
end
r.Redirect NGROK +
voice_redirect_path(@notification.id, @final_question_response)
end.to_xml
end
3) @latest_question.text
says Please press any key to continue
. If the call recipient hangs up, I would like to terminate the call. I was hoping my statusCallback
would be hit with a status letting me know the call recipient has hung up. It doesn't do that and I don't know it has happened. After a timeout, my voice_redirect_path
is hit. There is no response so I ask the question again. I still don't know the call has hung up. I continue asking the question and everything continues on as if the call recipient was still on the line. This continues on until I give up and hang up on the call. Then my statusCallback
is hit with a status of completed
.
Thanks in advance.
Upvotes: 2
Views: 1453
Reputation: 73029
Twilio developer evangelist here.
That's weird that you wouldn't immediately get a hangup event from the status callback. If you could email me at [email protected] with a call SID where this happens, I will raise it with the team internally (though I'm on holiday this week, so it might be quicker to go through the support team).
To work around this, you could use a count parameter when you redirect to the voice_redirect_path
and hang up yourself when the count reaches too high. Something like:
def response
Twilio::TwiML::Response.new do |r|
@count = params[:count] || 0
if @count > MAX_REDIRECTS
r.Hangup
else
r.Gather finishOnKey: '#', numDigits: @num_digits,
timeout: @phone_wait_time,
action: NGROK +
voice_path(@notification.id, @final_question_response) do |g|
g.Say @latest_question.text, voice: 'alice', language: 'en-AU'
end
r.Redirect NGROK +
voice_redirect_path(@notification.id, @final_question_response, :count = @count+1)
end
end.to_xml
end
This would allow you to set a max number of redirects while waiting for an answer.
Let me know if this helps at all.
Upvotes: 2