Twilio 黑名单规则致命错误

Hus*_*lak 7 twilio twilio-php twilio-api

我正在使用 twilio 发送批量短信。假设一些客户决定他们不想再接收消息,所以他们回复“停止”,这会将他们添加到黑名单。我对电话号码进行了硬编码,因为我仍在自己的手机上进行测试。我注意到当我没有从我的代码中删除黑名单上的数字时,我收到一条错误消息并且我的脚本在那时停止。

将来,我可能会使用存储在数据库或文件中的数字。在这种情况下,如果发生了这个问题,我该如何克服。基本上我想要做的是:如果一个号码在黑名单中,请转到下一个号码并使用异常或其他方式避免该错误。错误消息和代码如下。

谢谢,

<!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";
    }
?>
Run Code Online (Sandbox Code Playgroud)

( ! ) 致命错误:未捕获异常“Twilio\Exceptions\RestException”,消息为“[HTTP 400] 无法创建记录:消息发件人/收件人对违反黑名单规则。” 在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 上的第 86 行(!)Twilio\Exceptions\RestException:[HTTP 400] 无法创建记录:来自/至对的消息违反了黑名单规则。在 C:\wamp64\www\Twilio\twilio-php-master\Twilio\Version.php 中的第 86 行调用堆栈

时间记忆函数位置 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

phi*_*ash 5

Twilio 开发人员布道者在这里。

您需要捕获从向黑名单号码发送消息的请求中抛出的异常。你可以这样做trycatch像这样:

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

当您将其连接到数据库时,您将需要使用catch来更新字段以将该号码标记为已阻止,以便您不会再次尝试向其发送。

让我知道这是否有帮助。

  • Hi Jin,当有人被列入黑名单时,库不会崩溃,但是当 SDK 收到来自 API 的非 200 响应时,它确实会抛出错误。无论如何,您应该在对外部 API 的此类调用周围放置一个 try/catch 块,就好像存在网络问题或其他错误请求一样,SDK 也会抛出异常,因此这与此没有太大区别。 (2认同)