Search code examples
facebookbotsfacebook-messenger

Facebook Messenger BOT with trouble receiving the same responses every minute


I have developed a Facebook messenger bot because I want to make a music bot.

Yesterday, I had trouble receiving the same responses every minute.

I've tried changing the code many times, but I can't find out what is at fault.

Here is a trouble sample.

sample

Here is the code:

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

Please help me solve the problem.


Solution

  • As @CBroe pointed out, Facebook will expect a 200 response if the message was received properly.

    Here is a snippet from the FB sample. Note the res.sendStatus(200); at the end.

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

    It looks like you can do that same thing

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

    You can see a functioning example at the DMS Software Bot. The source code is at FB-Robot. For details on deploying a Bot, see my article Facebook Robots for Fun and Profit.