Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed Dec 5, 2016
2 parents 082805a + c03d710 commit 59acdc5
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 93 deletions.
45 changes: 38 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ winston.add(winston.transports.Telegram, options);
Options are the following:

* __token:__ The Telegram bot authentication token. *[required]*
* __chatid:__ The chatid you want to send to. *[required]*
* __chatId:__ The chatid you want to send to. *[required]*
* __level:__ Level of messages that this transport should log. *[optional]* *[default info]*
* __unique:__ Whether to log only the declared level and none above. *[boolean]* *[optional]*
* __silent:__ Whether to suppress output. *[boolean]* *[optional]*
* __disable_notification:__ Sends the message silently. *[boolean]* *[optional]*
* __disableNotification:__ Sends the message silently. *[boolean]* *[optional]*
* __template:__ Format output message. *[optional]*
* __handleExceptions:__ Handle uncaught exceptions. *[boolean]* *[optional]*

String template is based on named arguments:
``` js
'{level}' -> level of messages
'{message}' -> text of messages
```

Due applying some coding style, you must change these option properties if you're updating from lower versions to 1.0.0:

- chatid to chatId
- disable_notificacion to disableNotification

## Examples
Using the Default Logger
``` js
Expand All @@ -49,7 +55,7 @@ require('winston-telegram').Telegram;

winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatid : 'CHAT_ID',
chatId : 'CHAT_ID',
level : 'error',
unique : true
});
Expand All @@ -67,17 +73,17 @@ var logger = new (winston.Logger)({
new (winston.transports.Telegram)({
name: 'error-channel',
token : 'TELEGRAM_TOKEN',
chatid : 'CHAT_ID_1',
chatId : 'CHAT_ID_1',
level : 'error',
unique : true
}),
new (winston.transports.Telegram)({
name: 'info-channel',
token : 'TELEGRAM_TOKEN',
chatid : 'CHAT_ID_2',
chatId : 'CHAT_ID_2',
level : 'info',
unique : true,
disable_notification: true
disableNotification: true
})
]
});
Expand All @@ -94,7 +100,7 @@ require('winston-telegram').Telegram;

winston.add(winston.transports.Telegram, {
token : 'TELEGRAM_TOKEN',
chatid : 'CHAT_ID',
chatId : 'CHAT_ID',
level : 'error',
unique : true,
template : '[{level}] [{message}]'
Expand All @@ -105,5 +111,30 @@ winston.log('error', 'Redrum. Redrum. Redrum.');
//Output: [error] [Redrum. Redrum. Redrum.]
```

## Change history

