如何使用 JSON 訊息?
Last updated
https://yourdomian.com?fields=user,userParams{
"bot_id": "bot-xxxxx",
"bot_uid": "3xxxxxxxxxxxd",
"bot_raw_uid": "2xxxxxxxxxxxx0",
"bot_pid": "1234779123812",
"bot_channel": 0,
"bot_isLinked": true,
"user": {
"id": "3xxxxxxxxxxxd",
"rawId": "2xxxxxxxxxxxx0",
"created": 1505360797321,
"name": "陳邦妮",
"gender": "female",
"last_name": "陳",
"pic": "https://scontent.xx.fbcdn.net/v/t31.0-1/p960x960/1800025_10202073294786135_45024857_o.jpg?oh=85252db7049dbf99de251175d03a6d46&oe=5A604B50",
"locale": "zh_TW",
"first_name": "邦妮",
"timezone": 8
},
"userParams": {
"input": "你好",
...
}
...
}var express = require('express')
var bodyParser = require('body-parser')
var crypto = require('crypto')
// Express error-handling middleware function.
// Read more: http://expressjs.com/en/guide/error-handling.html
function abortOnError(err, req, res, next) {
if (err) {
console.log(err)
res.status(400).send({ error: "Invalid signature." })
} else {
next()
}
}
var app = express();
var listener = app.listen(process.env.PORT ? process.env.PORT : 3000)
// body-parser is the first Express middleware.
app.use(bodyParser.json({ verify: verifyRequest }))
// Add an error-handling Express middleware function
// to prevent returning sensitive information.
app.use(abortOnError)
app.post('/webhook/', function (req, res) {
res.status(200).send("done!")
})// Calculate the X-Hub-Signature header value.
function getSignature(buf) {
var hmac = crypto.createHmac("sha1", process.env.BOTBONNIE_API_SECRET)
hmac.update(buf, "utf-8")
return "sha1=" + hmac.digest("hex")
}
// Verify function compatible with body-parser to retrieve the request payload.
// Read more: https://github.com/expressjs/body-parser#verify
function verifyRequest(req, res, buf, encoding) {
var expected = req.headers['x-hub-signature']
var calculated = getSignature(buf)
console.log("X-Hub-Signature:", expected, "Content:", "-" + buf.toString('utf8') + "-")
if (expected !== calculated) {
throw new Error("Invalid signature.")
} else {
console.log("Valid signature!")
}
}