0

我是聊天机器人开发的新手,我需要一些帮助。虽然将 botkit 与 facebook messenger 和 wit.ai 连接起来以使用 NLP 似乎很简单。我没能做到。我最初的目标是进行像 hello-hello 这样的简单对话,但使用 wit.ai 作为中间件。下面我附上代码。它应该做的是接收“你好”消息,将其传递给 wit.ai,然后回复“我听到你好!” 作为答复(在此阶段不使用机智)。相反,我只是收到

调试:收到消息调试:自定义查找 XXXXXXXXXXXXXX XXXXXXXXXXXXXXX 调试:没有 message_received 的处理程序

在我发送到 facebook messenger bot 的每条消息之后。机智地看来,我收到了消息,因为我在收件箱中收到消息以更新意图。如果有任何代码比下面的代码简单得多,我会很高兴拥有它,这样我就可以从更简单的东西开始:)。谢谢

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

1 回答 1

0

确保您事先在 Wit.ai 中进行了一些对话,例如hello there并将该语句中的hello突出显示为类似, greetings

现在我不确定你在 wit.ai 中的意图是什么,但在你的陈述中controller.hears(['hello']你实际上是在听 wit.ai 的意图。所以在我上面提到的例子中,我们会使用hears(['greetings']),因为这是 wit.ai 的意图。

此外,而不是使用direct_messageusemessage_received这应该是这样的:

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

如果您正在努力追查问题,您可以在控制器中粘贴一个控制台语句,例如console.log("Wit.ai detected entities", message.entities);,看看您能从中得到什么。

如果您还有任何问题,请告诉我:)

于 2017-01-23T21:28:56.070 回答