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
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);
};
Related
I have send notification messages using Twilio api. Messages are sending properly.
$notification = $client
->notify->services($serviceSid)
->notifications->create([
"toBinding" => [
'{"binding_type":"sms", "address":"+971444444444"}',
'{"binding_type":"sms", "address":"+971444444445"}'
],
'body' => 'Test message 8'
]);
Response of the request is 201 and returning a sid starting with 'NT'. How to track status of this messages?
Should be like this:
PHP:
$notification = $client
->notify->services($serviceSid)
->notifications->create([
"toBinding" => [
'{"binding_type":"sms", "address":"+971444444444"}',
'{"binding_type":"sms", "address":"+971444444445"}'
],
'body' => 'Test message 8'
'sms' => ['status_callback' => 'https://youcallbackurl.com']
]);
Or Javascript
const service = twilio.notify.services(notifyId);
const bindings = numbers.map(number => {
return JSON.stringify({
binding_type: 'sms',
address: number,
});
});
service.notifications.create({
toBinding: bindings,
body: message,
sms: {
status_callback: 'https://youcallbackurl.com'
}
})
twilio has a callback status webhook , you need to configure it inorder to track the delivery status of an sms message
$notification = $client
->notify->services($serviceSid)
->notifications->create([
"toBinding" => [
'{"binding_type":"sms", "address":"+971444444444"}',
'{"binding_type":"sms", "address":"+971444444445"}'
],
'body' => 'Test message 8'
'statusCallback' => "your public end point to track sms delivery status"
]);
see more here
Step to send Multiple SMS via Twilio using PHP
Create Account on Twilio
Get a Twilio Number
Add Phone numbers here: https://www.twilio.com/console/phone-numbers/verified
Create a notification Service here: https://www.twilio.com/console/notify/services and copy "SERVICE SID" (This will be used in code)
Create a Message Service here: https://www.twilio.com/console/sms/services
Select MESSAGING SERVICE SID under Notify - Configuration's page, what you have created on 5th step:
$sid = 'ACb2f967a907520b85b4eba3e8151d0040'; //twilio service SID
$token = '03e066de1020f3a87cec37bb89f56dea'; //twilio Account SID
$serviceSid = 'IS4220abf29ae4169992b8db5fc2668b10'; //Notify service SID
$client = new Client($sid, $token);
$rs = $client->notify->services($serviceSid)->notifications->create([
"toBinding" => [
'{"binding_type":"sms", "address":"+919999999999"}',
'{"binding_type":"sms", "address":"+919999999999"}'
],
'body' => 'Test message 8',
'statusCallback' => "your public end point to track sms delivery status"
]);
I'm trying to enable access to a menu after a call as been dialed and answered. For example, an agent dials a number, which uses the verb to place a call. During that call the person called asks to be transferred to a different agent, extension or queue. I have read about putting a call into a conference and using the hangupOnStar attribute to put the person called on hold and bring up a menu for the agent to further manipulate the call but have been unsuccessful. It seems that pressing the '*' button ends the call and therefore the DialCallSid belongs to a completed call which can't be updated.
I was originally going about this the wrong way. This is for an Outbound call, so I was able to successfully create a "on hold" conference, dial up a caller using the REST API, then add the caller to the conference. I was also able to harness the hangupOnStar attribute to enable leaving the conference and going to a menu.
Here is the code in my first function:
public function makeOutboundConference(Request $request) {
$sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$response = new Twiml();
$dial = $response->dial(
[
'hangupOnStar' => true,
'action' => 'url-to-incall-menu',
'method' => 'POST'
]
);
$dial->conference("On Hold",
[
'beep' => false,
]
);
$client = new Client($sid, $token);
try {
$call = $client->calls->create(
$to,
$callerId,
array('url' => 'fq-url-to-connect-caller')
);
} catch (Exception $e) {
error_log("Error: " . $e->getMessage());
}
return $response;
}
and my second function to add the person called to the conference:
public function showConnectCaller(Request $request) {
$response = new Twiml();
$dial = $response->dial();
$dial->conference(
"On Hold",
[
'record' => 'record-from-start',
'beep' => false,
'endConferenceOnExit' => true,
]
);
return $response;
}
Twilio: Can I make OUTBOUND Conference Calls? was very helpful.
I am using Laravel 4 version and using Twilio for sending messages. I have installed Twilio/SDK through composer. I am using the below code in helper.php
$client = new \Services_Twilio($AccountSid, $AuthToken);
try {
$message = $client->account->messages->create(array(
"From" => $twillo_number,
"To" => $phone,
"Body" => $message,
));
} catch (Exception $e) {
//Log::error($e->getMessage());
}
I always getting an error on my screen which is
Class 'Services_Twilio' not found
Can you help on this? Why is this issue coming?
Twilio developer evangelist here.
My guess here is that you have installed the Twilio PHP SDK version 5, but you are following a tutorial that was written for version 4.
To send a message with the latest version of the Twilio SDK, you can use this code:
use Twilio\Rest\Client;
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Client($sid, $token);
try {
$client->messages->create(
$phone,
array(
'from' => $twilio_number,
'body' => $message
)
);
} catch (Exception $e) {
//Log::error($e->getMessage());
}
Also, you can check the documentation for the most up to date way to send a message.
Let me know if that helps at all.
My app receives an access token by virtue of an implicit grant. Now I am attempting to use this token to access the content servers RPC service. I am not 100% sure if this is the correct way to do it as I am unable to get it to work.
$code = (string) $this->params()->fromQuery('code', null);
$client = new HttpClient(
'http://www.example.com/api/books',
array(
'maxredirects' => 0,
'timeout' => 30
)
);
$client->setMethod('GET');
$client->setHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$code
]
);
try {
$response = $client->send();
} catch (\Exception $e) {
throw new \Exception($e);
}
Here is the example in postman which is failing:
In this question the authorization_code was used and not the access_token and that's why it failed.
<?php
$options = array(
"StatusCallback" => 'http://173.203.104.63/call/out/log-callback.php? id='.$id
);
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
$cphonenumber, // The number of the phone receiving call
'60',
'http://173.203.104.63/call/out/three.php?id='.$id, // The URL Twilio will request when the call is answered
$options
);
...
here i have set up url for statuscallback but i dont know weather it is redirecting or not and also how to get my callstatus values in that url
The 3rd argument should be the URL, not '60'. Once the call ends, all the data will be passed to your callback URL as normal POST or GET parameters (depending on what you've setup in your account).
you have forgotten to specify the method for statuscallback url
i have included it here in the code
<?php
//"StatusCallbackMethod" can be "POST" or "GET", depends on the way you recieve it on your StatusCallback,
$options = array(
"StatusCallbackMethod"=>"GET",
"StatusCallback" => 'http://173.203.104.63/call/out/log-callback.php? id='.$id
);
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
$cphonenumber, // The number of the phone receiving call
'60',
'http://173.203.104.63/call/out/three.php?id='.$id, // The URL Twilio will request when the call is answered
$options
);
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); //
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "AC5ef8732a3c49700934481addd5ce1659";
$token = "{{ auth_token }}";
$client = new Services_Twilio($sid, $token);
$call = $client->account->calls->create("+18668675309", "+14155551212", "http://demo.twilio.com/docs/voice.xml", array(
"Method" => "GET",
"StatusCallback" => "https://www.myapp.com/events",
"StatusCallbackMethod" => "POST",
"StatusCallbackEvent" => array("initiated", "ringing", "answered", "completed"),
));
echo $call->sid;
Please see https://www.twilio.com/docs/api/rest/making-calls