From 9628e702aa5a203be326ebadb9109c535d0a5d11 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 12 Nov 2015 15:57:24 +0100 Subject: [PATCH 1/4] Metadata relevant to the project --- package.json | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..24760d7 --- /dev/null +++ b/package.json @@ -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" +} \ No newline at end of file From 027c750d4ed19724f9e1e1426105cef89348fe4f Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 12 Nov 2015 15:57:33 +0100 Subject: [PATCH 2/4] Library script --- lib/winston-telegram.js | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lib/winston-telegram.js diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js new file mode 100644 index 0000000..dd5cb92 --- /dev/null +++ b/lib/winston-telegram.js @@ -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); + }); +}; \ No newline at end of file From bf71b49c163b244906f31e5d9671fafb0b855f4a Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 12 Nov 2015 15:57:40 +0100 Subject: [PATCH 3/4] Test script --- test/winston-telegram-test.js | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/winston-telegram-test.js diff --git a/test/winston-telegram-test.js b/test/winston-telegram-test.js new file mode 100644 index 0000000..5b91ff7 --- /dev/null +++ b/test/winston-telegram-test.js @@ -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); \ No newline at end of file From ab993778ac98bf7d9740a520a03bb313c6065668 Mon Sep 17 00:00:00 2001 From: Ivan Marban Date: Thu, 12 Nov 2015 15:57:55 +0100 Subject: [PATCH 4/4] Readme file --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ca3042 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file