【问题标题】:Nexmo API SMS delivery receiptNexmo API 短信收据
【发布时间】:2017-11-20 10:17:47
【问题描述】:

我正在使用 laravel 通知向我的应用程序的注册用户发送短信。

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

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

例如。

[{"to":"441122334455","message-id":"0B00000099A49D63","status":"0","remaining-balance":"7.00500000","message-price":"0.03330000","network":"23410"}]

我的自定义短信渠道如下

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
        ]);

    }
}

这发送消息正常,我接收良好,没有问题。

我已经在 Nexmo 控制面板上设置了网络挂钩,以便将送货收据设置为正确的 URL(我使用的是 http,它需要是 https 吗?)

我的路由文件如下

Route::get('sms/delivery-status', 'SmsController@deliveryStatus');

使用我的 SmsController 方法

/**
 * 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,例如。

[{"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' 变量,则数据库行会更新得很好,以便排除这种情况。

很抱歉,这篇文章很长,这可能不是最雄辩的方式,但我错过了什么?!

【问题讨论】:

  • 我看不出你的问题是什么问题。是不是来自 Nexmo 时 webhook 没有处理?
  • 是的,网络挂钩要么不发送,要么不在控制器中处理。
  • 首先,您能否检查服务器日志以查看 Nexmo 是否命中您的端点?如果是这样,那么我会将请求记录在您的控制器中以查看有效负载是什么。
  • 但是如果我通过浏览器手动运行 GET 请求,它工作正常,所以我看不出这是代码问题?
  • 我想知道为什么这个循环是必要的,如果直接写类似`` $entries = SmsHistory::where('message-id', $request-> messageId)->update([ '消息'=>'新消息']); ```

标签: laravel vonage


【解决方案1】:

我发现了问题。

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

常识说我用'to'但我需要用'msisdn'?!

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多