1

我用我的电话号码创建了一个对话,以选择退出短信服务进行测试。一旦选择退出,我将删除对话,这样我的应用程序就不会尝试将消息添加到当前的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 块?所有凭证都是占位符

4

1 回答 1

0

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

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

同样,当您向对话发送消息时,该消息会散播给每个参与者。因此,响应无法告诉您单条消息是否因任何原因未能发送。API 请求将成功,因为会话上的消息资源已成功创建,即使稍后在该过程中发送单个消息失败。

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

于 2021-11-14T22:51:16.457 回答