Monday, 5 June 2017

Unable to send button template as response on Facebook Messenger Platform (Node.js)

I am developing a chatbot on the Facebook Messenger Platform using Node.js. This is my functioning code for setting up a text response:

const fbReq = request.defaults({
  uri: 'https://graph.facebook.com/me/messages',
  method: 'POST',
  json: true,
  qs: {
    access_token: Config.FB_PAGE_TOKEN
  },
  headers: {
    'Content-Type': 'application/json'
  },
});


const fbMessage = (recipientId, msg, cb) => {
  const opts = {
    form: {
      recipient: {
        id: recipientId,
      },
      message: {
        text: msg,
      },
    },
  };

  fbReq(opts, (err, resp, data) => {
    if (cb) {
      cb(err || data.error && data.error.message, data);
    }
  });
};

I am also able to set up an image response this way. However, when I try to make the response a button template (https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template), no response is received. No error is thrown either.

const fbInfo = (recipientId, cb) => {
  const opts = {
    form: {
      recipient: {
        id: recipientId,
      },
      message: {
        attachment:{
          type:"template",
          text:"Check out our website",
          payload:{
            template_type:"button",
            buttons:[
              {
                type:"web_url",
                url:"https://some.website.com",
                title:"Website"
              }
            ]
          }
        }
      }
    }
  };

  fbReq(opts, (err, resp, data) => {
    if (cb) {
      cb(err || data.error && data.error.message, data);
    }
  });
};



via Rahul Mukherji

No comments:

Post a Comment