2016-05-21 39 views
0

Im Anschluss an ein Tutorial, um einen einfachen Slack-Bot mit node.js auszuführen. Lernprogramm ist hier: https://scotch.io/tutorials/building-a-slack-bot-with-node-js-and-chuck-norris-super-powersNach Slack Bot tut mit Node.js, keine Ausgabe auf Lauf?

Wenn node bin\bot.js im Terminal ausgeführt wird, geht der Cursor auf eine neue Zeile, aber druckt nichts und blinkt weiterhin ohne Ausgabe.

Sollte ich einen Stack-Trace oder etwas erwarten? Ich bin nicht sicher, wie man debuggt, was ohne irgendeine Ausgabe geschieht. Heres der Code:

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" 
    } 
} 

Irgendwelche Ideen?

Antwort

0

Ich bin mir nicht ganz sicher, was Ihre CookieBot erbt von der Bibliothek, aber es gibt nichts in Ihrem Code, der nichts auf dem Terminal drucken würde.

Die scotch.io Tutorial sagt:

Wenn alles gut ging sehen Sie Ihre NorrisBot in Ihrem Slack Kanal erscheinen und eine Begrüßungsnachricht wird es veröffentlicht. Versuche ihn mehrmals mit seinem Namen anzurufen und genieße seine Witze!

Wenn Sie überprüfen möchten, ob es funktioniert, überprüfen Sie den Slack-Kanal, mit dem es verbunden ist. Alternativ fügen Sie ein paar console.log() Zeilen hinzu, um ein Gefühl dafür zu bekommen, wie der Code läuft.

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..."); 
     ... 
Verwandte Themen