【问题标题】:Building a slack bot in node.js, throwing an error I can't make sense of. Has anyone seen this before?在 node.js 中构建一个 slack 机器人,抛出一个我无法理解的错误。有没有人见过这个?
【发布时间】:2016-05-20 22:51:54
【问题描述】:

我正在尝试构建一个 slack 机器人,但遇到了一个我无法理解的错误。

这是错误:

/Users/maecapozzi/Desktop/maebot/node_modules/vow/lib/vow.js:104
        throw e;
        ^

Error: [Slack Bot Error] undefined
    at assert (/Users/maecapozzi/Desktop/maebot/node_modules/slackbots/libs/utils.js:15:15)
    at /Users/maecapozzi/Desktop/maebot/node_modules/slackbots/index.js:42:9
    at Array.<anonymous> (/Users/maecapozzi/Desktop/maebot/node_modules/vow/lib/vow.js:712:56)
    at Immediate.callFns [as _onImmediate] (/Users/maecapozzi/Desktop/maebot/node_modules/vow/lib/vow.js:23:35)
    at tryOnImmediate (timers.js:534:15)
    at processImmediate [as _immediateCallback] (timers.js:514:5)

我也会与您分享我的代码,因为这应该有助于更清楚地了解所发生的事情。

bot.js:

'use strict';

var MaeBot = require('../lib/maebot');

var token = process.env.BOT_API_KEY;
var dbPath = process.env.BOT_DB_PATH;
var name = process.env.BOT_NAME;

var maebot = new MaeBot({
    token: token,
    dbPath: dbPath,
    name: name
});

maebot.run(); 

数据库.js:

var pg = require('pg');
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/maebot';

var client = new pg.Client(connectionString);
client.connect();
var query = client.query('CREATE TABLE dacts(id SERIAL PRIMARY KEY, text VARCHAR(40) not null)');
query.on('end', function() { client.end(); });

maebot.js:

'use strict';

var util = require('util'); 
var path = require('path');
var fs = require('fs');
var PostGres = require('postgresql'); 
var Bot = require('slackbots');

var MaeBot = function Constructor(settings) { 
  this.settings = settings;
  this.settings.name = this.settings.name || 'maebot'; 
  this.dbPath = settings.dbPath || path.resolve(process.cwd(), 'data', 'database.js');
  this.user = null;
  this.db = null;
};


MaeBot.prototype.run = function () { 
  MaeBot.super_.call(this, this.settings);

  this.on('start', this._onStart); 
  this.on('message', this._onMessage);
};

MaeBot.prototype._onStart = function () { 
  this._loadBotUser(); 
  this._connectDB(); 
}; 

MaeBot.prototype._loadBotUser = function () { 
  var self = this; 
  this.user = this.users.filter (function (user) { 
    return user.name === self.name; 
  })[0];
}; 

MaeBot.prototype._connectDB = function () { 
  if (!fs.existsSync(this.dbPath)) { 
    console.error('Database path ' + '"' + this.dbPath + '" does not exists or it\'s not readable."')
    process.exit(1); 
  }

  this.db = new PostGres.Database(this.dbPath);
}; 

MaeBot.prototype._welcomeMessage = function () {
  this.postMessageToChannel(this.channels[0].name, 'Hi! Maebot here.' +
    '\n I can tell you about my creator, Mae. Just say `Hi, maebot` or `' + this.name + '` to invoke me!',
    {as_user: true});
};

MaeBot.prototype._onMessage = function (message) {
  if (this._isChatMessage(message) &&
    this._isChannelConversation(message) &&
    !this._isFromMaeBot(message) &&
    this._isMentioningMaeBot(message)
    ) {
    this._replyWithRandomFact(message);
}
};

MaeBot.prototype._isChatMessage = function (message) {
  return message.type === 'message' && Boolean(message.text);
};


MaeBot.prototype._isChannelConversation = function (message) {
  return typeof message.channel === 'string' &&
  message.channel[0] === 'C';
};

MaeBot.prototype._isFromMaeBot = function (message) {
  return message.user === this.user.id;
};

MaeBot.prototype._isMentioningMaeBot = function (message) {
  return message.text.toLowerCase().indexOf('maebot') > -1 ||
  message.text.toLowerCase().indexOf(this.name) > -1;
};

MaeBot.prototype._replyWithRandomFact = function (originalMessage) {
  var self = this;
  self.db.get('SELECT id, fact FROM facts ORDER BY used ASC, RANDOM() LIMIT 1', function (err, record) {
    if (err) {
      return console.error('DATABASE ERROR:', err);
    }

    var channel = self._getChannelById(originalMessage.channel);
    self.postMessageToChannel(channel.name, record.fact, {as_user: true});
    self.db.run('UPDATE facts SET used = used + 1 WHERE id = ?', record.id);
  });
};

MaeBot.prototype._getChannelById = function (channelId) {
  return this.channels.filter(function (item) {
    return item.id === channelId;
  })[0];
};

util.inherits(MaeBot, Bot); 

module.exports = MaeBot;

【问题讨论】:

  • 断言在 /Users/maecapozzi/Desktop/maebot/node_modules/slackbots/libs/utils.js:15:15 第 15 行失败将使您进一步了解为什么会发生此错误。
  • 有问题的函数 (utils.js) function assert(condition, error) { if (!condition) { throw new Error('[Slack Bot Error] ' + error); } }
  • 按照相同的教程,我得到了同样的错误。看起来错误来自与 api 的连接。
  • 我在我的一个函数中使用了 self 而不是 this。解决这个问题,为我解决问题。为了调试,我只是破解了 node_modules/slackbots/index.js 将第 42 行替换为 this.emit('error', new Error(data.error ? data.error : data)); 而不是 this.emit('error', new Error(data.error));。这样就可以正常调试了。
  • 你到底在哪里使用了“self”?您指的是 0.5.2 版的 slackbots,对吧?

标签: javascript node.js slack-api slack


【解决方案1】:

您的机器人用户和/或令牌可能不正确。尝试重新创建您的机器人并确保您为您的应用使用有效的令牌。

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多