首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >连接到hubot的wit.ai机器人引擎故事

连接到hubot的wit.ai机器人引擎故事
EN

Stack Overflow用户
提问于 2016-04-14 19:14:13
回答 2查看 1K关注 0票数 2

我正在尝试获得新的wit.ai机器人引擎连接到机器人与Javascript。不幸的是,我不是JS开发人员,所以我在苦苦挣扎。

下面是我的代码:

代码语言:javascript
复制
'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);
    });
}

脚本监听“嘿botname”,并输出在此之后编写的内容。我的问题是,我不知道如何将此输入发送到wit客户端。在bash中使用这个脚本而不使用hubot的东西非常适合wit,因为这是基于wit.ai的快速入门示例。

我面临的另一个问题是,我想让hubot在私有通道上监听每个用户,并让它响应每条没有前缀的消息。就像控制台中的节点示例一样。

帮助是非常重要的!

EN

回答 2

Stack Overflow用户

发布于 2016-04-15 19:44:56

好的,经过一段时间的摆弄,我做到了这一点。下面是我的hubot脚本现在的样子:

代码语言:javascript
复制
'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进行了一次可怕的攻击,以获得这项工作。我添加了以下函数,因为我不能使用可用的方法来使其工作。基本上,回调和会话阻碍了我:

代码语言:javascript
复制
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
      );
  }

如果有人能更进一步并正确地实现它,我很乐意看到结果。这个方法起作用了,现在我们的rocket.chat里有了一个非常聪明的机器人,它能理解自然语言,每天都在学习。

票数 1
EN

Stack Overflow用户

发布于 2016-11-24 23:17:44

你可以直接使用这个模块,它看起来工作得很好:https://www.npmjs.com/package/hubot-wit

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36621406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档