Facebook Messenger bot not sending messages in order

后端 未结 11 2053
遥遥无期
遥遥无期 2021-01-01 21:00

I\'m playing around with building a simple Facebook Messenger chatbot and I\'m having trouble sending messages in sequence.

In the example above, it should

相关标签:
11条回答
  • 2021-01-01 21:20

    Implement the send request as a Promise and only send consequent messages once the previous one is resolved

    const send = (userId, messageData)  => {
    
          return new Promise((resolve, reject) => {
            request
            (
                {
                    url     : BASE_URL + "me/messages",
                    qs      : { access_token : PAGE_ACCESS_TOKEN },
                    method  : "POST",
                    json    : 
                            {
                                recipient: { id : userId },
                                message: messageData,
                            }
                }, (error, response, body) => 
                {
                    if (error) { console.log("Error sending message: " + response.error); return reject(response.error); }
                    else if (response.body.error) { console.log('Response body Error: ' + response.body.error); return reject(response.body.error); }
    
                    console.log("Message sent successfully to " + userId); 
                    return resolve(response);
                }
            );    
        });
    };
    
    0 讨论(0)
  • 2021-01-01 21:23

    Based on the recursive solution proposed by @user3884594, I kind of make it work using this (I removed the error handling in order to simplify):

    send_messages (["message 01", "message 02", "message 03"]);
    
    function send_messages (which, i = 0)
    {
        request({
            url: 'https://graph.facebook.com/v2.10/me/messages',
            qs: { access_token: FACEBOOK_ACCESS_TOKEN },
            method: 'POST',
            json: { recipient: { id: senderId }, message: { text: which [i] }
        }, (error, response, body) => 
        {
            // You need to put your error handling logic here
            if (i++ < which.length - 1)
                send_messages (which, i);
        });
    }
    
    0 讨论(0)
  • 2021-01-01 21:23

    You can try putting them inside a setTimeout function so each one goes after a certain period of time.

    So replace this:

    sendTextMessage(user, "Hello!");
    sendTextMessage(user, "1");
    sendTextMessage(user, "2");
    sendTextMessage(user, "3");
    

    With this:

    sendTextMessage(user, "Hello!");              
    
    // 1 second
    
    setTimeout(function() {
        sendTextMessage(user, "1");
    }, 1000)
    
    // 2 seconds
    
    setTimeout(function() {
        sendTextMessage(user, "2");
    }, 2000)
    
    // 3 seconds
    
    setTimeout(function() {
        sendTextMessage(user, "3");
    }, 3000)    
    

    And they should go one after another. You could also embed the functions inside each other if need be.

    0 讨论(0)
  • 2021-01-01 21:25

    I had exactly same problem, that solution worked for me:

    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);
    

    That's my question: Sequential Message Sending Using Facebook Send-API

    0 讨论(0)
  • 2021-01-01 21:25

    You can achieve QUEUING by promises.

    function delay(time) {
      return new Promise(function(resolve, reject) {
        setTimeout(resolve, time);
      });
    }
    
    delay(2000).then(() => {
      console.log('hi');
      delay(2000).then(() => {
        console.log('hello');
        delay(2000).then(() => {
          console.log('welcome');
        })
      })
    })

    0 讨论(0)
  • 2021-01-01 21:26

    I submitted a bug report to Facebook about this because I was having the same problem. They acknowledged that it is indeed a bug and are working to fix it: https://developers.facebook.com/bugs/565416400306038

    0 讨论(0)
提交回复
热议问题