Botkit: bot.startConversation() returns No handler for conversationStarted

Created on 21 Nov 2016  路  9Comments  路  Source: howdyai/botkit

I have written a simple Facebook bot application using NodeJS and Express but every time I call bot.StartConversation() I get the following output in my server logs:
debug: No handler for conversationStarted

I looked over the code in CoreBot.js and it is triggering an event for conversationStarted.
this.activate = function() { this.task.trigger(conversationStarted, [this]); this.task.botkit.trigger(conversationStarted, [this.task.bot, this]); this.status = active; };

Is there a way to register a listener for conversationStarted which is not documented?

Facebook-related help wanted

All 9 comments

You can register an event for this, and it will stop logging.

controller.on('conversationStarted',function(bot, convo) { });

Or, you can set debug: false in your controller options, and you will stop seeing this type of error.

I tried registering the event but that doe not seem to work, seems like a "task" is trying to fire a conversationStarted event which does not exist. Botkit.trigger seems to be working, its not giving me an error in the console. I traced the error back to CoreBot.js.
this.activate = function() { this.task.trigger('conversationStarted', [this]); this.task.botkit.trigger('conversationStarted', [this.task.bot, this]); this.status = 'active'; };

The example app i am trying to run can be found here, https://github.com/mvaragnat/botkit-messenger-express-demo. Notice how the example app does not provide code for starting a conversation which leads me to believe the conversation functionality is not supported for Facebook Messenger. I went ahead and added a conversation method to the example app and thats when I start to see No handler for conversationStarted errors.

What you are seeing is a debug message informing you that, though a conversationStarted event fired, you did not provide a handler for it.

This happens for _all_ of the events that fire, just to show you what is happening.

You can turn off these messages by passing debug: false into your controller configuration. Or, you can safely ignore them.

After turning off debug mode I still can not get a conversation started. Can someone run the code below to see if it fails on startConversation?

Code :
`'use strict'

var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
var app = express()

var Botkit = require('botkit')

var token = "";

var controller = Botkit.facebookbot({
//debug: true,
access_token: token
})

var bot = controller.spawn({})

app.set('port', (process.env.PORT || 5000))

// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))

// Process application/json
app.use(bodyParser.json())

// for Facebook verification
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'supersecret') {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})

// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am a chat bot')
})

app.post('/webhook/', function (req, res) {
console.log(">> Somone knocking...");
handler(req.body);
res.sendStatus(200);
})

//-------
// this is triggered when a user clicks the send-to-messenger plugin
controller.on('facebook_optin', function (bot, message) {
bot.reply(message, 'Welcome, friend')
})

//pizzatime
controller.hears(['pizzatime'], 'message_received', function(bot,message) {
var askFlavor = function(err, convo) {
convo.ask('What flavor of pizza do you want?', function(response, convo) {
convo.say('Awesome.');
askSize(response, convo);
convo.next();
});
};
var askSize = function(response, convo) {
convo.ask('What size do you want?', function(response, convo) {
convo.say('Ok.')
askWhereDeliver(response, convo);
convo.next();
});
};
var askWhereDeliver = function(response, convo) {
convo.ask('So where do you want it delivered?', function(response, convo) {
convo.say('Ok! Good bye.');
convo.next();
});
};

bot.startConversation(message, askFlavor);

});

// user said hello
controller.hears(['hello'], 'message_received', function (bot, message) {
bot.reply(message, 'Hey there.');
})

// user says anything else
controller.hears('(.*)', 'message_received', function (bot, message) {
bot.reply(message, 'you said ' + message.match[1])
})

// this function processes the POST request to the webhook
var handler = function (obj) {
controller.debug('GOT A MESSAGE HOOK')
var message;

if (obj.entry) {
for (var e = 0; e < obj.entry.length; e++) {
for (var m = 0; m < obj.entry[e].messaging.length; m++) {
var facebook_message = obj.entry[e].messaging[m]

    console.log(facebook_message)

    // normal message
    if (facebook_message.message) {
      message = {
        text: facebook_message.message.text,
        user: facebook_message.sender.id,
        channel: facebook_message.sender.id,
        timestamp: facebook_message.timestamp,
        seq: facebook_message.message.seq,
        mid: facebook_message.message.mid,
        attachments: facebook_message.message.attachments
      }

      // save if user comes from m.me adress or Facebook search
      create_user_if_new(facebook_message.sender.id, facebook_message.timestamp)

      controller.receiveMessage(bot, message)
    }
    // clicks on a postback action in an attachment
    else if (facebook_message.postback) {
      // trigger BOTH a facebook_postback event
      // and a normal message received event.
      // this allows developers to receive postbacks as part of a conversation.
      message = {
        payload: facebook_message.postback.payload,
        user: facebook_message.sender.id,
        channel: facebook_message.sender.id,
        timestamp: facebook_message.timestamp
      }

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

      message = {
        text: facebook_message.postback.payload,
        user: facebook_message.sender.id,
        channel: facebook_message.sender.id,
        timestamp: facebook_message.timestamp
      }

      controller.receiveMessage(bot, message)
    }
    // When a user clicks on "Send to Messenger"
    else if (facebook_message.optin) {
      message = {
        optin: facebook_message.optin,
        user: facebook_message.sender.id,
        channel: facebook_message.sender.id,
        timestamp: facebook_message.timestamp
      }

        // save if user comes from "Send to Messenger"
      create_user_if_new(facebook_message.sender.id, facebook_message.timestamp)

      controller.trigger('facebook_optin', [bot, message])
    }
    // 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])
    }
    else {
      controller.log('Got an unexpected message from Facebook: ', facebook_message)
    }
  }
}

}
}

var create_user_if_new = function (id, ts) {
controller.storage.users.get(id, function (err, user) {
if (err) {
console.log(err)
}
else if (!user) {
controller.storage.users.save({id: id, created_at: ts}, function(){
console.log(">> Saved user!");
});
}
})
}
//-------

// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
`

