請先至哈哈姆特不EY-Bot開發者後台設定webhook網址
哈哈姆特server將會以 POST 的方式,將 webhook 事件傳送至所填寫的webhook網址。
{
"botid":<BOT_ID>,
"time":1512353744843,
"messaging":[
{
"sender_id":<SENDER_ID>,
"message":{
"text":"Hello~"
}
}
]
}
{
"botid":<BOT_ID>,
"time":1512353744843,
"messaging":[
{
"sender_id":<SENDER_ID>,
"message":{
"sticker":{
"group":<STICKER_GROUP>
"id":<STICKER_ID>
}
}
}
]
}
接收 webhook 事件後,必須傳回 200 OK HTTP 回應。
這項 HTTP 要求將包含 x-baha-data-signature 標頭,其中包含要求承載的 SHA1 簽章,所使用的密鑰為應用程式密鑰,開頭則是 sha1=。您的回呼端點可以檢查這個簽章,以驗證承載的完整性和原始來源。
var crypto = require('crypto');
var hmac = crypto.createHmac('sha1', <APP_SECRET>);
hmac.update(JSON.stringify(req.body), 'utf-8');
var expectedSignature = 'sha1=' + hmac.digest('hex');
if (req.headers['x-baha-data-signature'] != expectedSignature) {
console.log('not ok');
} else {
console.log('ok');
}
$body = file_get_contents("php://input");
$signature = $_SERVER['HTTP_X_BAHA_DATA_SIGNATURE'];
$expectedOutput = 'sha1='.hash_hmac('sha1', $body, <APP_SECRET>, false);
if ($signature !== $expectedOutput) {
echo 'not ok';
} else {
echo 'ok';
}