0

I'm using Wit.ai to create a chatbot on facebook messenger. However, if I set quick replies for a certain user input, having deployed the bot and entering said input, I only see the text that the quick reply buttons are supposed to follow.

Govind Mohan
  • 109
  • 2
  • 7

1 Answers1

0

I'm assuming you are using the messenger exemple in the Node.js SDK.

This code does not support quickreplies. First, you can use my code to implement the needed feature in the sendTextMessage function:

const sendMessage = (id, message) => {
  const body = JSON.stringify({
    recipient: { id },
    message: message,
  });
  const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
  return fetch('https://graph.facebook.com/me/messages?' + qs, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body,
  })
  .then(rsp => rsp.json())
  .then(json => {
    if (json.error && json.error.message) {
      throw new Error(json.error.message);
    }
    return json;
  });
};

const sendTextMessage = (id, text, quickreplies) => {
  if(!quickreplies) return sendMessage(id, {text});

  if(quickreplies.length > 10) {
    throw new Error("Quickreplies are limited to 10");
  }

  let body = {text, quick_replies: []};
  quickreplies.forEach(qr => {
    body.quick_replies.push({
      content_type: "text",
      title: qr,
      payload: 'PAYLOAD' //Not necessary used but mandatory
    });
  });
  return sendMessage(id, body);
};

Second, you have to consider the quickreplies in the "send" action. Here is a very simple "send action" example:

const actions = {
  send({sessionId}, {text, quickreplies}) {
    const recipientId = sessions[sessionId].fbid;
    return sendTextMessage(recipientId, text, quickreplies);
  }
}

EDIT: Also, note that the messenger quickreplies are limited to 20 characters.

Nitsuja
  • 73
  • 6