Botkit: Conversation not working! On require_delivery: true

Created on 6 Feb 2017  路  10Comments  路  Source: howdyai/botkit

Trying the simple example of conversation, but no reply !!

controller.hears(['simple co'], 'message_received', function (bot, message) {
      // start a conversation to handle this response.
    // start a conversation to handle this response.
      bot.startConversation(message,function(err,convo) {

         convo.ask('How are you?',function(response,convo) {
          convo.say('Cool, you said: ' + response.text);
          convo.next();

        });
      });
});

user: simple co
bot: How are you?
user: fine thank you

bot stop replying after that!!! and what ever i write no reply!

On the logs

debug: ---------------RECIEVED FACEBOOK MESSAGE----------------
debug: {"sender":{"id":"1486361863543"},"recipient":{"id":"1486361863286"},"timestamp":1486379776981,"message":{"mid":"mid.1486379776981:d3e0d6a735","seq":2541,"text":"fine thank you"}}
debug: ---------------------------------------------------------
debug: RECEIVED MESSAGE
debug: CUSTOM FIND CONVO 1486361863543 1486361863543
debug: FOUND EXISTING CONVO!
debug: HANDLING MESSAGE IN CONVO { text: 'fine thank you',
  user: '1486361863543',
  channel: '1486361863543',
  timestamp: 1486379776981,
  seq: 2541,
  is_echo: undefined,
  mid: 'mid.1486379776981:d3e0d6a735',
  sticker_id: undefined,
  attachments: undefined,
  quick_reply: undefined,
  type: 'user_message' }
POST /webhook 200 5ms - 2b
debug: Message received from FB WeebHOOK { object: 'page',
  entry: 
   [ { id: '1486361863286',
       time: 1486379777070,
       messaging: [Object] } ] }
debug: ---------------RECIEVED FACEBOOK MESSAGE----------------
debug: {"sender":{"id":"1486361863543"},"recipient":{"id":"1486361863286"},"timestamp":1486379777068,"read":{"watermark":1486379767813,"seq":0}}
debug: ---------------------------------------------------------
POST /webhook 200 2ms - 2b
debug: Message received from FB WeebHOOK { object: 'page',
  entry: 
   [ { id: '1486361863286',
       time: 1486380594460,
       messaging: [Object] } ] }
debug: ---------------RECIEVED FACEBOOK MESSAGE----------------
debug: {"sender":{"id":"1486361863543"},"recipient":{"id":"1486361863286"},"timestamp":1486380594366,"message":{"mid":"mid.1486380594366:000b583f75","seq":2544,"text":"any thing"}}
debug: ---------------------------------------------------------
debug: RECEIVED MESSAGE
debug: CUSTOM FIND CONVO 1486361863543 1486361863543
debug: FOUND EXISTING CONVO!
debug: HANDLING MESSAGE IN CONVO { text: 'any thing',
  user: '1486361863543',
  channel: '1486361863543',
  timestamp: 1486380594366,
  seq: 2544,
  is_echo: undefined,
  mid: 'mid.1486380594366:000b583f75',
  sticker_id: undefined,
  attachments: undefined,
  quick_reply: undefined,
  type: 'user_message' }



dependencies

"dependencies": {
    "botkit": "^0.4.10",
    "dotenv": "^4.0.0",
    "ejs": "*",
    "express": "3.2.6",
    "natural": "^0.4.0"
  },
"engines": {
    "node": "6.9.4"
  }
Facebook-related bug

Most helpful comment

Solved (please fix Facebook/ express example code)

