通过Twilio通知获取SMS发送的状态

时间:2018-02-08 13:37:38

标签: twilio twilio-api twilio-php

我已使用Twilio api发送通知消息。消息正在正确发送。

$notification = $client
    ->notify->services($serviceSid)
    ->notifications->create([
        "toBinding" => [
            '{"binding_type":"sms", "address":"+971444444444"}',
            '{"binding_type":"sms", "address":"+971444444445"}'
        ],
        'body' => 'Test message 8'
    ]);

请求的响应是201并返回以'NT'开头的sid。如何跟踪此消息的状态?

3 个答案:

答案 0 :(得分:1)

应该是这样的:

PHP:

$notification = $client
    ->notify->services($serviceSid)
    ->notifications->create([
        "toBinding" => [
            '{"binding_type":"sms", "address":"+971444444444"}',
            '{"binding_type":"sms", "address":"+971444444445"}'
        ],
        'body' => 'Test message 8'
        'sms' => ['status_callback' => 'https://youcallbackurl.com']
    ]);

或Javascript

const service = twilio.notify.services(notifyId);
const bindings = numbers.map(number => {
        return JSON.stringify({ 
            binding_type: 'sms', 
            address: number,
        });
    });

service.notifications.create({
        toBinding: bindings,
        body: message,
                sms: {
                    status_callback: 'https://youcallbackurl.com'
                }
  })

答案 1 :(得分:0)

twilio有一个callback状态webhook,您需要对其进行配置以跟踪短信的发送状态

$notification = $client
    ->notify->services($serviceSid)
    ->notifications->create([
        "toBinding" => [
            '{"binding_type":"sms", "address":"+971444444444"}',
            '{"binding_type":"sms", "address":"+971444444445"}'
        ],
        'body' => 'Test message 8'
        'statusCallback' => "your public end point to track sms delivery status"
    ]);

查看更多here

答案 2 :(得分:0)

使用PHP通过Twilio发送多个SMS的步骤

  1. 在Twilio上创建帐户
  2. 获取一个Twilio号码
  3. 在此处添加电话号码:https://www.twilio.com/console/phone-numbers/verified
  4. https://www.twilio.com/console/notify/services处创建通知服务并复制“ SERVICE SID”(将在代码中使用)
  5. 在此处创建消息服务:https://www.twilio.com/console/sms/services
  6. 在“通知-配置”页面的第5步中创建的内容下,选择MESSAGING SERVICE SID
$sid = 'ACb2f967a907520b85b4eba3e8151d0040'; //twilio service SID
$token = '03e066de1020f3a87cec37bb89f56dea'; //twilio Account SID
$serviceSid = 'IS4220abf29ae4169992b8db5fc2668b10'; //Notify service SID
$client = new Client($sid, $token);
$rs = $client->notify->services($serviceSid)->notifications->create([
    "toBinding" => [
        '{"binding_type":"sms", "address":"+919999999999"}',
        '{"binding_type":"sms", "address":"+919999999999"}'
    ],
    'body' => 'Test message 8',
    'statusCallback' => "your public end point to track sms delivery status"
]);