Facebook Messenger BOT每分钟都收到相同的回复

时间:2016-09-11 03:48:42

标签: facebook bots facebook-messenger

我开发了一个Facebook messenger机器人,因为我想制作音乐机器人。

昨天,我每分钟都收到相同的回复。

我尝试多次更改代码,但我无法找出问题所在。

这是一个麻烦的样本。

sample

以下是代码:

(function(){
var sys = require ('sys'),
url = require('url'),
http = require('http'),
qs = require('querystring'),
request = require('request');

var token = '*******************';
var AddressManager = function(){
};

AddressManager.prototype = {
    endpoint:'http://itunes.apple.com/search?term=',

    parseArtist:function(freetext){
          var code;
          if(code = freetext.match(/(\S+)のおすすめは何ですか?/)){
              return code[1];
          }else{
              return [];
          }
    },
    getArtist:function(freetext, onSuccess, onError){
          var parsedArtist = this.parseArtist(freetext);
            if(!parsedArtist) {
                onError();
                return; 
            };

        http.get(this.endpoint+encodeURIComponent(parsedArtist)+'&country=jp&media=music&attribute=artistTerm&limit=1', function(res) {
            var body = '';
            res.setEncoding('utf8');
            res.on('data', function(chunk) {
                body += chunk;
            });
            res.on('end', function() {
                try{
                    ret = JSON.parse(body);
                    onSuccess(ret);
                } catch(ex){
                    onError();
                }
            });
        }).on('error', function(e) {
            onError();
        });
    }
};



http.createServer(function (req, res) {
    if (!req.url.match(/\/zip|\/\?hub\.mode/)){
        res.statusCode = 404;
        res.setHeader('Content-Type', 'text/plain');
        res.end('error');
        return;
    } else if (req.url.match(/\/\?hub\.mode/)){
        var param = url.parse(req.url,true);
        if (param.query['hub.verify_token'] === 'MUBOT') {
            res.end(param.query['hub.challenge']);
        } else {
            res.end('Error, wrong validation token');
        }
        return;
    }
    if(req.method=='POST') {
        var body='';

        req.on('data', function (data) {
            body +=data;
        });
        req.on('end',function(){

            qs.parse(body);


            sendResponse(JSON.parse(body), res);
        });
    }else if(req.method=='GET') {
        res.end('');
    }
}).listen(process.env.PORT || 5000);




function sendResponse(param, response){
    var manage = new AddressManager();
    var messaging_events = param.entry[0].messaging,
    replayMessages = [], text="", sender="";

//{
//  "object":"page",
//  "entry":[
//    {
//      "id":"PAGE_ID",
//      "time":1460245674269,
//      "messaging":[
//        {
//          "sender":{
//            "id":"USER_ID"
//          },
//          "recipient":{
//            "id":"PAGE_ID"
//          },
//          "timestamp":1460245672080,
//          "message":{
//            "mid":"mid.1460245671959:dad2ec9421b03d6f78",
//            "seq":216,
//            "text":"hello"
//          }
//        }
//      ]
//    }
//  ]
//}


    if (messaging_events.length > 0) {
        event = messaging_events[0];
        sender = event.sender.id;
        if (event.message && event.message.text) {
            text = event.message.text;
        }
    }

    console.log(text);


    if(text === "Hello"){
        var messageText = {
             text :"very good!"
        };

        request({
                url: 'https://graph.facebook.com/v2.6/me/messages',
                qs: {access_token:token},
                method: 'POST',
                json: {
                    recipient: {id:sender},
                    message: messageText
                }
            }, function(error, response, body) {
                if (error) {
                    console.log('Error sending message: ', error);
                } else if (response.body.error) {
                    console.log('Error: ', response.body.error);
                }
            });
    }else if(text === "music"){

        var messagePost = {
             attachment: {
                type: "template",
                payload: {
                  template_type: "generic",
                  elements: [{
                    title: "rift",
                    subtitle: "Next-generation virtual reality",
                    item_url: "https://www.oculus.com/en-us/rift/",               
                    image_url: "http://messengerdemo.parseapp.com/img/rift.png",
                    buttons: [{
                      type: "web_url",
                      url: "https://www.oculus.com/en-us/rift/",
                      title: "Open Web URL"
                    }, {
                      type: "postback",
                      title: "Call Postback",
                      payload: "Payload for first bubble",
                    }],
                  }, {
                    title: "touch",
                    subtitle: "Your Hands, Now in VR",
                    item_url: "https://www.oculus.com/en-us/touch/",               
                    image_url: "http://messengerdemo.parseapp.com/img/touch.png",
                    buttons: [{
                      type: "web_url",
                      url: "https://www.oculus.com/en-us/touch/",
                      title: "Open Web URL"
                    }, {
                      type: "postback",
                      title: "Call Postback",
                      payload: "Payload for second bubble",
                    }]
                  }]
                }
              }
        };

        request({
                url: 'https://graph.facebook.com/v2.6/me/messages',
                qs: {access_token:token},
                method: 'POST',
                json: {
                    recipient: {id:sender},
                    message: messagePost
                }
            }, function(error, response, body) {
                if (error) {
                    console.log('Error sending message: ', error);
                } else if (response.body.error) {
                    console.log('Error: ', response.body.error);
                }
            });
    }else{
        manage.getArtist(text, function(result){

            var messageData = {
                attachment: {
                    type: "audio",
                    payload: {
                        url: "http://www.hmix.net/music/n/n114.mp3"
                    }
                }
            };

            request({
                url: 'https://graph.facebook.com/v2.6/me/messages',
                qs: {access_token:token},
                method: 'POST',
                json: {
                    recipient: {id:sender},
                    message: messageData
                }
            }, function(error, response, body) {
                if (error) {
                    console.log('Error sending message: ', error);
                } else if (response.body.error) {
                    console.log('Error: ', response.body.error);
                }
            });
        }, function(){
            response.end('error');
        });
    }
}  }());

