使用 Facebook 发送的顺序消息发送-API
Sequential Message Sending Using Facebook Send-API
我正在尝试使用 FB Send-Message API 按顺序发送消息,但遇到了问题。结果我的机器人以错误的顺序发送消息。我有以下功能:
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 发送消息的承诺);
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),然后将其插入链中)。
如何顺序发送消息?现在它随机发送它们,我的意思是如何等到发送一条消息然后发送另一条消息?谢谢。
同样的问题:
(1) Facebook Messenger bot not sending messages in order
当您调用 sendMessage
时,它会 return 在承诺实际解决之前发出承诺。因此,在上一个 sendMessage
调用解决之前,您不想调用下一个 sendMessage
。但是,在您的 .map()
调用中,您正是这样做的。相反,您想 return 一个 函数 来调用正确的 sendMessage
最简单的方法是使用 sequential promise 库,或者您可以使用 reduce()
.
自己滚动
const sequential = require('promise-sequential');
const async_actions = flow['messages'].map(message => {
// return a *function* which executes sendMessage when the function is called
return () => {
console.log(`sending message ${message['payload']}`);
sendMessage(recipient, message['payload'], flow['page']['accessToken'])
};
});
sequential(async_actions)
.then(res => {
console.log("all done");
console.log(res);
});
这是此处报告的 Facebook 错误 - 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);
我正在尝试使用 FB Send-Message API 按顺序发送消息,但遇到了问题。结果我的机器人以错误的顺序发送消息。我有以下功能:
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 发送消息的承诺);
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),然后将其插入链中)。
如何顺序发送消息?现在它随机发送它们,我的意思是如何等到发送一条消息然后发送另一条消息?谢谢。
同样的问题: (1) Facebook Messenger bot not sending messages in order
当您调用 sendMessage
时,它会 return 在承诺实际解决之前发出承诺。因此,在上一个 sendMessage
调用解决之前,您不想调用下一个 sendMessage
。但是,在您的 .map()
调用中,您正是这样做的。相反,您想 return 一个 函数 来调用正确的 sendMessage
最简单的方法是使用 sequential promise 库,或者您可以使用 reduce()
.
const sequential = require('promise-sequential');
const async_actions = flow['messages'].map(message => {
// return a *function* which executes sendMessage when the function is called
return () => {
console.log(`sending message ${message['payload']}`);
sendMessage(recipient, message['payload'], flow['page']['accessToken'])
};
});
sequential(async_actions)
.then(res => {
console.log("all done");
console.log(res);
});
这是此处报告的 Facebook 错误 - 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);