diff --git a/.gitignore b/.gitignore index db04da3..8f87d8e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ thumbs.db *.log node_modules/ +*.iml diff --git a/README.md b/README.md index bd1c448..37e8fdf 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ This library is released under the terms of the Apache 2.0 license. See [License ## Library Prerequisites * Node >= 5.0.0 -* Winston >= 2.0.0 * Get your Viber Public Account authentication token. Your token is generated and provided to you during the Public Account creation process. As a Public Account admin, you can always find the account token in the "edit info" page. * SSL Certification - You'll need a trusted (ca.pem) certificate, not self-signed. You can find one at [Let's Encrypt](https://letsencrypt.org/) or buy one. @@ -45,16 +44,6 @@ Firstly, let's *import and configure* our bot: const ViberBot = require('viber-bot').Bot; const BotEvents = require('viber-bot').Events; -const winston = require('winston'); -const toYAML = require('winston-console-formatter'); - -function createLogger() { - const logger = new winston.Logger({ level: "debug" }); // We recommend DEBUG for development - logger.add(winston.transports.Console, toYAML.config()); - return logger; -} - -const logger = createLogger(); const bot = new ViberBot(logger, { authToken: YOUR_AUTH_TOKEN_HERE, name: "EchoBot", @@ -78,6 +67,31 @@ const httpsOptions = { key: ... , cert: ... , ca: ... }; // Trusted SSL certific https.createServer(httpsOptions, bot.middleware()).listen(port, () => bot.setWebhook(webhookUrl)); ``` +### Using Winston logger +We provide an option to use [Winston](https://www.npmjs.com/package/winston) logger with our library. +The only requirement is that you use Winston >= 2.0.0. + +```javascript +'use strict'; + +const ViberBot = require('viber-bot').Bot; +const winston = require('winston'); +const toYAML = require('winston-console-formatter'); // makes the output more friendly + +function createLogger() { + const logger = new winston.Logger({ level: "debug" }); // We recommend DEBUG for development + logger.add(winston.transports.Console, toYAML.config()); + return logger; +} + +const logger = createLogger(); +const bot = new ViberBot(logger, { + logger: logger, + authToken: ..., + ... +}); +``` + ### Do you supply a basic router for text messages? Well funny you ask. Yes we do. But a word of warning - messages sent to your router callback will also be emitted to the `BotEvents.MESSAGE_RECEIVED` event. diff --git a/lib/noop-logger.js b/lib/noop-logger.js new file mode 100644 index 0000000..523ccf9 --- /dev/null +++ b/lib/noop-logger.js @@ -0,0 +1,10 @@ +"use strict"; + +function dummy() {} + +module.exports = { + info: dummy, + debug: dummy, + warn: dummy, + error: dummy +}; \ No newline at end of file diff --git a/lib/viber-bot.js b/lib/viber-bot.js index 6f627da..755cfe3 100644 --- a/lib/viber-bot.js +++ b/lib/viber-bot.js @@ -4,6 +4,7 @@ const _ = require('underscore'); const util = require('util'); const EventEmitter = require('events'); +const NoopLogger = require(__dirname + '/noop-logger'); const EventConsts = require(__dirname + '/event-consts'); const ViberClient = require(__dirname + '/viber-client'); const Middleware = require(__dirname + '/middleware'); @@ -21,7 +22,7 @@ const REQUIRED_CONFIGURATION_FIELDS = ["authToken", "name", "avatar"]; const SUBSCRIBED_EVENTS = ["subscribed", "unsubscribed", "conversation_started", "message", "delivered", "seen"]; const API_URL = "https://chatapi.viber.com/pa"; -function ViberBot(logger, configuration) { +function ViberBot(configuration) { if (!configuration) { throw new Error(`Invalid configuration`); } @@ -35,11 +36,11 @@ function ViberBot(logger, configuration) { this.name = configuration.name; this.avatar = configuration.avatar; - this._logger = logger; - this._client = new ViberClient(logger, this, API_URL, configuration.registerToEvents || SUBSCRIBED_EVENTS); - this._middleware = new Middleware(logger, new MessageValidator(logger, this.authToken)); - this._messageFactory = new MessageFactory(logger); - this._regexMatcherRouter = new RegexMatcherRouter(logger); + this._logger = configuration.logger || NoopLogger; + this._client = new ViberClient(this._logger, this, API_URL, configuration.registerToEvents || SUBSCRIBED_EVENTS); + this._middleware = new Middleware(this._logger, new MessageValidator(this._logger, this.authToken)); + this._messageFactory = new MessageFactory(this._logger); + this._regexMatcherRouter = new RegexMatcherRouter(this._logger); this._callbacks = { [EventConsts.CONVERSATION_STARTED]: [] }; this._registerStreamAndHandleEvents(this._middleware.getStream()); diff --git a/package.json b/package.json index fcb8140..9ad2c82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "viber-bot", - "version": "1.0.2", + "version": "1.0.3", "description": "A bot interface to work with Viber API", "author": "Viber LTD", "engines": { diff --git a/test/util/mock-logger.js b/test/util/mock-logger.js index 98b2d24..523ccf9 100644 --- a/test/util/mock-logger.js +++ b/test/util/mock-logger.js @@ -5,5 +5,6 @@ function dummy() {} module.exports = { info: dummy, debug: dummy, + warn: dummy, error: dummy }; \ No newline at end of file