kk3294120
kk3294120

Reputation: 68

how to get callback status in twilio

How to callback status in twilio for example in php.

require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;

// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "---------------";
$token = "-----------------;
$client = new Client($sid, $token);

$call = $client->calls->create(
    $to, $from,
    array(
        "url" => "http://demo.twilio.com/docs/voice.xml",//this line complete
        "method" => "GET",
        "statusCallbackMethod" => "POST",
        "statusCallback" => "https://www.myapp.com/events", //in this line no idea
        "statusCallbackEvent" => array(
            "initiated", "ringing", "answered", "completed"
        )
    )
);

echo $call->sid;

In this example how to get event after call

Upvotes: 0

Views: 934

Answers (1)

Yuri Danilchenko
Yuri Danilchenko

Reputation: 121

Your code is correct for registering call status notifications from Twilio. For every event you specified in your statusCallbackEvent array, Twilio will make an asynchronous HTTP request to the URL specified in the statusCallback parameter. See this article for more details.

So now you will need a service at this URL: https://www.myapp.com/events, listening for notification sent by Twilio. In your case these will be POST requests, given the method you specified in statusCallbackMethod parameter. The parameter you need to tell which event is being notified is CallStatus. See this documentation for more info.

We're not using PHP, but if you want to do a quick test, just create a Twilio Function and paste the following code in there to see your event notifications:

exports.handler = function(context, event, callback) {
  let response = { get_started: true };

  console.log(`Call status notified was '${event.CallStatus}'.`);

  callback(null, response);
};

Upvotes: 1

Related Questions