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