Search code examples
javascriptnode.jssettimeoutfacebook-messengerfacebook-messenger-bot

Timed response not working messenger bot


I am working on a facebook messenger bot using node.js. I am trying to get my code to send a timed response to the user but it is not working. Here is the code:

function handlePostback(sender_psid, received_postback) {
let response;
let subscribe;

// Get the payload for the postback
let payload = received_postback.payload;

// Set the response based on the postback payload
if (payload === 'yes') {
  response = { "text": "Thanks! Here's your first task : Hold the door for someone. " };
  subscribe = true;
} else if (payload === 'no') {
  response = {"text":"That's unfortunate" };
  subscribe = false;
}

// Send the message to acknowledge the postback
callSendAPI(sender_psid, response);

  if (subscribe===true) {
    response = {"text":"Today's task will be:"};
    setTimeout(sendTask(), 5000);
  }

}

Solution

  • The problem was solved using a different send function for the timed response.