首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nexmo API短信送达回执

Nexmo API短信送达回执
EN

Stack Overflow用户
提问于 2017-11-20 18:17:48
回答 1查看 672关注 0票数 0

我正在使用laravel通知向我的应用程序的注册用户发送文本消息。

我最初使用的是默认的Nexmo通道,但后来创建了自己的通道来排除任何问题。

我将每个消息存储在我的数据库中,每个消息都有一个'messages‘数组列,其中包含Nexmo发送的每个物理消息的JSON响应信息。

例如:

代码语言:javascript
复制
[{"to":"441122334455","message-id":"0B00000099A49D63","status":"0","remaining-balance":"7.00500000","message-price":"0.03330000","network":"23410"}]

我的自定义短信频道如下

代码语言:javascript
复制
namespace App\Notifications\Channels;

use Illuminate\Notifications\Notification;
use Nexmo\Laravel\Facade\Nexmo;

class CustomSmsChannel
{
    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $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
        ]);

    }
}

这将发送消息OK,并且我收到它很好,没有问题。

我已经在Nexmo控制面板上将递送回执的web挂钩设置为正确的URL (我使用的是http,它需要是https吗?)

我的路径文件如下所示

代码语言:javascript
复制
Route::get('sms/delivery-status', 'SmsController@deliveryStatus');

使用我的SmsController方法

代码语言:javascript
复制
/**
 * The webhook for Nexmo to receive delivery statuses.
 * 
 * @param  \Illuminate\Http\Request  $request
 * @return  \Illuminate\Http\Response
 */
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);
}

简而言之,它搜索phone_number与'to‘值匹配的所有消息。然后,对于每条消息,它循环遍历Nexmo发送的每个消息部分(存储在JSON列中)以匹配messageId。

一旦找到messageId,它就用收据上提供的JSON替换JSON。

代码语言:javascript
复制
[{"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"}]

然后,这用于确认消息已经在我的视图中传递(通过确保所有部分都显示为已传递,等等)

如果我手动执行GET请求,并在请求中设置正确的'to‘和'messageId’变量,那么数据库行就会更新得很好,这样就可以排除这种情况。

很抱歉发了这么长的帖子,这可能不是最有说服力的方式,但是我错过了什么?!

EN

回答 1

Stack Overflow用户

发布于 2017-11-20 19:25:08

我找到问题了。

记录请求(我不知道为什么不这样做,但谢谢您的建议),我意识到我正在为错误的号码搜索匹配的phone_number。

常识告诉我,我用了' to‘,但我需要用'msisdn'?!

无论如何,在控制器中更改了它,但它不能正常工作!:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47389675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档