Get Caller From value in Twilio - twilio-php

I've written a script which generates a wordpress post each time a call is received to log a serevice event within our call centre application which is working correctly in as much as the call is handled correctly and a wordpress post is created, but I'm not able to pass the value of the person who is calling to Wordpress. As you can see below I've tried several things.
Here is the code I'm currently using:
$response = new TwiML;
$response = new VoiceResponse();
// $caller = $fromCaller;
$caller = $call['from'];
$caller .= $call['callerid'];
$caller .= $dial['callerid'];
$content = 'Service Request received from ' . $caller;
// Register the Request in to Wordpress
// Create post object
$my_post = array(
'post_title' => 'Service Event',
'post_type' => 'mce_service',
'post_content' => $content,
'post_status' => 'private',
'post_author' => 1
);
// Insert the post into the database
wp_insert_post( $my_post );
Anyone able to point me in the right direction as clearly I'm missing something and the docs aren't helping me.

I found what I was looking for and posting an answer to my own question as it may help others.
$caller = $_REQUEST["From"];

Related

Twilio PHP SDK - Did twilio receive my request?

We are using the Twilio PHP SDK and the Notify Client. When I make a request how do I get the status of that request. Did it return a good 2XX, an error 4XX? And to be clear, I don't mean, what is the status of the messages. I simply mean, did twilio get my API call?
When testing in Postman with the REST API I typically get a 200 or 201 response if everything went well.
$twilio = new Client($acct_sid, $token);
$Addresses = array("+12015551234");
$toBindingAttributes = array();
foreach ($Addresses as $Address) {
array_push($toBindingAttributes, '{"binding_type":"sms","address":"' . $Address . '"}');
}
$notification = $twilio->notify->services($notify_sid)
->notifications->create([
"toBinding" => $toBindingAttributes,
"body" => "Twilio Test."
]);
I've tried to return $notification and I just get [Twilio.Notify.V1.NotificationInstance]
-----Edit-----
ok I realize now that [Twilio.Notify.V1.NotificationInstance] is an object. I was able to print_r($notification) and see that there is a statusCode property.
I tried to echo that property print_r(#notification->statusCode) but I get "Unknown Property".
Is it because it's "protected"?
[statusCode:protected] => 201
Thanks
Since the result is the [Twilio.Notify.V1.NotificationInstance] object.
The problem is all the properties within the object are protected we cannot access them directly.
We were able to get to them with a bunch of strpos, substr, and regex but found a much easier way using a getter.
By doing this way
print($twilio->getHttpClient()->lastResponse->getStatusCode());
More info in the Twilio-PHP library
https://github.com/twilio/twilio-php/blob/650f42647c9b3039e03ae075785164ec97203e94/src/Twilio/Http/Response.php

How to record-from-answer-dual with Twilio's object oriented interface?

I understand how to enable the 'record-from-answer-dual' with the XML style command set, but I'm not finding any way to accomplish the same thing with the more object-oriented style code, such as:
<?php
require_once 'twilio-php-master/Twilio/autoload.php';
$response = new Twilio\Twiml();
$sayMsg = 'Attention! Attention! The network operations
center has opened a ticket concerning an ATMS failure in the Eastern
region. The ticket number is ECHO,1,5,7,4. I repeat, the ticket number is
ECHO,1,5,7,4. Thank you.';
$response->record();
$response->say($sayMsg, array('voice' => 'alice'));
$response->hangup();
echo $response;
I've tried adding it to the new line, and the record line as an array-style entry, similar to enabling the Alice voice. No dice.
I want to record the entire call, from answer, including the message spoken by Twilio.
Thanks for any information anyone can provide!
Twilio developer evangelist here.
<Record> is used to record messages from a call, not to record the TwiML that follows. It's more useful if you are building a messaging or voicemail system for voice.
Given that your message sounds like some kind of announcement, I am guessing that you are generating this call from the REST API. In that case, you can use the Record parameter when you place the call and the entire call will be recorded. In PHP, that would be something like this:
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account Sid and Auth Token from twilio.com/console
$sid = "your_account_sid";
$token = "your_auth_token";
$client = new Client($sid, $token);
$call = $client->calls->create(
$to, $from,
array(
"url" => $url,
"record" => true
)
);
Check out the documentation on the parameters you can use when making a call, including Record here.
Let me know if that helps at all.
Update from Jeffrey's comment
This is the Perl version, using the unofficial Twilio Perl module:
use WWW::Twilio::API;
my ($twilaccountsid, $twilauthtoken, $fromnum, $tonum, $twiml_uri) = #_;
my $twilio = WWW::Twilio::API->new(AccountSid => $twilaccountsid, AuthToken => $twilauthtoken);
my $response = $twilio->POST( 'Calls', From => $fromnum, To => $tonum, Record => 'true', Url => $twiml_uri);
return $response->{content};

Twilio how i find answer by human/ machine and also get the response using PHP

I have send the parameter from my front end code Angular like this,
var params = {"PhoneNumber": Ph};
Twilio.Device.connect(params, {"Record": true, "IfMachine": 'Continue'});
Now, how i get the response of call attends by machine or human status i need to get the response of the call?
Tamil, I've written a post explaining how we recommend you go about this using <Gather> and the example in PHP:
https://www.twilio.com/blog/2016/02/tracking-call-status-how-can-you-tell-if-a-human-answers-the-phone-2.html
<?php
# Include Twilio PHP helper library.
require "/path/to/twilio-php/Services/Twilio.php";
# Create response object.
$response = new Services_Twilio_Twiml();
$response->say("Hello, press any key to hear a top secret message.");
$gather = $response->gather(array(
'action' => "YOUR_TWIML_BIN"
));
$response->say("You did not reveal yourself to be human. Goodbye!");
print $response;
?>

Drupal 8 Guzzle Format Query String

Forgive me for my ignorance, this is my first attempt at Drupal 8 and I'm not a good php developer to begin with. But I've been reading and searching for hours. I'm trying to do a post using the new Guzzle that replaces the drupal_http_request(). I've done this using Curl but can't seem to get this going in the right direction here. I'm just not "getting it".
Here is a sample of the array I have that pulls data from a custom form. I also tried this with a custom variable where I built the string.
$fields = array(
"enroll_id" => $plan,
"notice_date" => $date,
"effective_date" => $date,
);
$client = \Drupal::httpClient();
$response = $client->post('myCustomURL', ['query' => $fields]);
$data = $response->getBody()->getContents();
try {
drupal_set_message($data);
} catch (RequestException $e) {
watchdog_exception('MyCustomForm', $e->getMessage());
}
This indeed returns the result of REJECTED from my API in $data below - but it doesn't append the URL to included the query => array. I've tried numerous combinations of this just putting the fully built URL in the post (that works with my API - tested) and I still receive the same result from my API. In the end what I'm trying to accomplish is
https://myCustomURL?enroll_id=value&notice_date=12/12/12&effective_date=12/12/12
Any direction or tips would be much appreciated.
Thanks for the responses guys. I was able to get it to work correctly by changing a few things in my post. First changing client -> post to a request('POST', XXX) and then changing "query" to "form_params" as "body" has been deprecated.
http://docs.guzzlephp.org/en/latest/quickstart.html#query-string-parameters
$client = \Drupal::httpClient();
$response = $client->request('POST','https://myURL.html', ['form_params' => $fields]);
$data = $response->getBody()->getContents();
Using $client->post will send a POST request. By looking at the URL that you tested directly you want a GET request.
Either use $client->get or $client->request with the GET parameter. More information and examples in the Guzzle documentation.

tmhOauth twitter api stopped working with update_with_media call

So, this morning I got the following error:
{"errors": [{"message": "The Twitter REST API v1 will soon stop functioning.
Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.",
"code": 68}]}
Since I was using the tmhOauth twitter api I went to look if there are updates for it, and as it seems there is an issue listed here.
I'm using the api to update the status with media like this:
$code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
array(
'media[]' => "#{$image}",
'status' => "{$text}"
),
true, // use auth
true // multipart
);
I found notes that I should just change the link to use 1.1 instead of 1 but it's still not working.
My main problem was that I didn't read the docs fully! While the change in the url from 1 to 1.1 was sufficient I missed the point by not looking that the new url for update_with_media,
as explained in the documentation, is https://api.twitter.com/1.1/statuses/update_with_media.json, namely it's api instead of the old upload subdomain.
So, now my api call looks like this and all works again:
$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
array(
'media[]' => "#{$image}",
'status' => "{$text}"
),
true, // use auth
true // multipart
);
Hope this helps someone.
Instead of using tmhOauth api, use abraham's twitteroauth api ( updated to version 1.1 ) :
https://github.com/abraham/twitteroauth/tree/master/twitteroauth
and replace your code as follows :
$connection = new TwitterOAuth($twitter_consumer_key, $twitter_consumer_secret, $twAccessToken, $twAccessTokenSecret);
$parameters = array(
'media[]' => "#{$image}",
'status' => "{$text}"
);
$code = $connection->post('statuses/update_with_media', $parameters);

Resources