Reputation: 1
I have a MVP app written in C#.
At a high level, my app does the following:
Register a user's phone system to their profile and as an approved DID caller in Twilio.
User presses button in website, Twilio calls the user, after the user Presses 1, Twilio adds the destination number of whom they are supposed to call. Twilio records the call (yes, we have consent).
Problem statement:
From a mobile device, when I click the button to initiate a call, Twilio rings the mobile device correctly. The user can receive and then participate in the call to the destination number. At the end of the call, the Twilio user may hang up their cell phone. In any other scenario excluding Twilio, that would end the call. But in this case, as the Twilio user hangs up, Twilio immediately calls the Twilio user back, putting the user in a loop. Like trying to reconnect a disconnected user.
Desired outcome:
When Twilio user hangs up their cell phone, the Twilio call should end completely and not try to reconnect.
How I think to accomplish this:
Add this trailing logic to a CallStatusCallback webhook. To my understanding, this should trigger any time the the call status changes.
When Twilio posts back that the callStatus == completed, then issue a hangup command, as defined here:
https://www.twilio.com/docs/voice/twiml/hangup
Is this correct syntax/logic?
How does the response.Hangup() know to end the call that was triggered by the webhook?
[HttpPost("twilio-webhook-CallStatusCallback")]
public ActionResult CallStatusCallback()
{
var callStatus = Request.Form["CallStatus"];
if (callStatus == "completed")
{
// Handle call completed event
var response = new VoiceResponse();
response.Hangup();
return TwiML(response);
}
// Handle other call statuses if needed
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
I've tried adding a webhook, but need help detecting the correct status and sending a Hangup() command to that specific call.
Upvotes: 0
Views: 89
Reputation: 1116
Twilio does not automatically call someone back when they disconnect. Somewhere in your code you are instructing Twilio to do this. Maybe a status callback event? Please note you can (and usually will) have different callback webhook endpoints or use arbitrary query parameters to keep track of call state which is how "response.Hangup() know[s] to end the call that was triggered by the webhook". In other words, the webhook only returns twiml if it is the initial call or return different twiml to the callee vs caller - the logic for your code reacting to the query parameters is up to you.
To include a query parameter, use this type of format: https://yourwebhookendpoint.com/action/twiml/hangup?callersrole=caller
Twilio will then include "callersrole=caller" when yourwebhookendpoint.com is called.
Upvotes: 0