首页 > 解决方案 > PHP 队列停止使用 Twilio 无效的电话号码

问题描述

我正在使用 PHP/Laravel 通过 Twilio 发送批量电子邮件。我不能使用 Twilio 通知,因为每个电话号码都会使用不同的文本。如果所有电话号码都有效,则一切正常,但如果出现任何无效电话号码,我的队列因错误而停止。尝试捕获块或失败的功能根本不起作用

我的代码:

 public function handle()
    {
        $message = $this->client->messages->create(
            $this->number,
            [
                'from' => $this->from,
                'body' => $this->msg
            ]
        );
    }

(我也尝试将上面的函数代码包装在 try catch 块中,但没有运气)然后我尝试添加失败的函数

public function failed(\Exception $exception)
    {
        // Send user notification of failure, etc...
    }

仍然没有运气。

我收到以下错误:

array:4 [
  "code" => 21211
  "message" => "The 'To' number  is not a valid phone number."
  "more_info" => "https://www.twilio.com/docs/errors/21211"
  "status" => 400
]

我正在使用以下队列命令

php artisan queue:work  --sleep=3 --tries=3 &

收到 Twilio 错误后,如果我停止命令并再运行 2 次,则只有失败的短信移动到 failed_jobs 表并继续执行下一个作业。使用 laravel 7.x

任何帮助高度赞赏。

=================================

这是完整的工作

class BulkSms implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $number;
    private $msg;
    private $client;
    private $from;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($number, $from, $msg, $client)
    {
        $this->number = $number;
        $this->from = $from;
        $this->msg = $msg;
        $this->client = $client;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $message = $this->client->messages->create(
            $this->number,
            [
                'from' => $this->from,
                'body' => $this->msg
            ]
        );
    }

    public function failed(\Exception $exception)
    {
        // Send user notification of failure, etc...
    }
}

上面是从控制器调用的,例如

foreach ($candidateSMS as $number) { 
    BulkSms::dispatch($mobile, $this->from, $msgBody, $client)->delay(now()->addSecond(.2));
}

标签: phplaraveltwiliosms

解决方案


推荐阅读