[英]Twilio Not Throwing Error on Try/Catch block when user opted out of messaging service [PHP/Laravel]
我創建了一個與我的電話號碼的對話,以選擇退出短信服務進行測試。 一旦選擇退出,我將刪除對話,因此我的應用程序不會嘗試將消息添加到當前的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 塊? 所有憑據都是 PLACHOLDERS
Twilio 開發人員布道者在這里。
由於 Conversations API 使用不同的消息發送方法(例如 SMS 或聊天)處理參與者,因此在對話中創建參與者不會檢查退出狀態。 用戶也可以在被添加到對話后選擇退出。
同樣,當您向對話發送消息時,該消息會散布給每個參與者。 因此,響應無法告訴您單個消息是否因某種原因未能發送。 API 請求將成功,因為會話中的消息資源已成功創建,即使在此過程中稍后發送單個消息失敗。
解決此問題的最佳方法是注冊交付 Webhook。 onDeliveryUpdated
webhook將在消息的狀態更改時觸發,您將能夠在應用程序中記錄該消息是否已發送或因某種原因失敗。 這將允許您處理每個參與者的選擇退出和其他失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.