请帮我解决问题。

2 个答案:

答案 0 :(得分:0)

正如@CBroe指出的那样,如果邮件被正确收到,Facebook将会期待200回复​​。

以下是FB示例的摘录。请注意最后的res.sendStatus(200);

app.post('/webhook', function (req, res) {
  var data = req.body;

  // Make sure this is a page subscription
  if (data.object == 'page') {
    // Iterate over each entry
    // There may be multiple if batched
    data.entry.forEach(function(pageEntry) {
      var pageID = pageEntry.id;
      var timeOfEvent = pageEntry.time;

      // Iterate over each messaging event
      pageEntry.messaging.forEach(function(messagingEvent) {
        if (messagingEvent.optin) {
          receivedAuthentication(messagingEvent);
        } else if (messagingEvent.message) {
          receivedMessage(messagingEvent);
        } else if (messagingEvent.delivery) {
          receivedDeliveryConfirmation(messagingEvent);
        } else if (messagingEvent.postback) {
          receivedPostback(messagingEvent);
        } else if (messagingEvent.read) {
          receivedMessageRead(messagingEvent);
        } else if (messagingEvent.account_linking) {
          receivedAccountLink(messagingEvent);
        } else {
          console.log("Webhook received unknown messagingEvent: ", messagingEvent);
        }
      });
    });

    // Assume all went well.
    //
    // You must send back a 200, within 20 seconds, to let us know you've 
    // successfully received the callback. Otherwise, the request will time out.
    res.sendStatus(200);
  }
});

看起来你可以做同样的事情

if(req.method=='POST') {
    var body='';

    req.on('data', function (data) {
        body +=data;
    });
    req.on('end',function(){

        qs.parse(body);


        sendResponse(JSON.parse(body), res);
        res.sendStatus(200);
    });     

您可以在DMS Software Bot查看功能正常的示例。源代码位于FB-Robot。有关部署Bot的详细信息,请参阅我的文章Facebook Robots for Fun and Profit

答案 1 :(得分:0)

if(req.method=='POST') {
        var body='';

        req.on('data', function (data) {
            body +=data;
        });
        req.on('end',function(){

            qs.parse(body);

            sendResponse(JSON.parse(body), res);
            →→res.statusCode = 200;
            →→res.end('');
        });

我添加了两行代码。我已经解决了我的问题。