Nexmo API SMS delivery receipt
I'm using laravel Notifications to send a text message to registered users of my application.
I was originally using the default Nexmo channel but have since created my own channel to rule out any issues.
I am storing each message in my database, with each message having a 'messages' array column which contains JSON response information for each physical message sent by Nexmo.
eg.
[{"to":"441122334455","message-id":"0B00000099A49D63","status":"0","remaining-balance":"7.00500000","message-price":"0.03330000","network":"23410"}]
My custom SMS channel is as follows
namespace AppNotificationsChannels;
use IlluminateNotificationsNotification;
use NexmoLaravelFacadeNexmo;
class CustomSmsChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param IlluminateNotificationsNotification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toCustomSms($notifiable);
return Nexmo::message()->send([
'to' => $notifiable->phone_number,
'from' => env('NEXMO_FROM'),
'text' => $message->content,
'status-report-req' => 1
]);
}
}
This sends the message OK and I receive it fine, no issues there.
I've set the web hook on the Nexmo control panel for delivery receipts to the correct URL (I'm using http, does it need to be https?)
My routes file is as follows
Route::get('sms/delivery-status', 'SmsController@deliveryStatus');
With my SmsController method
/**
* The webhook for Nexmo to receive delivery statuses.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function deliveryStatus(Request $request)
{
if (!isset($request->messageId) OR !isset($request->status)) {
Log::error('Not a valid delivery receipt');
return;
}
// Loop for all main SMS messages with the given phone number.
$entries = SmsHistory::where('phone_number', $request->to)->get();
// Loop through each of the SMS message to that number.
foreach ($entries as $item) {
// Loop through each of the rsent messages for the main message.
foreach ($item->messages as $key => $message) {
// Check whether the given messageID matches the one stored in the messages array field.
if ($message['message-id'] == $request->messageId) {
$messages = $item->messages;
// Remove the current message
array_pull($messages, $key);
// Add the new message
$messages = array_add($messages, $key, $request->input());
$item->messages = $messages;
$item->save();
}
}
}
return response('OK', 200);
}
Which in a nutshell, searches for all messages where the phone_number matches the 'to' value. Then for each message it loops through each of the message parts sent by Nexmo (stored in the JSON column) to match the messageId.
Once the messageId is found, it replaces the JSON with the JSON provided on the receipt eg.
[{"msisdn":"441122334455","to":"441122334455","network-code":"23410","messageId":"0B000000999B5FCB","price":"0.02000000","status":"delivered","scts":"1208121359","err-code":"0","message-timestamp":"2020-01-01 12:00:00"}]
This is then used to confirm that the message has been delivered in my views (by making sure all parts are shown as delivered, etc)
If I perform the GET request manually and set the correct 'to' and 'messageId' variables in the request the database row is updated fine so that rules that out.
Sorry for the long post and it's probably not the most eloquent way of doing this but what am I missing?!
I found the issue.
Logging the request (why I didn't do this I don't know but thank you for suggesting it) I realised that I was searching for a matching phone_number for the wrong number.
Common sense stated that I used 'to' but I needed to use 'msisdn'?!
Anyway, changed this in the controller and it's not working fine! :)
链接地址: http://www.djcxy.com/p/33102.html上一篇: Laravel应用程序不正确地路由mod
下一篇: Nexmo API短信发送收据