### v1.0.0 (2016/12/05)
- [#6](https://github.com/ivanmarban/winston-telegram/pull/6) Add optional handleExceptions param (@speedone)
- Node.js coding style
- Change option properties for matching coding style

### v0.4.0 (2016/09/26)
- [#5](https://github.com/ivanmarban/winston-telegram/issues/5) Add message template option
- Update dependencies
- Remove peer dependecies

### v0.3.0 (2016/07/17)
- [#2](https://github.com/ivanmarban/winston-telegram/issues/2) Allow multiple transports, send messages silently
- Update dependencies

### v0.2.1 (2016/03/30)
- Fix typos

### v0.2.0 (2016/03/08)
- [#1](https://github.com/ivanmarban/winston-telegram/issues/1) Add log level option

### v0.1.0 (2015/11/12)
- First version

[0]: https://telegram.org/
[1]: https://github.com/flatiron/winston
127 changes: 64 additions & 63 deletions lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
* MIT LICENCE
*/

var util = require('util');
var request = require('request');
var winston = require('winston');
var util = require('util');
var request = require('request');
var winston = require('winston');
var nargs = /\{([0-9a-zA-Z_]+)\}/g;

/**
* @constructs
* @param {object} options
* @param {String} options.token Telegram bot authentication token
* @param {String} options.chatid Telegram unique identifier for chat
* @param {String} options.chatId Telegram unique identifier for chat
*/
var Telegram = exports.Telegram = function (options) {
options = options || {};
if (!options.token || !options.chatid){
throw new Error('winston-telegram requires \'token\' and \'chatid\' property');
}
this.token = options.token;
this.chatid = options.chatid;
this.level = options.level || 'info';
this.unique = options.unique || false;
this.silent = options.silent || false;
this.disable_notification = options.disable_notification || false;
this.name = options.name || this.name;
this.template = options.template || '[{level}] {message}';
options = options || {};
if (!options.token || !options.chatId){
throw new Error('winston-telegram requires \'token\' and \'chatId\' property');
}
this.token = options.token;
this.chatId = options.chatId;
this.level = options.level || 'info';
this.handleExceptions = options.handleExceptions || false;
this.unique = options.unique || false;
this.silent = options.silent || false;
this.disableNotification = options.disableNotification || false;
this.name = options.name || this.name;
this.template = options.template || '[{level}] {message}';
};

/** @extends winston.Transport */
Expand All @@ -55,54 +56,54 @@ Telegram.prototype.name = 'telegram';
* @param callback {function} Continuation to respond to when complete.
*/
Telegram.prototype.log = function (level, msg, meta, callback) {
var self = this;
if (this.silent) return callback(null, true);
if (this.unique && this.level != level) return callback(null, true);
request({
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage',
method : 'POST',
json : {
chat_id : this.chatid,
text : format(this.template,{level : level, message : msg}),
disable_notification : this.disable_notification
}
}, function(error, response, body){
if (error) {
self.emit('error', error);
}
if (response && response.statusCode != 200) {
self.emit('error', response.statusCode);
}
self.emit('logged');
callback(null, true);
});
var self = this;
if (this.silent) return callback(null, true);
if (this.unique && this.level != level) return callback(null, true);
request({
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage',
method : 'POST',
json : {
chat_id : this.chatId,
text : format(this.template,{level : level, message : msg}),
disable_notification : this.disableNotification
}
}, function(error, response, body){
if (error) {
self.emit('error', error);
}
if (response && response.statusCode != 200) {
self.emit('error', response.statusCode);
}
self.emit('logged');
callback(null, true);
});
};

function format(string) {
var args
if (arguments.length === 2 && typeof arguments[1] === 'object') {
args = arguments[1]
} else {
args = new Array(arguments.length - 1)
for (var i = 1; i < arguments.length; ++i) {
args[i - 1] = arguments[i]
}
}
if (!args || !args.hasOwnProperty) {
args = {}
}
return string.replace(nargs, function replaceArg(match, i, index) {
var result
if (string[index - 1] === '{' &&
string[index + match.length] === '}') {
return i
} else {
result = args.hasOwnProperty(i) ? args[i] : null
if (result === null || result === undefined) {
return ''
}
return result
}
})
var args
if (arguments.length === 2 && typeof arguments[1] === 'object') {
args = arguments[1]
} else {
args = new Array(arguments.length - 1)
for (var i = 1; i < arguments.length; ++i) {
args[i - 1] = arguments[i]
}
}
if (!args || !args.hasOwnProperty) {
args = {}
}
return string.replace(nargs, function replaceArg(match, i, index) {
var result
if (string[index - 1] === '{' &&
string[index + match.length] === '}') {
return i
} else {
result = args.hasOwnProperty(i) ? args[i] : null
if (result === null || result === undefined) {
return ''
}
return result
}
})
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "winston-telegram",
"description": "A Telegram transport for winston",
"version": "0.4.0",
"version": "1.0.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
Expand All @@ -16,7 +16,7 @@
"bot"
],
"dependencies": {
"request": "^2.75.0"
"request": "^2.79.0"
},
"devDependencies": {
"winston": "",
Expand Down
42 changes: 21 additions & 21 deletions test/winston-telegram-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
* (C) 2015 Ivan Marban
* MIT LICENSE
*/
var vows = require('vows'),
assert = require('assert'),
winston = require('winston'),
helpers = require('winston/test/helpers'),
Telegram = require('../lib/winston-telegram').Telegram;
var vows = require('vows'),
assert = require('assert'),
winston = require('winston'),
helpers = require('winston/test/helpers'),
Telegram = require('../lib/winston-telegram').Telegram;

var TelegramTransport;

TelegramTransport = new (Telegram)({
token : '177492804:AAG318J_PjC03-okUmqQV652EDbf_Rr0vTo',
chatid : '-50115750'
TelegramTransport = new(Telegram)({
token: '177492804:AAG318J_PjC03-okUmqQV652EDbf_Rr0vTo',
chatId: '-50115750'
});

function assertTelegram(transport) {
assert.instanceOf(transport, Telegram);
assert.isFunction(transport.log);
assert.instanceOf(transport, Telegram);
assert.isFunction(transport.log);
}

vows.describe('winston-telegram').addBatch({
'An instance of the Telegram Transport': {
'when passed an options': {
'should have the proper methods defined': function () {
assertTelegram(TelegramTransport);
},
'the log() method': helpers.testNpmLevels(TelegramTransport, 'should log messages to Telegram', function (ign, err, logged) {
assert.isNull(err);
assert.isTrue(logged);
})
}
}
'An instance of the Telegram Transport': {
'when passed an options': {
'should have the proper methods defined': function() {
assertTelegram(TelegramTransport);
},
'the log() method': helpers.testNpmLevels(TelegramTransport, 'should log messages to Telegram', function(ign, err, logged) {
assert.isNull(err);
assert.isTrue(logged);
})
}
}
}).export(module);

0 comments on commit 59acdc5

Please sign in to comment.