Search code examples
node.jsfacebookaxiosqfacebook-messenger-bot

Sequential Message Sending Using Facebook Send-API


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


Solution

  • It was a Facebook bug reported here - https://developers.facebook.com/bugs/565416400306038

    function sendMessage(recipient, messages, accessToken, i) {
    
    
        axios.post(baseURL + 'v2.11/me/messages/?access_token=' + accessToken,
            Object.assign({}, {
                messaging_type: "RESPONSE",
                recipient: {
                    id: recipient
                }
            }, messages[i]['payload']) )
            .then(response => {
    
                if(i < messages.length) sendMessage( recipient, messages, accessToken, i+1 );
    
                },
                error => {})
            .catch(error => {});
    
    }
    sendMessage(recipient, flow['messages'], flow['page']['accessToken'], 0);