Share
Webhook 是我们平台最有用的功能之一。它允许开发人员和网站所有者侦听来自设备或 WhatsApp 服务器的事件,这对于创建自动化任务很有用。此功能专为接收事件而设计,例如短信、WhatsApp 聊天、ussd 响应和 android 通知。
Posted At: 2月 10, 2023 - 1,327 Views
使用案例
- 为 SMS 和 WhatsApp 创建自动回复机器人。
- 接收时将消息和聊天保存到您自己的数据库中。
- 接收通知时将通知保存到您自己的数据库中。
- 接收时将 USSD 响应保存到您自己的数据库中。
- 将有效负载发送到指定的电子邮件地址。
- 当您收到来自 Facebook 的通知时发送短信/聊天。
- 当您收到来自应用的通知时,请执行某些操作。
它是如何工作的
系统从两个源获取接收到的事件,然后将调用 Webhook URL 并发送有效负载数据。当您的 Webhook 服务器收到它时,您可以自己对有效负载执行任何操作。有效载荷采用POST方式发送,确保高效投递。
有效载荷结构
有效载荷结构简单明了;您只需要检查有效负载的类型,然后处理数据正文的内容。
# 短信
[
"type" => "sms", // type of payload: received sms
"data" => [
"id" => 2, // unique id from the system
"rid" => 10593, // unique id from the device
"sim" => 1, // sim card slot
"device" => "00000000-0000-0000-d57d-f30cb6a89289", // device unique id
"phone" => "+639760713666", // sender phone number
"message" => "Hello World!", // message
"timestamp" => 1645684231 // receive timestamp
]
]
[
"type" => "whatsapp", // type of payload: received whatsapp chat
"data" => [
"id" => 2, // unique id from the system
"wid" => "+639760713666", // whatsapp account phone number
"phone" => "+639760666713", // sender phone number
"message" => "Hello World!", // message
"timestamp" => 1645684231 // receive timestamp
]
]
# ussd
[
"type" => "ussd", // type of payload: received ussd response
"data" => [
"id" => 98, // unique id from the system
"sim" => 1, // sim card slot
"device" => "00000000-0000-0000-d57d-f30cb6a89289", // device unique id
"code" => "*143#", // ussd code
"response" => "Sorry! You are not allowed to use this service.", // ussd response
"timestamp" => 1645684231 // receive timestamp
]
]
# 通知
[
"type" => "notification", // type of payload: received notification
"data" => [
"id" => 77, // unique id from the system
"device" => "00000000-0000-0000-d57d-f30cb6a89289", // device unique id
"package" => "com.facebook.katana", // application package name
"title" => "Someone commented on your post!", // notification title
"content" => "Someone commented on your post!", // notification content
"timestamp" => 1645684231 // receive timestamp
]
]
代码示例
Webhooks)
/**
* Validate webhook secret
*/
if(isset($request["secret"]) && $request["secret"] == $secret):
// Valid webhook secret
$payloadType = $request["type"];
$payloadData = $request["data"];
// do something with the payload
print_r($payloadType);
print_r($payloadData);
else:
// Invalid webhook secret
endif;