I saw an example in twilio: https://www.twilio.com/docs/sms/tutorials/how-to-confirm-delivery-php
<?php
$sid = $_REQUEST['MessageSid'];
$status = $_REQUEST['MessageStatus'];
openlog("myMessageLog", LOG_PID | LOG_PERROR, LOG_USER);
syslog(LOG_INFO, "SID: $sid, Status: $status");
closelog();
I don't know what the code above exactly do, but what I want is to save the data to my local database.
The code in my post method(my statuscallback):
public function smsStatusCallback(Request $request){
$sms = SmsChannel::create([
'number' => $request['MessageSid'],
'body' => $request['MessageStatus'],
]);
}
I've found a solution already. I saw the possible solutions in twilio debugger: "Double check that your TwiML URL does not ...". So I tried making it as a twiml
public function smsStatusCallback(Request $request){
$response = new Twiml();
$sms = SmsChannel::create([
'sid' => $request['MessageSid'],
'status' => $request['MessageStatus'],
]);
return response($response)
->header('Content-Type', 'text/xml');
}
I've added my route to api.php since the URL should be accessible by twilio.
Route::post('sms-status-callback','CommunicationController#smsStatusCallback');
Related
So here's the logic of what I am working on:
Someone calls my Twilio Number I use the dial twiml to forward to a cell phone
I use gather and play a whisper to the operator answering the phone (so his cell phone).
The operator has a choice - press 1 to accept, press 2 (transfers to
a different agent).
step 3 is where I am having trouble I am using the code below:
$call = $twilio->calls($CallSid)
->update([
"method" => "POST",
"url" => "http:www.example.com/directcall.php"
]
);
Here's the problem it is modifying the call but it's redirecting the operators phone number instead of the person who is calling in. So the operator is getting redirected to the other operator and the customer is being hung up on. I tried using the parentcallsid too but that doesn't seem to work either.
Any ideas what I am doing wrong?
so just to be clear I want the flow to work like this:
Customer calls phone number -> redirects to designated operator -> if designated operator presses 2 it redirects the customer to operator 2 and disconnects operator 1 from the call. Is this possible?
Thanks for the help, I greatly appreciate it.
UPDATE PLEASE FIND THE CODE SAMPLES BELOW
Index.php
<?php
include ("config.php");
require_once './vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;
$response = new VoiceResponse();
$twilionumber = ltrim($_POST['To'], '+');
$callernumber=ltrim($_POST['From'], '+');
createCall($phonenumbertouse,$response,$twilionumber);
echo $response;
function createCall($phonenumbertouse,$response,$twilionumber) {
$dial = $response->dial('',['timeout' => '30']);
$dial->number($phonenumbertouse, ['action' => "http://example.com/whisper.php",'method' => 'GET']);
}
WHISPER.PHP
<?php
include ("config.php");
require_once './vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;
$response = new VoiceResponse();
$gather = $response->gather(['action' => "http://example.com/route.php",
'method' => 'GET']);
$gather->say($whisper, ['voice' => 'woman', 'language' => 'en-US']);
echo $response;
?>
route.php
<?php
include ("config.php");
require_once './vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;
use Twilio\Rest\Client;
$response = new VoiceResponse();
$keyedInput=$_REQUEST['Digits'];
$mycallsid=$_REQUEST['ParentCallSid'];
if ($keyedInput == 1){
$response->say('connecting the call');
}
elseif ($keyedInput == 2){
$twilio = new Client($sid, $token);
$call = $twilio->calls($mycallsid)
->update([
"method" => "POST",
"url" => "http://example.com/redirect.php"
]
);
}
elseif ($keyedInput == 3){
$response->say('you selected 3');
}
else {
$response->say('Sorry, I don\'t understand that choice.');
}
echo $response;
?>
**Redirect.php **
<?php
include ("config.php");
require_once './vendor/autoload.php';
use Twilio\TwiML\VoiceResponse;
$response = new VoiceResponse();
$dial = $response->dial('+14151234567',['timeout' => '30']);
echo $response;
?>
Twilio developer evangelist here.
Your issue here is that you are using the wrong CallSid to update.
In this case there are two CallSids at play. When your user dials in to your Twilio number, that call between the user and Twilio has one CallSid. Then, when Twilio creates an outbound call to the operator, that call has a different CallSid.
In your application, when you get the <Gather> response from the operator, the CallSid being sent to your endpoint is the CallSid of the operator's call leg. Instead, you need to find the CallSid of the original call.
You should find that the ParentCallSid parameter is sent to the webhook endpoint as well. That ParentCallSid is the original inbound call's Sid and is what you should use to redirect the caller to another operator.
Edit
OK, so I had a go at building this. I don't normally work in PHP, so I wrote this in Node.js (as Twilio Functions). I got it to work and the answer still seems to me to be "use the ParentCallSid, so hopefully it gives you some idea where you might have gone wrong.
The incoming call
This makes an outbound call to my cell phone number, with a url set to make the whisper to me when I answer the call.
exports.handler = function (context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
const dial = twiml.dial();
dial.number({ url: "/whisper" }, MY_CELL_PHONE_NUMBER);
callback(null, twiml);
};
<Response>
<Dial><Number url="/whisper">MY_CELL_PHONE_NUMBER</Number></Dial>
</Response>
The whisper
This returns a <Gather> spoken to the person answering the cell phone number. It offers a choice, dial "1" to connect or "2" to hang up, triggering the original caller to dial to another number.
exports.handler = function (context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
const gather = twiml.gather({ digits: 1, action: "/after-whisper" });
gather.say("Dial 1 to connect, dial 2 to hang up.");
callback(null, twiml);
};
<Response>
<Gather action="/after-whisper" digits="1">
<Say>Dial 1 to connect, dial 2 to hang up.</Say>
</Gather>
</Response>
The Gather action "/after-whisper"
This checks the Digits parameter. If it is "1" it immediately calls back with an empty response, signalling the end of the whisper and leading the call to connect with the original caller.
If the Digits parameter is not "1" then we initialise a Twilio client and make a request to the API to update the call with the sid ParentCallSid to a new URL. Once that is complete we return TwiML that says to <Hangup> the whisper call (which wasn't strictly necessary, but I like the intention).
exports.handler = async function (context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
if (event.Digits === "1") {
callback(null, twiml);
} else {
const client = context.getTwilioClient();
try {
// Update the parent call with a new URL.
await client.calls(event.ParentCallSid).update({
url: URL_TO_NEXT_TWIML,
});
} catch (error) {
console.log(error);
}
twiml.hangup();
callback(null, twiml);
}
};
The next TwiML
This is the endpoint that the url above relates to, when it gets the callback it just returns a <Dial> to another number, though it could include another whisper and go round this loop again if desired.
exports.handler = async function (context, event, callback) {
const twiml = new Twilio.twiml.VoiceResponse();
twiml.dial(NEXT_NUMBER_TO_DIAL);
callback(null, twiml);
};
<Response>
<Dial>NEXT_NUMBER_TO_DIAL</Dial>
</Response>
I am trying to forward a call to user phone if his twilio phone is called.
$usr_ph = 'xxxxxxxxxx'; // get user personal phone from database
$twiml = new Twiml();
$twiml->dial($usr_ph);
return $twiml;
The above code works fine , I need to get the call minutes. So I added status callback url to dial . But its not sending me the any status event
Below is the code I have
$data = array ('statusCallbackEvent' => array('completed'),
statusCallback' => "https://mywebsite.com/get_Call_outcome",
'statusCallbackMethod' => 'POST'
);
$twiml = new Twiml();
$twiml->dial($usr_ph, json_encode($data));
return $twiml;
I did tried :
$twiml = new Twiml();
$twiml->dial($usr_ph, [
'statusCallbackEvent' => array('completed'),
'statusCallback' => "https://mywebsite.com/get_Call_outcome",
'statusCallbackMethod' => 'POST'
]);
return $twiml;
But then I get the error message as
SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given
Not sure , but I think I need to add statusCallbackEvent on the original call which was made on the twilio phone.
Edit:
Now I tried updating the incoming call by adding statusCallbackEvent, but its still not sending any Event.
$twi = new Client($twilio->sid, $twilio->token);
$call = $twi->calls($request->input('CallSid'))
->update(array ('statusCallbackEvent' => array('completed'),
'statusCallback' =>"www.mywebsite.com/get_Call_outcome",
'statusCallbackMethod' => 'POST'
));
The statusCallback is part of the Number noun, not Dial verb. You can find more details below. See if that addresses the issue.
Twilio Voice:
https://www.twilio.com/docs/voice/twiml/number
Try this Set Status Codes on Number Noune
$dial->number('+14158675310', [
'statusCallbackEvent' => 'initiated ringing answered completed',
'statusCallback' => 'https://myapp.com/calls/events',
'statusCallbackMethod' => 'POST'
]);
I am creating webhook for trello using trello api and mattzuba sdk, webhook is created successfully by using post method but there are two issues
On getting this webhook by using GET function, no results I found.
On changing in model (board id ive used) its not hitting to the callbackURL, i want some example code for callbackurl.
Here is my code of Example.php
$id = '558d029fd94e87c6230df746';
$callback_url = 'http://exampledomain.com/webhook.php';
$description = 'Webhook for board';
$webhook = array(
'idModel' => $id,
'callbackURL' => $callback_url,
'description' => $description,
);
$post = $trello2->post("tokens/$token/webhooks/?key=$key", array_filter($webhook));
print_r($post);
Here is the code of callbackurl page webhook.php
$json = file_get_contents('php://input');
$action = json_decode($json);
$sql = mysql_query("INSERT INTO trellowebhook (data) VALUES ('$action')",$con);
print_r($sql);
I am following the tutorial on https://www.twilio.com/docs/api/rest/change-call-state#post I am coding in php the portion that allows you to forward a current inbound call to a new Twiml URL. I am finding that in order for this to work, I have to specify a To and From parameter in the update array. I need the call forwarded to the URL specified not the number specified in the To parameter. However, the Twilio API throws an error that says the To parameter is required but the docs indicate that it is not. Is there something I am doing wrong here?
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'XXXXXXX';
$token = 'XXXXXXX';
$callSid = $_POST['CallSid'];
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get($callSid);
$call->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));?>
forwardToBob.xml contains a Response with a Say Verb.
This php code (not the twiml) throws the error
Uncaught exception 'Services_Twilio_RestException' with message 'No 'To' number is specified' in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297
If I add
"To" => "Some Ten Digit Phone Number",
"From" => "Some Ten Digit Phone Number",
to the array, the error is not thrown. The call is then directed to the "To" phone number. If the phone number specified in the "To" parameter answers, then the call is connected AND the twiml at forwardToBob.xml executes all at the same time.
EDIT # 3----------------------------------------------------------------------
Here is my entire code....
This is the Twiml that is executed everytime the Twilio verified number is called. I got this code from the Twilio quickstart site.
<?php
header('Content-type: text/xml');
$callerId = "+19012311158";
// put your default Twilio Client name here, for when a phone number isn't given
$number = "Bob";
// get the phone number from the page request parameters, if given
if (isset($_REQUEST['PhoneNumber'])) {
$number = htmlspecialchars($_REQUEST['PhoneNumber']);
}
// wrap the phone number or client name in the appropriate TwiML verb
// by checking if the number given has only digits and format symbols
if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) {
$numberOrClient = "<Number>" . $number . "</Number>";
} else {
$numberOrClient = "<Client>" . $number . "</Client>";
}
?>
<Response>
<Dial callerId="<?php echo $callerId ?>">
<?php echo $numberOrClient ?>
</Dial>
</Response>
This is the client browser which I mostly copied from the Twilio quickstart site.
<?php
include 'TwilioAPI/twilio-php-master/Services/Twilio/Capability.php';
// put your Twilio API credentials here
$accountSid = 'XXXXXXXX';
$authToken = 'XXXXXXXX';
// put your Twilio Application Sid here
$appSid = 'XXXXXXXXXXXXX';
// put your default Twilio Client name here
$clientName = 'Bob';
// get the Twilio Client name from the page request parameters, if given
if (isset($_REQUEST['client'])) {
$clientName = $_REQUEST['client'];
}
$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing($appSid);
$capability->allowClientIncoming($clientName);
$token = $capability->generateToken();
?>
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<script type="text/javascript"
src="//static.twilio.com/libs/twiliojs/1.2/twilio.min.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<link href="http://static0.twilio.com/bundles/quickstart/client.css"
type="text/css" rel="stylesheet" />
<script type="text/javascript">
var callSid = "";
Twilio.Device.setup("<?php echo $token; ?>");
Twilio.Device.ready(function (device) {
$("#log").text("Client '<?php echo $clientName ?>' is ready");
});
Twilio.Device.error(function (error) {
$("#log").text("Error: " + error.message);
});
Twilio.Device.connect(function (conn) {
callSid = conn.parameters.CallSid;
$("#log").text("Successfully established call");
});
Twilio.Device.disconnect(function (conn) {
$("#log").text("Call ended");
});
Twilio.Device.incoming(function (conn) {
$("#log").text("Incoming connection from " + conn.parameters.From);
// accept the incoming connection and start two-way audio
conn.accept();
});
Twilio.Device.presence(function (pres) {
if (pres.available) {
// create an item for the client that became available
$("<li>", {id: pres.from, text: pres.from}).click(function () {
$("#number").val(pres.from);
call();
}).prependTo("#people");
}
else {
$("#" + pres.from).remove();
}
});
function call() {
// get the phone number or client to connect the call to
params = {"PhoneNumber": $("#number").val()};
Twilio.Device.connect(params);
}
function forward() {
var xmlhttp = new XMLHttpRequest();
params = "?CallSid=" + callSid + "&ForwardTo=" + document.getElementById("number").value;
xmlhttp.open("POST","forward.php" + params,false);
xmlhttp.send();
document.getElementById("log").innerHTML=xmlhttp.responseText;
}
function hangup() {
Twilio.Device.disconnectAll();
}
</script>
</head>
<body>
<button class="call" onclick="call();">
Call
</button>
<button class="hangup" onclick="hangup();">
Hangup
</button>
<input type="text" id="number" name="number"
placeholder="Enter a phone number or client to call"/>
<button class="call" onclick="forward();">
Forward
</button>
<div id="log">Loading pigeons...</div>
<ul id="people"/>
</body>
</html>
This is the forwarding code that is called via an HTTP POST request from my forward() function.
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('TwilioAPI/twilio-php-master/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = 'XXXXXX';
$token = 'XXXXXX';
$callSid = $_POST['CallSid'];
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get($callSid);
$call->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));
The first code snippet shows the Twiml that is executed when 901-231-1158 is called. It is then directed to the Client "Bob". Once the connection is successful, I press the forward button which I added in. This forward button calls the forward function which then makes an HTTP POST request to the PHP script which is the last snippet. Upon executing, if I do not have the "To" and "From" parameters specified in the array for the update function, I receive an error. Any suggestions on what I should try to fix this would help very much!
PLEASE NOTE: The button that I created in HTML is called Forward and it calls the function forward() which I created. I am getting the call Sid and saving it in an instance variable. I retrieve this value in the Twilio.Device.Connect function.
The full stack trace.
Stack trace:
#0 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php(180): Base_Services_Twilio->_processResponse(Array)
#1 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio/InstanceResource.php(31): Base_Services_Twilio->createData('/2010-04-01/Acc...', Array)
#2 /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/forward.php(22): Services_Twilio_InstanceResource->update(Array)
#3 {main}
thrown in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php on line 297
[12-Jun-2015 00:19:39 UTC] PHP Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'No 'To' number is specified' in /home/wcmtechnologies/public_html/Phone/TheFinalTouchSecurity/TwilioAPI/twilio-php-master/Services/Twilio.php:297
Twilio developer evangelist here.
Thanks for all the details, I managed to put together most of what you were trying to do in order to find the errors. I never received the error message you had though, so bare that in mind.
I found I had to change the following parts of your code to get this to work.
In your XHR request, stop trying to make the request synchronously (I was using Firefox and it was deprecated), just use:
xmlhttp.open("POST","forward.php" + params);
I was also not getting the call sid from the XHR request in my PHP, so I changed $_POST to $_REQUEST and that started to work.
Finally, the call sid on the client side is not the same as the originating call sid. That is the parent call and you can get hold of it like this using the twilio-php helper library:
$callSid = $_REQUEST['CallSid'];
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$call = $client->account->calls->get($callSid);
$parentCall = $client->account->calls->get($call->parent_call_sid);
$parentCall->update(array(
"Url" => "http://ftstoo.com/Phone/TheFinalTouchSecurity/forwardToBob.xml",
"Method" => "POST"
));
You then need to update the parent call to forward it on to your original URL and hang up on the client side.
I hope this helps!
Too bad my php knowledge.I'm using YouTube-api.Where will write this code: Retrieve Youtube Channel info for "Vanity" channel
If you are talking about this line :
GET https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC6ltI41W4P14NShIBHU8z1Q&key={YOUR_API_KEY}
You are simply making a get request, you can use file_get_contents to get the response for you :
$response = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC6ltI41W4P14NShIBHU8z1Q&key={YOUR_API_KEY}");
Two notes :
You have to replace {YOUR_API_KEY} with the developer key. You can easily request one from youtube: http://code.google.com/apis/youtube/dashboard/
This is just an example in one line of code, I suggest you use a better approach for making this request like the following :
// Encode the parameters of the link
function encode_param($params) {
foreach ($params as $field => $value){
$encoded_params[] = $field . '=' . urlencode($value);
}
return $encoded_params;
}
// Get the response
function get_response($url) {
$response = file_get_contents($url);
// If error, send message back to the client
if ($response === false) {
exit("Couldn't get response from the api");
}
return $response;
}
$params = array(
"part" => "snippet,contentDetails,statistics",
"id" => "UC6ltI41W4P14NShIBHU8",
"key" => "-----------", // Your API key
);
$encoded_params = encode_param($params);
$request_url = "https://www.googleapis.com/youtube/v3/channels?".implode('&', $encoded_params);
$response = get_response($request_url);
//............