使用Facebook Send-API顺序发送消息 [英] Sequential Message Sending Using Facebook Send-API

查看:191
本文介绍了使用Facebook Send-API顺序发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FB Send-Message 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)(返回发送消息的承诺);

(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)(使用消息遍历数组并执行函数(1),然后将其插入链中).

(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.

同一问题: (1) Facebook Messenger机器人未按顺序发送消息

推荐答案

此处是一个Facebook错误- https://developers.facebook.com/bugs/565416400306038

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

这篇关于使用Facebook Send-API顺序发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