nodejs facebook与apiai聊天机器人

时间:2017-02-07 21:46:13

标签: node.js facebook-javascript-sdk chatbot facebook-chatbot api-ai

我有一个关于在Facebook处理富信息的问题。

当我尝试使用api.ai发送消息时,

我发送的信息如enter link description here

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

事情就是它发出乱序(快速发送短信,其他重要内容发送缓慢,甚至更早开始。)

所以我想做的是&#34;使用发送动作&#34;在第一封邮件成功发送之前。

有什么想法吗?你能救我吗?

感谢,

0 个答案:

没有答案