-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |