2016-04-14 13 views
2

Ich versuche, die neue wit.ai Bot Engine mit Javascript zu Hubot zu verbinden. Leider bin ich kein JS Entwickler, also kämpfe ich.wit.ai Bot Engines Stories verbunden mit hubot

Hier ist der Code, den ich habe:

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

Das Skript hört "hey botname" und gibt, was danach geschrieben wurde. Mein Problem ist, ich habe keine Ahnung, wie diese Eingabe an den Wit-Client gesendet wird. Die Verwendung dieses Skripts in bash ohne den hubot-Kram funktioniert gut, da dies auf dem Schnellstart-Beispiel von wit.ai basiert.

Das andere Problem, mit dem ich konfrontiert bin, ist, dass ich möchte Hubot hören in einem privaten Kanal mit jedem Benutzer und haben es auf jede Nachricht ohne Präfix reagieren. So wie es das Knotenbeispiel in der Konsole macht.

Hilfe ist sehr geschätzt!

Antwort

1

Ok, nach einer Weile habe ich diese Arbeit gemacht. Hier ist, was meine hubot Skript wie jetzt aussieht:

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

zusätzlich ich eine schreckliche Hack gemacht wit.js diese Arbeit zu bekommen. Ich habe die folgende Funktion hinzugefügt, da ich die verfügbaren Methoden nicht verwenden konnte, um das zu erreichen. Grundsätzlich Rückrufe und Sitzung hielten mich zurück:

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

Wenn jemand dies weiter nehmen würde und setzen es richtig, würde ich lieben das Ergebnis zu sehen. Dieser Hack funktioniert und wir haben jetzt einen wirklich schlauen Bot in unserem rocket.chat, der die natürliche Sprache versteht und jeden Tag lernt.

0

Sie direkt mit diesem Modul können, so scheint es gut zu funktionieren: https://www.npmjs.com/package/hubot-wit

Ich habe gerade jetzt fertig Integration. Sie müssen nur die Umgebungsvariable WIT_TOKEN bereitstellen und es funktioniert wunderbar!