2

我正在尝试使用 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.ai的快速入门示例。

我面临的另一个问题是我想让hubot在一个私人频道中与每个用户一起收听,并让它响应每条不带前缀的消息。就像控制台中的节点示例一样。

高度赞赏帮助!

4

2 回答 2

1

好的,在摆弄了一段时间后,我完成了这项工作。这是我的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 做了一个可怕的破解来完成这项工作。我添加了以下功能,因为我无法使用可用的方法来使其正常工作。基本上回调和会话阻碍了我:

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 中有一个非常聪明的机器人,它理解自然语言并每天都在学习。

于 2016-04-15T11:44:56.343 回答
0

你可以直接使用这个模块,它似乎运行良好:https ://www.npmjs.com/package/hubot-wit

我刚刚完成了整合。您只需要提供环境变量WIT_TOKEN,它就可以很好地工作!

于 2016-11-24T15:17:43.777 回答