通过 Twilio 通知获取短信发送状态
Get status of SMS send though Twilio notify
我已经使用 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。如何跟踪此消息的状态?
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
应该是这样的:
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'
}
})
使用 PHP
通过 Twilio 发送多条短信的步骤
- 在 Twilio 上创建帐户
- 获取 Twilio 号码
- 在此处添加 Phone 个号码:https://www.twilio.com/console/phone-numbers/verified
- 在此处创建通知服务:https://www.twilio.com/console/notify/services并复制"SERVICE SID"(这将在代码中使用)
- 在此处创建消息服务:https://www.twilio.com/console/sms/services
- Select
MESSAGING SERVICE SID
在 Notify - Configuration 页面下,您在第 5 步创建的内容:
$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"
]);
我已经使用 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。如何跟踪此消息的状态?
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
应该是这样的:
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'
}
})
使用 PHP
通过 Twilio 发送多条短信的步骤- 在 Twilio 上创建帐户
- 获取 Twilio 号码
- 在此处添加 Phone 个号码:https://www.twilio.com/console/phone-numbers/verified
- 在此处创建通知服务:https://www.twilio.com/console/notify/services并复制"SERVICE SID"(这将在代码中使用)
- 在此处创建消息服务:https://www.twilio.com/console/sms/services
- Select
MESSAGING SERVICE SID
在 Notify - Configuration 页面下,您在第 5 步创建的内容:
$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"
]);