我已经完成了如何为粉丝页面设置Facebook聊天机器人的教程。 它非常完美,我们还可以更改关键字和弹出的消息,以防用户输入关键字。
现在唯一的问题是,我们不知道如何添加多个带有答案的关键字。
var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
var app = express()
app.set('port', (process.env.PORT || 5000))
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am a chat bot')
})
// for Facebook verification
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'token') {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
// API End Point -
app.post('/webhook/', function (req, res) {
messaging_events = req.body.entry[0].messaging
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i]
sender = event.sender.id
if (event.message && event.message.text) {
text = event.message.text
if (text === 'hi') {
sendGenericMessage(sender)
continue
}
sendTextMessage(sender, "parrot: " + text.substring(0, 200))
}
if (event.postback) {
text = JSON.stringify(event.postback)
sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token)
continue
}
}
res.sendStatus(200)
})
var token = " enter token here"
// function to echo back messages
function sendTextMessage(sender, text) {
messageData = {
text:text
}
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)
}
})
}
// Send an test message back as two cards.
function sendGenericMessage(sender) {
messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "title 1",
"subtitle": "subtitle",
"image_url": "url of image",
"buttons": [{
"type": "web_url",
"url": "https://www.facebook.com/",
"title": "Facebook"
}, {
"type": "web_url",
"url": "https://www.reddit.com",
"title": "Reddit"
},{
"type": "web_url",
"url": "https://twitter.com/",
"title": "Twitter"
}],
}, {
"title": "Title 2 ",
"subtitle": "Subtitle",
"image_url": "imgurl",
"buttons": [{
"type": "postback",
"title": "title",
"payload": "payload",
},{
"type": "postback",
"title": "title",
"payload": "payload",
}, {
"type": "postback",
"title": "title",
"payload": "payload",
}],
}, {
"title": "title",
"subtitle": "subtitle",
"image_url": "http://www.google.com",
"buttons": [{
"type": "postback",
"title": "title",
"payload": "payload",
},{
"type": "postback",
"title": "title",
"payload": "payload",
}, {
"type": "postback",
"title": "title",
"payload": "payload",
}],
}]
}
}
}
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)
}
})
}
&#13;
答案 0 :(得分:0)
您的意思是说 - 您需要为同一个答案处理多个关键字吗?如果是这种情况,则facebook会在webhook上将该消息作为文本发送。您需要抓住它并相应地将消息发回给Facebook。
与您发送的代码一样,[if(text ===&#39; hi&#39;)],您需要将示例消息(在这种情况下为hi)存储在系统中并映射答案。