Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twilio blacklist rule fatal error

I am using twilio to send bulk sms messages. Let's say some customer decided that they don't want to receive messages anymore so they reply with "stop" and that will add them to the black list. I am hard coding the phone numbers because I am still testing on my own cell phones. I noticed that when I do not remove the numbers on the black list from my code, I am getting an error message and my script stops at that point.

In the future, I will probably be using numbers stored in a database or a file. In that case, how do I overcome this problem if it happened. Basically what I want to do is: If a number is in the black list, move on to the next number and avoid that error using an exception or something. The error message and code is below.

Thanks,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Send SMS</title>
<?php
    /* Send an SMS using Twilio. You can run this file 3 different ways:
     *
     * 1. Save it as sendnotifications.php and at the command line, run 
     *         php sendnotifications.php
     *
     * 2. Upload it to a web host and load mywebhost.com/sendnotifications.php 
     *    in a web browser.
     *
     * 3. Download a local server like WAMP, MAMP or XAMPP. Point the web root 
     *    directory to the folder containing this file, and load 
     *    localhost:8888/sendnotifications.php in a web browser.
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // following the instructions to install it with Composer.
    //require_once "vendor/autoload.php";
    require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
    use Twilio\Rest\Client;

    // Step 2: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "something";
    $AuthToken = "something";

    // Step 3: instantiate a new Twilio Rest Client
    $client = new Client($AccountSid, $AuthToken);

    // Step 4: make an array of people we know, to send them a message. 
    // Feel free to change/add your own phone number and name here.
    $people = array(
        "+17570123456" => "Chris",
        "+17571234568" => "Hussam"
    );

    // Step 5: Loop over all our friends. $number is a phone number above, and 
    // $name is the name next to it
    foreach ($people as $number => $name) {

        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                // that you've purchased
                'from' => "+184444444444", 

                // the sms body
                'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name.\n";
    }
?>

( ! ) Fatal error: Uncaught exception 'Twilio\Exceptions\RestException' with message '[HTTP 400] Unable to create record: The message From/To pair violates a blacklist rule.' in C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php on line 86 ( ! ) Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: The message From/To pair violates a blacklist rule. in C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php on line 86 Call Stack

Time Memory Function Location 1 0.0000 239280 {main}( ) ...\send.php:0 2 0.0156 799016 Twilio\Rest\Api\V2010\Account\MessageList->create( ) ...\send.php:56 3 0.0156 814688 Twilio\Version->create( ) ...\MessageList.php:63

like image 342
Hussam Hallak Avatar asked Dec 06 '16 14:12

Hussam Hallak


2 Answers

Twilio developer evangelist here.

You need to catch the exception that is thrown from the request to send a message to the blacklisted number. You can do so with try and catch like this:

foreach ($people as $number => $name) {
    try {
        $sms = $client->account->messages->create(
            $number,
            array(
                'from' => "+18443949780", 
                'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
            )
        );
        echo "Sent message to $name.\n";
    } catch (\Twilio\Exceptions\RestException $e) {
        echo "Couldn't send message to $number\n";
    }
}

When you hook this up to a database, you'll want to use the catch to update a field to mark the number as blocked so that you don't try to send to it again.

Let me know if that helps at all.

like image 54
philnash Avatar answered Oct 28 '22 02:10

philnash


This worked for me with Laravel 5. Notice the use of \Twilio\Exceptions\RestException.

   try {
        $sms = $client->account->messages->create(
            $number,
            array(
                'from' => "+16136543180", 
                'body' => "Hey $name, Are you still mad at us about your cat!"
            )
        );
        echo "Sent message to $name.\n";

     } catch (\Twilio\Exceptions\RestException $e) {
          if ($e->getCode() == 20404) {
              //this will be false condition
              dd('False Result 404');
          } else {
              //some other exception code
              dd($e->getMessage());    
          }
     }
like image 2
Peter Drinnan Avatar answered Oct 28 '22 03:10

Peter Drinnan