Twilio - get error code on sms send failure - php

Our code sends twilio sms messages by doing the following:
// send the text message to the member's mobile phone
try {
// attempt to send the message through Twilio
$tw_msg = $twilio_client->messages->create(
"+1".$recipient['address'],
array (
'From' => "+1".$org['twilio_number'],
'Body' => $txtmsg,
'StatusCallback' => CALLBACK_LINK.'/text_message_handler.php'
)
);
// else trap the error since the message could not be sent and the callback routine is not called
} catch (Exception $e) {
// process the text message error
process_text_msg_error($db, $e, $org, $msg, $recipient);
}
In the v4 library we would get the error code by doing the following:
// get the twilio error components
$twilio_error_status = $e->getStatus();
$twilio_error_code = $e->getCode();
$twilio_error_msg = $e->getMessage();
This is not giving us what we expected using the V5 library. How do we get the error status and code using the V5 lib?

Twilio developer evangelist here.
It looks to me like you need to update one of the methods you call on the exception to get the status code. The exception is now a RestException and has the method getStatusCode(). You should update to:
// get the twilio error components
$twilio_error_status = $e->getStatusCode();
$twilio_error_code = $e->getCode();
$twilio_error_msg = $e->getMessage();
Let me know if that helps at all.

Related

Unable to decode credential from JWT

I am trying to implement the new "Sign in with Google" button as described in https://developers.google.com/identity/gsi/web/guides/display-button.
Everything is fine, and I am able to get a response from the button with "credential" and "g_csrf_token" elements, which I can send to my server. However, using the Google API Client to decode the credential doesn't work. I'm trying to follow the instructions.
Here's my code:
$id_token = filter_input(INPUT_POST, 'credential');
$csrfToken = filter_input(INPUT_POST, 'g_csrf_token'); //??? Do we need this?
$client = new Google_Client(['client_id' => $clientid]);
$client->addScope("email"); // Recommended in another StackOverflow answer but makes no difference
try {
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload
The result is the error message id_token must be passed in or set as part of setAccessToken.
I've updated my Google API Client to v2.11.
I assume that I've missed a step somewhere - can someone help?
Have found a solution, by trial and error! Turns out that $id_token needs to be passed to the client twice, once in setAccessToken() and then again in verifyIdToken(). Omitting setAccessToken fails (like the error message says), but if you pass it in setAccessToken but NOT in verifyIdToken, that doesn't work either.
$id_token = filter_input(INPUT_POST, 'credential');
$client = new Google_Client(['client_id' => $clientid]);
try {
$client->setAccessToken($id_token);
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload
It would nice, if you're at Google and picking this up, if you updated the documentation.

Microsoft Graph v1 webhook notifications are not being sent

I have a weird issue with microsoft graph api v1.
I am trying to setup a subscription, so that I get webhook notification after a new outlook email arrives.
$body = [
"changeType" => "created,updated",
"notificationUrl" => route('api.outlook.push'),
"resource" => "me/messages",
"expirationDateTime" => now()->addMinutes(self::SUBSCRIPTION_EXPIRATION_MINUTES),
];
$response = null;
try {
$response = $this->service->createRequest('POST', '/subscriptions')
->attachBody(json_encode($body))
->setReturnType(Subscription::class)
->execute();
} catch (Exception $exception) {
logger()->info('outlook subscription exception', ['message' => $exception->getMessage()]);
}
This seems to be working correctly as microsoft is sending a notification to specified url with verification code, that I'm returning and the message says that subscription was created. The issue is that even though it was set up properly - I am not getting a single one webhook. Did any of you guys had this issue and manage to fix it?

Error: Voice call using twilo in php code

I using twilio for call system, but my error:
Error: Account not authorized to call +84983xxxxxx. Perhaps you need
to enable some international permissions:
https://www.twilio.com/user/account/settings/international
I checked in Vietnam (+84) via https://www.twilio.com/user/account/settings/international but still encounter the same error
My php code:
<?php
require 'twilio-php/Services/Twilio.php';
function call_notify($number) {
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'AC34962eb3f445dfa40893e64bb8xxxxxx';
$token = '9b0667c68299cf484228344d67xxxxxx';
// A phone number you have previously validated with Twilio
$phonenumber = '+155920xxxxx';
// Instantiate a new Twilio Rest Client
$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
$number, // The number of the phone receiving call
'http://103.255.238.29/notify.xml' // The URL Twilio will request when the call is answered
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
call_notify("+84983xxxxxx");
Please help me, thanks
Twilio developer evangelist here.
I clicked on the URL to the international settings but was redirected to the Messaging international settings.
Can you go to the Voice international settings here https://www.twilio.com/console/voice/settings/geo-permissions and ensure you have Vietnam checked on that page too.
I'll feedback that the error is pointing to the wrong page.
Let me know if that helps!

Error while making outbound call on twilio

I am new to twilio.I am using twilio trial account and I am using php.I have created my account and the verification code was sent to my number .But when i try to make a call i get the error
Error: The source phone number provided, +1937xxxxxx6, is not yet verified for your account. You may only make calls from phone numbers that you've verified or purchased from Twilio.
Here is my code
<?php
// Include the Twilio PHP library
require 'twilio-php-master/Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'Axxxxxxxxxxxxxxxxxxffa58';
$token = 'f878xxxxxxxxxxxxxxxeb05';
// A phone number you have previously validated with Twilio
$phonenumber = '+19xxxxxxxx6';
// Instantiate a new Twilio Rest Client
$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
'+91xxxxxxxx7', // The number of the phone receiving call
'http://demo.twilio.com/welcome/voice' // The URL Twilio will request when the call is answered
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
It happened to me when using the test account/token.
I double checked that the my phone number is verified but it was giving me this error.
Then I replaced the test account with my real account sid/token and the call succeeds.
I consider this as a bug in their system.
tested with python 3.6, twilio 6.16.2

Handle 500 internal server error while sending SMS using twilio in PHP

I am using Twilio sdk to send SMS in my PHP based application,I have an array of phone numbers and sending an SMS to each number in this array in a loop.....the problem is while the loop is running if an invalid number comes twilio API returns 500 internal server error and it stops the loop without trying sending sms for rest of the numbers in the array.
This is the code I am using for sending sms :
public function sendSmsAction($userphones)
{
foreach($userphones as $user_phone)
{
$message = 'Thanks for coming';
$this->twiliosms($user_phone,$message);
}
}
private function twiliosms($phone_num,$message)
{
require Mage::getBaseDir()."/twilio-php-master/Services/Twilio.php";
$AccountSid = "XXXXXXX";
$AuthToken = "XXXXXXX";
$client = new Services_Twilio($AccountSid, $AuthToken);
try {
$message = $client->account->messages->create(array(
"From" => "+1XXXXXXXXXX",
"To" => $phone_num,
"Body" => $message,
));
} catch (Services_Twilio_RestException $e) {
echo $e->getMessage();
}
}
And this is the error I am getting :
Status: 500 Internal Server Error
The 'To' number +1XXXXXXXXXX is not a valid phone number or shortcode.
please help me out in handling this error.
Thanks in advance.
Finally, I got my solution....by just adding another catch() made it working fine...
catch(Exception $e)
{
}

Categories