我正在尝试使用 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在一个私人频道中与每个用户一起收听,并让它响应每条不带前缀的消息。就像控制台中的节点示例一样。
高度赞赏帮助!