我有一个关于在 facebook 中处理丰富消息的问题。
当我尝试使用 api.ai 发送消息时,
我发送消息,例如在此处输入链接描述
function MessageHandler(event) {
let sender = event.sender.id;
let text = event.message.text;
//apiai text request
let apiai = apiaiApp.textRequest(text, {
sessionId: 'MY_HOSTETTER' // use any arbitrary id
});
//get reponse from apiai
apiai.on('response', (response) => {
let apiaiText = response.result.fulfillment.speech;
//let apiaiType = response.result.fulfillment.type;
var apiaiMessages = response.result.fulfillment.messages;
//response from agent or domain
if(response.result.source == 'agent'){
for(let i=0;i<apiaiMessages.length;i++){
let amessage=apiaiMessages[i];
let replymessage = null;
//response from apiai agent
//which type of response get
switch(amessage.type){
case 0 :
//0 is text message
console.log("ITS TEXT MESSAGE");
replymessage = TextMessage(event, amessage);
break;
case 1 :
//1 is card message
console.log("ITS CARD MESSAGE");
replymessage = CardMessage(event, amessage);
break;
case 2 :
//2 is quick reply
console.log("ITS QUICK REPLY");
replymessage = QuickReply(event, amessage);
break;
case 3 :
//3 is image mesaage
console.log("ITS IMAGE MESSAGE");
replymessage = ImageMessage(event, amessage);
break;
case 4 :
//4 is custom payload
console.log("ITS CUSTOM PAYLOAD");
console.log(amessage.payload);
replymessage = amessage.payload;
break;
}
sendFBmessage(event, replymessage);
}
}else{
//handling domain response
console.log("ITS DOMAIN SAYING");
replymessage = TextMessage(event, amessage);
sendFBmessage(event, replymessage);
}
});
//if apiai gets error
apiai.on('error', (error) => {
console.log(error);
});
//apiai end
apiai.end();
}
这是发送 FB 消息
function sendFBmessage(event, replymessage){
let sender = event.sender.id;
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: PAGE_ACCESS_TOKEN},
method: 'POST',
json:{
recipient: {id:sender},
message: replymessage
}
}, (error, response) => {
if(error){
console.log('Error : ', error);
}else if(response.body.error) {
console.log('Error : ', response.body.error);
}else{
console.log("Send Message Complete");
//return response.statusCode;
}
});
}
问题是它发送乱序(短信发送速度快,其他重内容发送速度慢,即使它开始得更早。)
所以我想做的是在成功发送第一条消息之前“使用发送操作”。
有什么想法吗?你可以帮帮我吗?
谢谢,