0

I am a novice in chatbot development and I would like some help. While it seems quite simple to connect botkit with facebook messenger and wit.ai in orger to use NLP. I haven't managed to do so. My initial goal is to have a simple conversation like hello-hello but using wit.ai as middleware. Below I attach the code. What it should do is receive a "hello" message, pass it to wit.ai and then respond "I heard hello!" as a reply (without using wit at this stage). Instead I just receive

debug: RECEIVED MESSAGE debug: CUSTOM FIND CONVO XXXXXXXXXXXXXX XXXXXXXXXXXXXX debug: No handler for message_received

after every message I send to facebook messenger bot. In wit it seems like I am getting the messages since I receive messages in my inbox to update the intents. If there is any code much simpler than the one below I would be very happy to have it so that I can start with something much simpler :). Thanks

<pre><code>
if (!process.env.page_token) {
console.log('Error: Specify page_token in environment');
process.exit(1);
}

if (!process.env.page_token) {
console.log('Error: Specify page_token in environment');
process.exit(1);
}

if (!process.env.verify_token) {
console.log('Error: Specify verify_token in environment');
process.exit(1);
}

if (!process.env.app_secret) {
console.log('Error: Specify app_secret in environment');
process.exit(1);
}

var Botkit = require('./lib/Botkit.js');
var wit = require('./node_modules/botkit-middleware-witai')({
token: process.env.wit,
minConfidence: 0.6,
logLevel: 'debug'
});
var os = require('os');
var commandLineArgs = require('command-line-args');
var localtunnel = require('localtunnel');

const ops = commandLineArgs([
  {name: 'lt', alias: 'l', args: 1, description: 'Use localtunnel.me to make your bot available on the web.',
  type: Boolean, defaultValue: false},
  {name: 'ltsubdomain', alias: 's', args: 1,
  description: 'Custom subdomain for the localtunnel.me URL. This option can only be used together with --lt.',
  type: String, defaultValue: null},
]);

if(ops.lt === false && ops.ltsubdomain !== null) {
console.log("error: --ltsubdomain can only be used together with --lt.");
process.exit();
}

var controller = Botkit.facebookbot({
debug: true,
log: true,
access_token: process.env.page_token,
verify_token: process.env.verify_token,
app_secret: process.env.app_secret,
validate_requests: true, // Refuse any requests that don't come from FB on your receive webhook, must provide FB_APP_SECRET in environment variables
});

var bot = controller.spawn({
});

controller.setupWebserver(process.env.port || 3000, function(err, webserver) {
controller.createWebhookEndpoints(webserver, bot, function() {
    console.log('ONLINE!');
    if(ops.lt) {
        var tunnel = localtunnel(process.env.port || 3000, {subdomain: ops.ltsubdomain}, function(err, tunnel) {
            if (err) {
                console.log(err);
                process.exit();
            }
            console.log("Your bot is available on the web at the following URL: " + tunnel.url + '/facebook/receive');
        });

        tunnel.on('close', function() {
            console.log("Your bot is no longer available on the web at the localtunnnel.me URL.");
            process.exit();
        });
    }
});
});

controller.middleware.receive.use(wit.receive);

controller.hears(['hello'], 'direct_message', wit.hears, function(bot, message) {
   bot.reply(message, 'I heard hello!');
 });

function formatUptime(uptime) {
var unit = 'second';
if (uptime > 60) {
    uptime = uptime / 60;
    unit = 'minute';
}
if (uptime > 60) {
    uptime = uptime / 60;
    unit = 'hour';
}
if (uptime != 1) {
    unit = unit + 's';
}

uptime = uptime + ' ' + unit;
return uptime;
}

1 Answers1

0

Make sure you have a few conversations in Wit.ai beforehand so for example hello there and highlight the hello in that statement as something like, greetings.

Now i'm not sure what your intents are called in wit.ai but in your statement controller.hears(['hello'] you're actually listening to the wit.ai intents. So in the example i mentioned above, we'd be using hears(['greetings']) since that's the intent in wit.ai.

Also, instead of using direct_message use message_received this is what it should look like:

controller.hears(['hello'], 'message_received', wit.hears, function(bot, message) {
   bot.reply(message, 'I heard hello!');
 });

If you're struggling tracking down the problem you can stick a console statement in your controller so something like console.log("Wit.ai detected entities", message.entities); and see what you get back from that.

Let me know if you're still having any issues :)

pourmesomecode
  • 4,108
  • 10
  • 46
  • 87