I’m trying to send messages using FB Send-Message API in sequential order, but have an issue with it. As a result my bot sends messages in wrong order. I have the following functions:
function sendMessage(recipient, payload, accessToken) {
return axios.post(baseURL + 'v2.11/me/messages/?access_token=' + accessToken,
Object.assign({}, {
messaging_type: "RESPONSE",
recipient: {
id: recipient
}
}, payload) );
}
(1)(returns Promise of sending message);
let async_actions;
let promise_chain = Q.fcall(function(){});
async_actions = flow['messages'].map(message => {
return sendMessage(recipient, message['payload'], flow['page']['accessToken'])
});
async_actions.forEach(async_action => {
//console.log(async_action)
promise_chain = promise_chain.then(f);
});
(2)(Loops through array with messages and executes function (1), then inserts it in a chain).
What should I do to send messages sequentially? For now it sends them randomly, I mean how to wait until one message is sent and then send another? Thank you.
Same issue:
(1) Facebook Messenger bot not sending messages in order
2
Answers
It was a Facebook bug reported here - https://developers.facebook.com/bugs/565416400306038
When you call
sendMessage
, it will return a promise before the promise is actually resolved. Therefore, you don’t want to call the nextsendMessage
until the previoussendMessage
call resolves. However, in your.map()
call you do exactly that. Instead, you want to return a function that will call the correctsendMessage
Simplest way would be to use a sequential promise library, or you can roll your own using
reduce()
.