与DialogFlow的Botkit控制台聊天集成无法正常工作

时间:2018-12-17 14:02:50

标签: dialogflow botkit

我正在使用控制台机器人(这是Botkit的console_bot.js示例)。在DialogFlow控制台上安装代理程序,下载带有私钥的子文件,下载botkit-middleware-dialogflow。

这是实际的代码:

var Botkit = require('./lib/Botkit.js');
var os = require('os');


var controller = Botkit.consolebot({
    debug: true,
});


const dialogflowMiddleware = require('botkit-middleware-dialogflow')({
    keyFilename: './botkit-test-file.json',  // service account private key file from Google Cloud Console
  });

//******************* */

var bot = controller.spawn();
controller.middleware.receive.use(dialogflowMiddleware.receive);


controller.middleware.send.use(function(bot, message, next) {


  if (message.intent == 'hey') {
      message.text = 'Hello from DialogFlow!!!';
  }
  console.log('SENDING ', message.text,'TO USER', message.text);
  next();

});
//lifecycle convo events
controller.on('conversationStarted', function(bot, convo) {
    console.log('----------------> A conversation started with ', convo.context.user);
  });
  controller.on('conversationEnded', function(bot, convo) {
    console.log('<----------------- A conversation ended with ', convo.context.user);
  });
  controller.hears(['Default Welcome Intent'], 'direct_message', dialogflowMiddleware.hears, function(bot, message) {
    var replyText = message.fulfillment.text;  // message object has new fields added by Dialogflow
    bot.reply(message, replyText);
  });

在打开调试模式后,我在控制台中看到了以下行:

debug: Setting up a handler for conversationStarted
debug: Setting up a handler for conversationEnded
debug: Setting up a handler for direct_message
hey
debug: RECEIVED MESSAGE
debug: CUSTOM FIND CONVO user text

这是问题所在:当我键入应触发DF意向的语音时(例如,“嘿”),机器人将不会回复任何内容

1 个答案:

答案 0 :(得分:0)

修复它,它是消息的类型,message_received而不是直接消息。 因此,这是更正:

// USE MESSAGE_RECEIVED, NOT DIRECT MESSAGE
     controller.hears(['Default Welcome Intent'], 'message_received', dialogflowMiddleware.hears, function(bot, message) {
        var replyText = message.fulfillment.text;  
        bot.reply(message, replyText);
      });