The issue with the Facebook/express examples they do not provide the data required for `require_delivery: true' while creating the handler:

 // message delivered callback
        else if (facebook_message.delivery) {
          message = {
            optin: facebook_message.delivery,
            user: facebook_message.sender.id,
            channel: facebook_message.sender.id,
            timestamp: facebook_message.timestamp
          }

          controller.trigger('message_delivered', [bot, message])

But In Facebook.js:

    if (facebook_botkit.config.require_delivery) {

        facebook_botkit.on('message_delivered', function(bot, message) {

            // get list of mids in this message
            for (var m = 0; m < message.delivery.mids.length; m++) {
                var mid = message.delivery.mids[m];

                // loop through all active conversations this bot is having
                // and mark messages in conversations as delivered = true
                bot.findConversation(message, function(convo) {
                    if (convo) {
                        for (var s = 0; s < convo.sent.length; s++) {
                            if (convo.sent[s].sent_timestamp <= message.delivery.watermark ||
                                (convo.sent[s].api_response && convo.sent[s].api_response.mid == mid)) {
                                convo.sent[s].delivered = true;
                            }
                        }
                    }
                });
            }

        });

    }

so, to solve the issue , handler creation should be updated to something like the following:

                else if (facebook_message.delivery) {
                    var message = {
                            delivery: facebook_message.delivery,
                            user: facebook_message.sender.id,
                            channel: facebook_message.sender.id,
                            timestamp: facebook_message.timestamp

                    }


                    controller.trigger('message_delivered', [bot, message]); 

and now it works fine for me!

All 10 comments

Note:
issue occur if

require_delivery: true

Solved (please fix Facebook/ express example code)

The issue with the Facebook/express examples they do not provide the data required for `require_delivery: true' while creating the handler:

 // message delivered callback
        else if (facebook_message.delivery) {
          message = {
            optin: facebook_message.delivery,
            user: facebook_message.sender.id,
            channel: facebook_message.sender.id,
            timestamp: facebook_message.timestamp
          }

          controller.trigger('message_delivered', [bot, message])

But In Facebook.js:

    if (facebook_botkit.config.require_delivery) {

        facebook_botkit.on('message_delivered', function(bot, message) {

            // get list of mids in this message
            for (var m = 0; m < message.delivery.mids.length; m++) {
                var mid = message.delivery.mids[m];

                // loop through all active conversations this bot is having
                // and mark messages in conversations as delivered = true
                bot.findConversation(message, function(convo) {
                    if (convo) {
                        for (var s = 0; s < convo.sent.length; s++) {
                            if (convo.sent[s].sent_timestamp <= message.delivery.watermark ||
                                (convo.sent[s].api_response && convo.sent[s].api_response.mid == mid)) {
                                convo.sent[s].delivered = true;
                            }
                        }
                    }
                });
            }

        });

    }

so, to solve the issue , handler creation should be updated to something like the following:

                else if (facebook_message.delivery) {
                    var message = {
                            delivery: facebook_message.delivery,
                            user: facebook_message.sender.id,
                            channel: facebook_message.sender.id,
                            timestamp: facebook_message.timestamp

                    }


                    controller.trigger('message_delivered', [bot, message]); 

and now it works fine for me!

Would you mind submitting a PR to update the example with the fix you found?

I will try to find some time to do it.

@mzaiady I am going to take a pass that this fix; if you have already started working on it, let me know

Could someone please submit a PR with this fix?

Hello, I run into this problem today, and found this issue.
I was not sure where to fix so I am simply setting "require_delivery: false" at this moment.
Can somebody recommend the fix (or tell what the PR would be?)

const controller = Botkit.facebookbot({
    debug: true,
    receive_via_postback: false,
    require_delivery: false,

I have require_delivery=true and I am sending a second (third...) reply on('message_delivered', my problem is that it is not always firing. Most of the times the event fires and I get my expected output. but sometimes it doesn't fire. Am I missing something? Is this an issue with botkit?

closing in favor of the most recently linked ticket

I know this is closed, but for the record @mzaiady solution is not the issue, but it is on the right track

I spent about 5 hours working on this yesterday, the following code is fired by a facebook event.

facebook_botkit.on('message_delivered', function(bot, message) {

I have a PR #1213 to address this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matueranet picture matueranet  路  4Comments

iworkforthem picture iworkforthem  路  3Comments

imjul1an picture imjul1an  路  3Comments

liornaar picture liornaar  路  3Comments

GautierT picture GautierT  路  3Comments