Adam McGurk
Adam McGurk

Reputation: 476

Prevent call from disconnected when client is not connected

I'm trying to set up some phone numbers in Twilio. I am able to call them fine and get connected fine when that specific client is connected (I'm using unique URLs to define which client should be dialed when a specific number is called...like this: https://example.io/my/path/to/the/twiml/?attribute=CLIENT_IDENTIFIER)

The problem arises when the specific client isn't connected to my app. When that happens, the automated message is said but then the call disconnects. I want it to be like a normal phone where it rings until it hits a voicemail. That's the problem I'm having. Here is my PHP:

<?php
declare(strict_types=1);
use Twilio\TwiML\VoiceResponse;

require_once '../../../vendor/autoload.php';

$response = new VoiceResponse();
$response->say('This call may be monitored for quality assurance', 
    ['voice' => 'woman', 'language' => 'us-EN']);
$dial = $response->dial('', [
    'record' => 'record-from-ringing-dual', 
    'recordingStatusCallback' => 'https://example.com/my/callback/'
]);
$dial->client($_GET['CLIENT_IDENTIFIER'], [
    'statusCallbackEvent' => 'completed',
    'statusCallback' => 'https://example.com/my/callback/'
]);

echo $response;

My status callback isn't getting called, so I know the call isn't getting completed. So my question is really this:

How can I make Twilio not immediately disconnect when It can't connect to the client I pass it?

Upvotes: 2

Views: 602

Answers (2)

philnash
philnash

Reputation: 73027

Twilio developer evangelist here.

If a <Dial> can't connect then it will make a webhook to its action attribute passing a DialCallStatus as a parameter. You can respond to the webhook with more TwiML to continue the call.

So, your initial PHP should look like this, with an action attribute added to <Dial>:

<?php
declare(strict_types=1);
use Twilio\TwiML\VoiceResponse;

require_once '../../../vendor/autoload.php';

$response = new VoiceResponse();
$response->say('This call may be monitored for quality assurance', 
    ['voice' => 'woman', 'language' => 'us-EN']);
$dial = $response->dial('', [
    'record' => 'record-from-ringing-dual', 
    'recordingStatusCallback' => 'https://example.com/my/callback/',
    'action' => 'https://example.com/my/action/'
]);
$dial->client($_GET['CLIENT_IDENTIFIER'], [
    'statusCallbackEvent' => 'completed',
    'statusCallback' => 'https://example.com/my/callback/'
]);

echo $response;

And then /my/action/ could do something like this:

<?php
declare(strict_types=1);
use Twilio\TwiML\VoiceResponse;

require_once '../../../vendor/autoload.php';

$response = new VoiceResponse();
if ($_POST['DialCallStatus'] == 'completed') {
    // the call succeeded, just end it
    $response->hangup();
} else {
  $response->say('The person you are calling cannot be reached right now. Please leave a message', 
      ['voice' => 'woman', 'language' => 'us-EN']);
  $response->record([
      'action' => 'https://example.com/my/callback/'
  ]);
}
echo $response;

Let me know if that helps at all.

Upvotes: 2

sandwish
sandwish

Reputation: 1

The listen action in twilio's autopilot seems to address the waiting time you are looking for.

Upvotes: 0

Related Questions