I have the same issue with facebook not receiving the convo.say('some message'); within the bot.startConversation

example from docs

controller.hears('hello world', 'message_received', function(bot, message) {
  // start a conversation to handle this response.
  bot.startConversation(message, function(err, convo) {
    bot.reply(message, 'test');
    convo.say('Hello!');
    convo.say('Have a nice day!');
  });
});

Expected:
user: hello world
bot: test
bot: Hello!
bot: Have a nice day!

Actual:
user: hello world
bot: test

note: if I remove the bot.reply, then the bot does not send anything back to Messenger

I have the same issue:

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 not 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' }



Note: dependencies

"dependencies": {
    "botkit": "^0.4.10",
    "dotenv": "^4.0.0",
    "ejs": "*",
    "express": "3.2.6",
    "natural": "^0.4.0"
  },

I was able to fix my issue after digging into the code of Facebook.js, I am NOT calling setupWebserver, because I setup express in my code. This causes an issue because facebook_botkit.startTicking() is never called.

My fix is to call startTicking() before I call createWebhookEndpoints():

controller.webserver = app;

request.post(`https://graph.facebook.com/me/subscribed_apps?access_token=${FACEBOOK.pageAccessToken}`, (err, res, body) => {
  if (err) {
    console.error('Could not subscribe to page messages');
  } else {
    console.log('Successfully subscribed to Facebook events:', body);
    controller.startTicking();
  }
});
controller.createWebhookEndpoints(app, bot, () => {
  console.log('ONLINE!');
});

I am going to submit a PR to move the subscribed_apps call and the startTicking into createWebhookEndpoints

@smalltalk-josh could your solution help me to fix mine?

@mzaiady were you able to solve your issue?
@micmacys were you able to solve your issue?

Update: This solution solved my issue!

Solution (from the link above)

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]); 
Was this page helpful?
0 / 5 - 0 ratings

Related issues

HannanShaik picture HannanShaik  路  3Comments

koubenecn picture koubenecn  路  3Comments

fieldcorbett picture fieldcorbett  路  4Comments

seriousssam picture seriousssam  路  3Comments

GautierT picture GautierT  路  3Comments