繁体   English   中英

在使用Node.js的Slack bot tut之后,运行时没有输出?

[英]Following Slack bot tut using Node.js, no output on run?

按照教程使用node.js运行一个简单的Slack机器人。 教程在这里: https : //scotch.io/tutorials/building-a-slack-bot-with-node-js-and-chuck-norris-super-powers

在终端中运行node bin\\bot.js时,光标移至新行,但不打印任何内容,并继续闪烁而没有任何输出。

我应该期待堆栈跟踪还是什么? 我不确定如何调试没有任何输出的情况。 这是代码:

lib / cookiebot.js:

'use strict';

var util = require('util');
var path = require('path');
var fs = require('fs');
var SQLite = require('sqlite3').verbose();
var Bot = require('slackbots');

var CookieBot = function Constructor(settings) {
    this.settings = settings;
    this.settings.name = this.settings.name || 'cookiebot';
  //  this.dbPath = settings.dbPath || path.resolve(process.cwd(), 'data', 'cookiebot.db');

    this.user = null;
  //  this.db = null;
};

// inherits methods and properties from the Bot constructor
util.inherits(CookieBot, Bot);

module.exports = CookieBot;

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

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


CookieBot.prototype._onStart = function () {
    this._loadBotUser();
  //  this._connectDb();
    this._firstRunCheck();
};

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

CookieBot.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 SQLite.Database(this.dbPath);
};

CookieBot.prototype._firstRunCheck = function () {
    // var self = this;
    // self.db.get('SELECT val FROM info WHERE name = "lastrun" LIMIT 1', function (err, record) {
    //     if (err) {
    //         return console.error('DATABASE ERROR:', err);
    //     }
    //
    //     var currentTime = (new Date()).toJSON();
    //
    //     // this is a first run
    //     if (!record) {
    //         self._welcomeMessage();
    //         return self.db.run('INSERT INTO info(name, val) VALUES("lastrun", ?)', currentTime);
    //     }
    //
    //     // updates with new last running time
    //     self.db.run('UPDATE info SET val = ? WHERE name = "lastrun"', currentTime);
    // });
};

CookieBot.prototype._welcomeMessage = function () {
    this.postMessageToChannel(this.channels[0].name, 'Hi guys, roundhouse-kick anyone?' +
        '\n I can tell jokes, but very honest ones. Just say `Chuck Norris` or `' + this.name + '` to invoke me!',
        {as_user: true});
};

CookieBot.prototype._onMessage = function (message) {
    if (this._isChatMessage(message) &&
        this._isChannelConversation(message) &&
        !this._isFromCookieBot(message) &&
        this._isMentioningCookies(message)
    ) {
        this._replyWithRandomJoke(message);
    }
};

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

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

CookieBot.prototype._isFromNorrisBot = function (message) {
    return message.user === this.user.id;
};

CookieBot.prototype._isMentioningCookies = function (message) {
    return message.text.toLowerCase().indexOf('cookies') > -1 ||
        message.text.toLowerCase().indexOf(this.name) > -1;
};

CookieBot.prototype._replyWithRandomJoke = function (originalMessage) {
    // var self = this;
    // self.db.get('SELECT id, joke FROM jokes 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.joke, {as_user: true});
    //     self.db.run('UPDATE jokes SET used = used + 1 WHERE id = ?', record.id);
    // });
};

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

bin \\ bot.js

'use strict';

var CookieBot = require('../lib/cookiebot');

var token = 'xxxxxx';
//var dbPath = process.env.BOT_DB_PATH;
var name = 'cookiebot';

var cookiebot = new CookieBot({
    token: token,
    name: name
  //  dbPath: dbPath,

});

cookiebot.run();

package.json

{
  "name": "cookiebot",
  "version": "1.0.0",
  "description": "",
  "main": "lib/cookiebot.js",
  "scripts": {
    "start": "node bin/bot.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "main": "lib/cookiebot.js",
  "bin": {
    "cookiebot": "bin/bot.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "slackbots": "^0.5.1",
    "sqlite3": "^3.1.4"
  }
}

有任何想法吗?

我不能完全确定您的CookieBot是从库继承的,但是您的代码中没有任何内容可以将任何内容打印到终端。

scotch.io教程说:

如果一切顺利,您会看到NorrisBot出现在Slack频道中,并在其中发布问候语。 尝试多次用名字来称呼他,并享受他的笑话!

如果要检查它是否正常工作,请检查它连接到的松弛通道。 或者,在其中添加几行console.log()行,以了解代码的运行方式。

    CookieBot.prototype.run = function () {
      console.log( "Initialising Cookie Bot..." );
      CookieBot.super_.call(this, this.settings);

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

  CookieBot.prototype._onMessage = function (message) {
    console.log( "Cookie Bot received message..." );
    ...


    CookieBot.prototype._onStart = function () {
      console.log( "Cookie Bot started..." );
      ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM