0
votes

I am trying to connect my facebook messenger chatbot with mongodb database. It is a simple questionery chatbot which will take a text and display some buttons with options and if user clicks one outton it should display more buttons with options and so on... Here i have configured chatbot in nodejs and instead on hardcoding, I want to fetch the data from mongodb database and post requests to the same database. Can anyone help me how to connect messenger chatbot with mongodb database and also how should i write the schemas for posting and getting responses. Thanks in advance. Here is my code of messenger chatbot.

3

3 Answers

0
votes
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"
            }
          ]
        }
      ]
    }
  }
};
0
votes
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);
};}
0
votes

This is how i am handling postbacks...

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"
            }
          ]
        }
      ]`