javascript - wit.ai Bot Engine 连接到 hubot 的故事

标签 javascript bots hubot wit.ai

我正在尝试使用 Javascript 将新的 wit.ai Bot Engine 连接到 hubot。 不幸的是,我不是 JS 开发人员,所以我很挣扎。

这是我的代码:

'use strict';
const Wit = require('../../../node-wit').Wit;

const firstEntityValue = (entities, entity) => {
  const val = entities && entities[entity] &&
    Array.isArray(entities[entity]) &&
    entities[entity].length > 0 &&
    entities[entity][0].value
  ;
  if (!val) {
    return null;
  }
  return typeof val === 'object' ? val.value : val;
};


const actions = {
  say: (sessionId, msg, cb) => {
    console.log(msg);
    cb();
  },
  merge: (context, entities, cb) => {
    const loc = firstEntityValue(entities, "location");
    if (loc) context.loc = loc;
    cb(context);
  },
  error: (sessionId, msg) => {
    console.log('Oops, I don\'t know what to do.');  
  },
    'fetch-weather': (context, cb) => {
    // Here should go the api call, e.g.:
    // context.forecast = apiCall(context.loc)
    context.forecast = 'sunny';
    cb(context);
  },
};

const client = new Wit('MY_TOKEN_HERE', actions);
client.interactive();



module.exports = function(robot) {

   robot.respond(/hey\s+(.+$)/i, function(msg){

        var match = msg.match[1];    
        msg.send("I've heared: " + match);

        console.log(match)
        process.stdout.write(match);
    });
}

该脚本监听“hey botname”并输出在此之后写入的内容。我的问题是我不知道如何将此输入发送到机智客户端。在没有 hubot 的 bash 中使用这个脚本对 wit 很有效,因为这是基于 wit.ai 的快速入门示例。

我面临的另一个问题是,我想让 hubot 在每个用户的私有(private) channel 中收听,并让它在没有前缀的情况下响应每条消息。就像控制台中的节点示例一样。

非常感谢您的帮助!

最佳答案

好的,经过一段时间的摆弄,我完成了这项工作。 这是我的 hubot 脚本现在的样子:

'use strict';
const Wit = require('../../../node-wit').Wit;

var room;

const firstEntityValue = (entities, entity) => {
  const val = entities && entities[entity] &&
    Array.isArray(entities[entity]) &&
    entities[entity].length > 0 &&
    entities[entity][0].value
  ;
  if (!val) {
    return null;
  }
  return typeof val === 'object' ? val.value : val;
};


const actions = {
  say: (sessionId, msg, cb) => {
    console.log(msg);
    room.send(msg)
    cb();
  },
  merge: (context, entities, cb) => {
    const loc = firstEntityValue(entities, "location");
    if (loc) context.loc = loc;
    cb(context);
  },
  error: (sessionId, msg) => {
    console.log('Oops, I don\'t know what to do.');    
    room.send('Oops, I don\'t know what to do.')
  },
};

const client = new Wit('MY_TOKEN_HERE', actions);
//client.interactive();


module.exports = function(robot) {

  robot.listen(function(msg) {        

        var userID = msg.user.id;
        var roomID = msg.user.room;

        // is this a direct chat(private room)?
        if(roomID.indexOf(userID) >= 0){
          if(typeof msg.text == "string"){
              client.pxMessage(msg.text);
          }          
        }       

        return true;
      }, 
      function(response){

          // save room for replys
          room = response;
      });  
}

此外,我对 wit.js 进行了一次糟糕的破解以完成这项工作。我添加了以下功能,因为我无法使用可用的方法来使其正常工作。基本上回调和 session 阻碍了我:

this.pxMessage = (message) => {
      const sessionId = uuid.v1();
      const context = {};
      const steps = 5;

      this.runActions(
        sessionId,
        message,
        context,
        (error, context) => {
          if (error) {
            l.error(error);
          }
          rl.prompt();
        },
        steps
      );
  }

如果有人愿意更进一步并正确实现它,我很乐意看到结果。这个 hack 很有效,现在我们的 rocket.chat 中有一个非常聪明的机器人,它理解自然语言并且每天都在学习。

关于javascript - wit.ai Bot Engine 连接到 hubot 的故事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36621406/

相关文章:

javascript - 函数中的递归调用如何工作?

javascript - 为什么onclick的处理函数没有按预期工作?

google-analytics - 如何在Google Analytics(分析)中分割和过滤机器人流量?

c# - Microsoft Teams 中的 ActionTypes.MessageBack 有问题吗?

javascript - Coffeescript 正则表达式未按预期匹配

javascript - 谁或哪个线程拥有 Javascript 事件循环?

javascript - 从不同域动态同步加载 JavaScript 文件

java - 在 Cookie Clicker (Selenium) 上查找鼠标悬停工具提示的 xpath 时遇到问题

cron - 您如何在 Slack 的这个 hubot cron 脚本中为 'mention' 用户设置通知,以便他们在通知设置打开时将其视为通知?

node.js - 无法在 Heroku 上部署 Hubot