2016-04-14 3 views
2

Yeni wit.ai Bot Engine'i Javascript ile birlikte hubbona bağlamaya çalışıyorum. Ne yazık ki ben bir JS Developer değilim, bu yüzden ben mücadele ediyorum.wit.ai Bot Engine Öyküler hububatta bağlı

İşte ben kod:

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

komut dinler için "botname hey" ve bundan sonra yazılanları çıkarır. Benim Sorunum, bu girdinin inisiyatifine nasıl gönderileceğine dair bir fikrim yok. Bu betiğin, bash içinde, kibritsiz bir şekilde, wit.ai.'den hızlı başlangıç ​​örneğine dayanarak, zekâya karşı gayet iyi çalışıyor.

Karşılaştığım diğer bir konu da, her kullanıcı ile özel bir kanalda kibrit dinlemesini ve önek olmadan her mesaja yanıt vermesini istiyorum. Düğüm örneğinde olduğu gibi konsolda.

Yardım çok beğenildi!

cevap

1

Tamam, bir süre uğraştıktan sonra bu çalışmayı yaptım. İşte benim hubot komut şimdi gibi görünüyor:

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

ayrıca ben bu işi almak için wit.js için korkunç bir hack yaptı. Bu çalışmayı elde etmek için mevcut yöntemleri kullanamadığım için aşağıdaki işlevi ekledim. Temelde geri aramaları ve oturum Beni engelliyorsun edildi: Birisi bu ileri götürmek ve uygun biçimde uygulanması istiyorsanız

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

Ben sonucunu görmek isteriz. Bu hack işe yarıyor ve şu anda roketlerimizde doğal bir dili anlayan ve her gün öğrenen gerçekten akıllı bir botumuz var.

0

doğrudan bu modülü kullanabilirsiniz, iyi çalışıyor gibi görünüyor: https://www.npmjs.com/package/hubot-wit

sadece şimdi bitirdim entegrasyon. Sadece WIT_TOKEN ortam değişkenini sağlamanız gerekiyor ve güzel çalışıyor!