2

I am trying to get the new wit.ai Bot Engine connected to hubot with Javascript. Unfortunately I am not a JS Developer so I'm struggling.

Here's the code I have:

'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);
    });
}

The script listens to "hey botname" and outputs what was written after this. My Problem is I have no clue how to get this input sent to the wit client. Using this script in bash without the hubot stuff works fine towards wit as this is based on the quick start example from wit.ai.

The other issue I'm facing is that I would like to have hubot listen in a private channel with every user and have it respond to every message without prefix. Just as the node example does in the console.

Help is highly apprechiated!

2 Answers2

1

Ok, after fiddling a while I made this work. Here's what my hubot script looks like now:

'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;
      });  
}

additionally I made an awful hack to wit.js to get this work. I added the following function as I was not able to use the available methods to get this working. Basically callbacks and session were holding me back:

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

If someone would take this further and implement it properly I would love to see the result. This hack works and we now have a really smart bot in our rocket.chat who understands natural language and learns every day.

0

You can directly use this module, it seems to be working well: https://www.npmjs.com/package/hubot-wit

I have just now finished integration. You just need to provide the environment variable WIT_TOKEN and it works beautifully!

Aswin Kumar
  • 5,158
  • 5
  • 34
  • 39