[英]How to get the current user email address for Slack bot using Bolt for JavaScript(Node.JS)
[英]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.