I am writing a Facebook messenger bot and am trying to store a text reply after a postback. What I mean by this is I have a message that sends a post back and depending on what option they choose, they can type a message back and I store that text. The problem is I only want to store this text after they select an option for the post back message. Also the text can be anything, so I can't write an if statement for it.
Here is my code:
app.post('/', function (req, res) {
messaging_events = req.body.entry[0].messaging;
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i];
sender = event.sender.id;
if (event.message && event.message.text) {
text = event.message.text;
if (text == "Start") {
sendTextMessage(sender, "Hey User! I'm a bot");
}
}
else if (event.postback) {
// These are for chosing availibility
if (JSON.stringify(event.postback) == '{"payload":"postback"}') {
setTextMessage(sender, "Postback recieved");
}
}
}
Everything is set up and running. But when the postback is received I want to wait for a reply from the user before finishing so I can save the text. Like I said earlier it can be anything meaning I can't just write an if statement like I did with "Start". I also only want to save the text after they click the postback button.
I was thinking about adding a while loop and waiting for text to be received before finishing the postback function but couldn't figure it out.
So how would I wait for text after I sent a message with a postback?
Any help is appreciated, thanks!