我使用 Botkit 和 wit.ai 创建了一个机器人。它运行得很好。但是,经过一段时间后,我开始收到来自机器人的多条消息。在我重新启动 node.js 服务后,它开始正常工作。
这是我写的代码,
const _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}
const controller = Botkit.slackbot({
json_file_store: './db_slackbutton_bot/',
logger: new winston.Logger({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './bot.log' })
]
}),
debug: true
})
.configureSlackApp({
clientId: config.slack.clientId,
clientSecret: config.slack.clientSecret,
scopes: ['bot'],
});
// Handle events related to the websocket connection to Slack
controller.on('rtm_open',function(bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close',function(bot) {
// you may want to attempt to re-open
console.log('** The RTM api just closed');
bot.startRTM(function(err) {
if (!err) {
trackBot(bot);
}
});
});
/*
code for starting RTM
*/
controller.storage.teams.all(function(err,teams) {
if (err) {
throw new Error(err);
}
// connect all teams with bots up to slack!
for (var t in teams) {
if (teams[t].bot) {
controller.spawn(teams[t]).startRTM(function(err, bot) {
if (err) {
console.log('Error connecting bot to Slack:',err);
} else {
trackBot(bot);
}
});
}
}
});
我无法弄清楚代码中的问题是什么。这些是错误日志中的一些错误,
{"level":"error","message":"** BOT ID: after5 ...reconnect failed after #4 attempts and 8050ms","timestamp":"2016-08-26T10:53:19.175Z"}
{"level":"error","message":"Abnormal websocket close event, attempting to reconnect","timestamp":"2016-08-27T03:08:30.972Z"}
以及消息的截图,