Skip to content

Commit

Permalink
Merge branch 'release/v0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed Nov 12, 2015
2 parents 8992a05 + ab99377 commit 02e8c8b
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# winston-telegram

A [Telegram][0] transport for [winston][1].

## Installation

``` sh
$ npm install winston
$ npm install winston-telegram
```

## Usage
``` js
var winston = require('winston');

/*
* Requiring `winston-telegram` will expose
* `winston.transports.Telegram`
*/
require('winston-telegram').Telegram;

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]*

[0]: https://telegram.org/
[1]: https://github.com/flatiron/winston
67 changes: 67 additions & 0 deletions lib/winston-telegram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* winston-telegram.js: Transport for outputting logs to Telegram
*
* (C) 2015 Ivan Marban
* MIT LICENCE
*/

var util = require('util');
var request = require('request');
var winston = require('winston');

/**
* @constructs
* @param {object} options
* @param {String} options.token Telegram bot authentication token
* @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;
};

/** @extends winston.Transport */
util.inherits(Telegram, winston.Transport);

/**
* Define a getter so that `winston.transports.Telegram`
* is available and thus backwards compatible.
*/
winston.transports.Telegram = Telegram;

/**
* Core logging method exposed to Winston.
* @function log
* @member Telegram
* @param level {string} Level at which to log the message
* @param msg {string} Message to log
* @param meta {Object} **Optional** Additional metadata to attach
* @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 && callback(null, true);
}
request({
url : 'https://api.telegram.org/bot'+this.token+'/sendMessage',
method : 'POST',
json : {
chat_id : this.chatid,
text : '['+level+'] '+msg
}
}, function(error, response, body){
if (error) {
self.emit('error', err);
}
if (response && response.statusCode != 200) {
self.emit('error', response.statusCode);
}
self.emit('logged');
callback(null, true);
});
};
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "winston-telegram",
"description": "A Telegram transport for winston",
"version": "0.1.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
"url": "http://github.com/ivanmarban/winston-telegram.git"
},
"keywords": ["logging",
"sysadmin",
"tools",
"winston",
"telegram",
"bot"],
"dependencies": {
"request": "^2.65.0"
},
"devDependencies": {
"winston": ""
},
"peerDependencies": {
"winston": "",
"vows": ""
},
"main": "./lib/winston-telegram",
"scripts": { "test": "vows test/*test.js --spec" },
"engines": {
"node": ">= 0.10.0"
},
"license": "MIT"
}
37 changes: 37 additions & 0 deletions test/winston-telegram-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* winston-telegram-test.js: Tests for instances of the Telegram transport
*
* (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 TelegramTransport;

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

function assertTelegram(transport) {
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);
})
}
}
}).export(module);

0 comments on commit 02e8c8b

Please sign in to comment.