Skip to content

Commit

Permalink
Added Noop logger, removed from ctor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Idan Harel committed Dec 15, 2016
1 parent f5346d1 commit 511caff
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
thumbs.db
*.log
node_modules/
*.iml
36 changes: 25 additions & 11 deletions README.md
Expand Up @@ -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.

Expand Down Expand Up @@ -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",
Expand All @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions lib/noop-logger.js
@@ -0,0 +1,10 @@
"use strict";

function dummy() {}

module.exports = {
info: dummy,
debug: dummy,
warn: dummy,
error: dummy
};
13 changes: 7 additions & 6 deletions lib/viber-bot.js
Expand Up @@ -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');
Expand All @@ -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`);
}
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion 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": {
Expand Down
1 change: 1 addition & 0 deletions test/util/mock-logger.js
Expand Up @@ -5,5 +5,6 @@ function dummy() {}
module.exports = {
info: dummy,
debug: dummy,
warn: dummy,
error: dummy
};

0 comments on commit 511caff

Please sign in to comment.