当用户选择退出消息服务 [PHP/Laravel] 时,Twilio 不会在 Try/Catch 块上抛出错误

Twilio Not Throwing Error on Try/Catch block when user opted out of messaging service [PHP/Laravel]

我用我的 phone 号码创建了一个对话,以选择退出短信服务进行测试。 选择退出后,我将删除对话,这样我的应用程序就不会尝试将消息添加到当前 Conversation->sid.

但是当我创建对话时它不会抛出 Twilio 错误。 尽管它应该给我一个禁止的错误,因为收件人号码被列入黑名单。

这是我的示例代码:

  //store registered user in DB
    public function createConversations(Request $request)
    {
        //check if forms are inputted properly
        $this->validate($request, [
            'Friendly_Title' => 'required|unique:conversations|max:100',
            'Recipient_Number' => 'required|size:11',
            'text_body' => 'required'
        ]);

        //twilio credentials
        $sid = config('services.twilio.example_sid');
        $token = config('services.twilio.example_token');
        $twilio = new Client($sid, $token);


        //first check if guest number exists in the db('conversations')
        $convo_guest_number = DB::table('example')->where('Recipient_Number', '=', $request->Recipient_Number)->count();
        if ($convo_guest_number > 0) {
            try {
                //get requested channel_id from new conversation
                $CH_ID = DB::table('example')->where('Recipient_Number', '=', $request->Recipient_Number)->get('Channel_Id')->first();
                //update the updated_at column
                DB::table('example')->where('Recipient_Number', '=', $request->Recipient_Number)->update(['updated_at' => now()]);

                //create body message
                $message = $twilio->conversations->v1->conversations($CH_ID->Channel_Id)
                    ->messages
                    ->create([
                            "author" => "EXAMPLE AUTHOR", //$participant->sid
                            "body" => $request->text_body
                        ]
                    );
                //return back with warning message
                return back()->with('warning', 'Conversation already exists, added message to existing conversation');
            }catch (TwilioException $e) {
                return back()->with("warning", "Error: {$e->getMessage()}");
            }
        } //if new conversatoin
        else {
            try {
                //create new conversation
                $conversation = $twilio->conversations->v1->conversations
                    ->create([
                            "friendlyName" => $request->Friendly_Title
                        ]
                    );
                //store the conversation channel id
                $Conversation_SID = $conversation->sid;

                //fetch the newly created conversation
                $conversation = $twilio->conversations->v1->conversations($Conversation_SID)
                    ->fetch();

                //set recipient number and twilio number
                //add participant
                $user_number = "+". config('services.example.example_phone_number');
                $participant_number = "+" . $request->Recipient_Number;
                $participant = $twilio->conversations->v1->conversations($conversation->sid)
                    ->participants
                    ->create([
                            "messagingBindingAddress" => $participant_number,
                            "messagingBindingProxyAddress" => $user_number
                        ]
                    );

                $participant = $twilio->conversations->v1->conversations($conversation->sid)
                    ->participants
                    ->create([
                            "identity" => "EXAMPLE AUTHOR"
                        ]
                    );

                // set you as the author of the meessage
                $message = $twilio->conversations->v1->conversations($conversation->sid)
                    ->messages
                    ->create([
                            "author" => "DEMO AUTHOR", //$participant->sid
                            "body" => $request->text_body . "\r\nReply 'STOP' to unsubscribe."
                        ]
                    );

                //insert into conversations table
                DB::table('example')
                    ->insert([
                        'user_id' => auth()->user()->id,
                        'Friendly_Title' => $request->Friendly_Title,
                        'Channel_Id' => $conversation->sid,
                        'Recipient_Number' => $request->Recipient_Number,
                        'text_body' => $request->text_body,
                        'twilio_number' => config('services.twilio.example_phone_number'),
                        'twilio_author' => 'EXAMPLE AUTHOR',
                        'NeedsAttention' => false,
                        'checkedConversation' => false,
                        'optedOut' => false,
                        'last_outbound_date' => now(),
                        'created_at' => now(),
                        'updated_at' => now()
                    ]);
                return back()->with("success", "{$request->Friendly_Title} - was created ");
            }catch (TwilioException $e) {
                return back()->with("warning", "Error: {$e->getMessage()}");
            }
        }
    }

是否需要为每个小型 Twilio 任务添加一个 try/catch 块? 所有证件都是挂件

此处为 Twilio 开发人员布道师。

由于对话 API 涉及使用不同消息发送方法(例如 SMS 或聊天)的参与者,因此在对话中创建参与者不会检查选择退出状态。用户也可以在添加到对话后选择退出。

同样,当您向对话发送一条消息时,该消息会分散到每个参与者。因此,响应无法告诉您是否有一条消息由于某种原因而未能发送。 API 请求将成功,因为对话中的消息资源已成功创建,即使在此过程中发送个别消息失败也是如此。

处理此问题的最佳方法是注册交付 Webhook。 onDeliveryUpdated webhook 将在消息状态更改时触发,您将能够在您的应用程序中记录该消息是已发送还是由于某种原因失败。这将允许您处理每个参与者的选择退出和其他失败。