how to use StatusCallback url in twilio - twilio

<?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

Related

Trying to connect two calls with Twilio using the first call sid to connect to the next call

I am new to Laravel 9 and usage of Twilio API.
I am trying to create a scenario where a customer_care will call the first participant and after talking with the participant it will call the second participant with whom the first participant will communicate. While calling the second participant, the first participant's call will be kept on hold and after the second participant receives the call both their call will be merged so that they are connected to the same call.
I have written two separate function one for the first call and the other for the second call . The first function is:
public function twiliocall1(Request $request) {
$returnArray = array();
$status = "Error";
$msg = "Call not started";
$validator = Validator::make($request->all(), [
'participants' => 'required|array',
'twilloNo' => 'required',
'contact_id' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}
$twilloNo = $request->input('twilloNo');
$participants = $request->input('participants');
$sid = "xxxxx";
$token = "yyyy";
$client = new Client($sid, $token);
if (!empty($participants)) {
foreach($participants as $participant) {
$call1 = $client->account->calls->create(
$participant,
$twilloNo,
array("url" => "https://lbwr.operative.dev/multitenant/public/files/conference.php")
);
}
$status = "Success";
$msg = "Call started";
echo($call1->sid);
}
$returnArray['status'] = $status;
$returnArray['msg'] = $msg;
return response()->json($returnArray, 200);
}
The second function is:
public function twiliocall2(Request $request) {
$returnArray = array();
$status = "Error";
$msg = "Call not started";
$validator = Validator::make($request->all(), [
'participants' => 'required|array',
'twilloNo' => 'required',
'contact_id' => 'required',
'sid' => 'required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}
$twilloNo = $request->input('twilloNo');
$participants = $request->input('participants');
$parent_sid = $request->input('sid');
$sid = "xxxx";
$token = "yyyy";
$client = new Client($sid, $token);
foreach($participants as $participant) {
$call = $client->account->calls->get($parent_sid);
$call->update(
$participant,
$twilloNo,
array(
"Url" => "http://demo.twilio.com/docs/voice.xml",
"Method" => "POST"));
}
$status = "Success";
$msg = "Call started";
$returnArray['status'] = $status;
$returnArray['msg'] = $msg;
return response()->json($returnArray, 200);
}
$sid and $token value is same for both the function. I am trying to call the first function using the following json array:
{
"participants":["12345"],
"twilloNo":"68564",
"contact_id":"1234"
}
And on hitting I am getting the sid of this call which I am passing as a json array element to the function twiliocall2() as:
{
"participants":["+7777"],
"twilloNo":"111122",
"contact_id":"1023",
"sid":"fdthgfjkhbklnj"
}
All the values are changed.
The problem arises when I am calling the second function with this Json array as it throws an error:
Twilio\Exceptions\TwilioException: Unknown subresource get in file /opt/lampp/htdocs/multitenant/vendor/twilio/sdk/src/Twilio/Rest/Api/V2010/Account/CallList.php
Kindly suggest if possible where am I going wrong. I am not getting any idea how to implement this scenario any further.

Get status of SMS send though Twilio notify

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"
]);

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
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);
};

In Twilio how can I access a menu after outgoing call is answered?

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.

Correct Way to use returned ApiGility Implicit Token

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.

Resources