我正在尝试将我的facebook messenger聊天机器人与mongodb数据库连接起来。这是一个简单的问题聊天机器人,它将采用文本并显示一些带有选项的按钮,如果用户点击一个外设,它应该显示更多带有选项的按钮等等......这里我在nodejs中配置了chatbot,而不是硬编码,我想要从mongodb数据库获取数据并将请求发布到同一数据库。任何人都可以帮助我如何连接messenger聊天机器人与mongodb数据库,以及我应该如何编写发布和获取响应的模式。提前致谢。这是我的messenger聊天机器人代码。
if (received_message.text) {
// Get the URL of the message attachments
response = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "Welcome to Flying Sphaghetti Monster Restaurant!",
subtitle: "Choose any of the options below.",
image_url:
"https://content3.jdmagicbox.com/comp/hyderabad/h5/040pxx40.xx40.140516124003.h3h5/catalogue/flying-spaghetti-monster-restaurant-jubilee-hills-hyderabad-home-delivery-restaurants-p6kmmr.jpg",
default_action: {
type: "web_url",
url:
"https://content3.jdmagicbox.com/comp/hyderabad/h5/040pxx40.xx40.140516124003.h3h5/catalogue/flying-spaghetti-monster-restaurant-jubilee-hills-hyderabad-home-delivery-restaurants-p6kmmr.jpg",
webview_height_ratio: "tall"
},
buttons: [
{
type: "postback",
title: "Walkin!",
payload: "A"
},
{
type: "postback",
title: "Reserve table!",
payload: "B"
},
{
type: "postback",
title: "Feed back!",
payload: "C"
}
]
}
]
}
}
};
else if (received_message.attachments){ let attachment_url = received_message.attachments[0].payload.url;
response = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "What this picture is for?",
subtitle: "Tap a button to answer.",
image_url: attachment_url,
buttons: [
{
type: "postback",
title: "Review!",
payload: "yes"
},
{
type: "postback",
title: "Suggestion!",
payload: "yeah"
}
]
}
]
}
} callSendAPI(sender_psid, response);
};}
这就是我处理回发的方式......
function handlePostback(sender_psid, received_postback) {
let response;
let payload = received_postback.payload;
if (payload === "A")
{
response = {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "you have choosen for walkin!",
subtitle: "please choose these available walkins!.",
image_url:
"https://content3.jdmagicbox.com/comp/hyderabad/h5/040pxx40.xx40.140516124003.h3h5/catalogue/flying-spaghetti-monster-restaurant-jubilee-hills-hyderabad-home-delivery-restaurants-p6kmmr.jpg",
default_action: {
type: "web_url",
url:
"https://content3.jdmagicbox.com/comp/hyderabad/h5/040pxx40.xx40.140516124003.h3h5/catalogue/flying-spaghetti-monster-restaurant-jubilee-hills-hyderabad-home-delivery-restaurants-p6kmmr.jpg",
webview_height_ratio: "tall"
},
buttons: [
{
type: "postback",
title: "4 PM",
payload: "a"
},
{
type: "postback",
title: "5 PM",
payload: "b"
},
{
type: "postback",
title: "6 PM",
payload: "c"
}
]
}
]`