我一直在使用 Andrew Templeton 的“Bottie”代码 ( https://github.com/andrew-templeton/bottie ) 来制作 NLP 驱动的机器人。
原始代码是为与 Slack 一起工作而构建的,但我希望将其改为适用于 Discord 客户端。
我已经取得了一些进展,但卡在“ears.js”文件的“.hear”函数部分(如下面的代码片段所示,这是接受消息并将其发送到“NLP 引擎”的主文件')。如果向其发送“ping”,该机器人会做出响应,但不会发生其他任何事情(该机器人是用来讲笑话、进行对话等的)。这是我的代码:
//This is the 'ears.js' file. This allows the bot to log in and 'hear' messages.
"use strict";
var Discord = require('discord.js');
var BotKit = require('botkit');
module.exports = Ears;
var Bot = new Discord.Client({
//var Bot = BotKit.slackbot({
debug: false,
storage: undefined
});
function Ears(token) {
this.scopes = [
'direct_mention',
'direct_message',
'mention',
'message'
];
// if we haven't defined a token, get the token from the session variable.
if (Bot.token == undefined) {
this.token = '...insert token here...';
}
}
Ears.prototype.listen = function() {
console.log('TOKEN: ' + this.token);
this.bot = Bot.login(this.token);
Bot.on('message', (message) => {
if(message.content == 'ping') {
message.channel.sendMessage('pong');
}
});
return this;
}
Ears.prototype.hear = function(pattern, callback) {
Bot.hears(pattern, this.scopes, callback);
return this;
};
下面是主程序“index.js”文件的代码:
//This is the 'index.js' file - the main program.
"use strict";
var fs = require('fs');
var Train = require('./src/train');
var Brain = require('./src/brain');
var Ears = require('./src/ears');
var builtinPhrases = require('./builtins');
var Bottie = {
Brain: new Brain(),
Ears: new Ears(process.env.SLACK_TOKEN)
};
var customPhrasesText;
var customPhrases;
try {
customPhrasesText = fs.readFileSync(__dirname + '/custom-phrases.json').toString();
} catch (err) {
throw new Error('Uh oh, Bottie could not find the ' +
'custom-phrases.json file, did you move it?');
}
try {
customPhrases = JSON.parse(customPhrasesText);
} catch (err) {
throw new Error('Uh oh, custom-phrases.json was ' +
'not valid JSON! Fix it, please? :)');
}
console.log('Bottie is learning...');
Bottie.Teach = Bottie.Brain.teach.bind(Bottie.Brain);
eachKey(customPhrases, Bottie.Teach);
eachKey(builtinPhrases, Bottie.Teach);
Bottie.Brain.think();
console.log('Bottie finished learning, time to listen...');
Bottie.Ears
.listen()
.hear('TRAINING TIME!!!', function(speech, message) {
console.log('Delegating to on-the-fly training module...');
Train(Bottie.Brain, speech, message);
})
.hear('.*', function(speech, message) {
var interpretation = Bottie.Brain.interpret(message.text);
console.log('Bottie heard: ' + message.text);
console.log('Bottie interpretation: ', interpretation);
if (interpretation.guess) {
console.log('Invoking skill: ' + interpretation.guess);
Bottie.Brain.invoke(interpretation.guess, interpretation, speech, message);
} else {
speech.reply(message, 'Hmm... I don\'t have a response what you said... I\'ll save it and try to learn about it later.');
// speech.reply(message, '```\n' + JSON.stringify(interpretation) + '\n```');
// append.write [message.text] ---> to a file
fs.appendFile('phrase-errors.txt', '\nChannel: ' + message.channel + ' User:'+ message.user + ' - ' + message.text, function (err) {
console.log('\n\tBrain Err: Appending phrase for review\n\t\t' + message.text + '\n');
});
}
});
function eachKey(object, callback) {
Object.keys(object).forEach(function(key) {
callback(key, object[key]);
});
}
不幸的是,“Bottie”的作者没有回复查询,因此我将问题在这里发布给社区的其他人。我对 JavaScript 相当陌生,希望能对此提供任何帮助。