0
我是一個編程新手,我已經成功運行Facebook Messenger Platform - quick start案例的示例。但我在原始示例上做了一些小的更改時遇到了問題:如何觸發回發按鈕打開另一個結構化的消息在facebook messenger chatbot
我想回發按鈕單擊事件以觸發另一個結構化消息。當用戶單擊菜單頁面內的「咖啡」按鈕時它會顯示另一個結構消息,即咖啡頁面。
我已經使用了,如果condiction檢查
JSON.stringify(event.postback.payload)
是特定的字符串(這應該是「咖啡」),但它沒有,當我顯示其他結構化的使者點擊菜單頁面中的'咖啡'按鈕。
這裏是我下面的網絡掛接代碼:
app.post('/webhook/', function (req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
if (text === 'menu') {
sendMenuMessage(sender)
continue
} else if (text === 'coffee') {
sendCoffeeMessage(sender)
continue
}
sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
}
if (event.postback) {
let text = JSON.stringify(event.postback.payload)
sendTextMessage(sender, "Postback.payload received: " + text.substring(0, 200), token)
if (text === 'coffee') {
sendCoffeeMessage(sender)
}
continue
}
}
res.sendStatus(200)})
,這兒有2信使的結構,其中包括: 1.菜單頁面(顯示各類產品的) 2.咖啡頁(顯示器只有咖啡)
1.Menu頁 要打開的菜單頁面,必須有一個函數調用sendMenuMessage:
function sendMenuMessage(sender) {
let messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "Our Menu",
"subtitle": "Click buttons to see more",
"image_url": "img.jpg",
"buttons": [{
"type": "postback",
"title": "Coffee",
"payload": "coffee",
}, {
"type": "postback",
"title": "Tea",
"payload": "tea",
}],
}]
}
}
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: token
},
method: 'POST',
json: {
recipient: {
id: sender
},
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})}
2.Coffee頁 要打開咖啡網頁,必須有一個函數調用sendCoffeeMessage:
function sendCoffeeMessage(sender) {
let messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "Americano",
"subtitle": "5$",
"image_url": "img2.jpg",
"buttons": [{
"type": "postback",
"title": "Detail",
"payload": "americano_detail",
}],
}, {
"title": "Latte",
"subtitle": "5.5$",
"image_url": "img3.jpg",
"buttons": [{
"type": "postback",
"title": "Detail",
"payload": "latte_detail",
}],
}]
}
}
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: token
},
method: 'POST',
json: {
recipient: {
id: sender
},
message: messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})}