Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed Jun 22, 2019
2 parents df8e763 + e90ca4a commit 6a46130
Show file tree
Hide file tree
Showing 11 changed files with 556 additions and 435 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 120
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v2.1.0] - 2019-06-22
- Replace [Request](https://github.com/request/request) by standard https Node.js lib.
- Fix examples.

## [v2.0.1] - 2019-04-28
- Code style: ESLint + Standard + Prettier.
- Test coverage.
Expand Down Expand Up @@ -58,6 +62,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [v0.1.0] - 2015-11-12
- First version.

[unreleased]: https://github.com/ivanmarban/winston-telegram/compare/v2.1.0...develop
[v2.1.0]: https://github.com/ivanmarban/winston-telegram/compare/v2.0.1...v2.1.0
[v2.0.1]: https://github.com/ivanmarban/winston-telegram/compare/v2.0.0...v2.0.1
[v2.0.0]: https://github.com/ivanmarban/winston-telegram/compare/v1.3.1...v2.0.0
[v1.3.1]: https://github.com/ivanmarban/winston-telegram/compare/v1.3.0...v1.3.1
Expand Down
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mhart/alpine-node:6
WORKDIR /app
COPY . .
RUN npm install
RUN echo $'#!/bin/sh\n\
sed -i "s/TELEGRAM_TOKEN/$TELEGRAM_TOKEN/g" /app/examples/*\n\
sed -i "s/CHAT_ID_1/$CHAT_ID/g" /app/examples/*\n\
sed -i "s/CHAT_ID_2/$CHAT_ID/g" /app/examples/*\n\
sed -i "s/CHAT_ID/$CHAT_ID/g" /app/examples/*\n\
files=$(find /app/examples -type f)\n\
for i in $files\n\
do\n\
node $i\n\
sleep 5\n\
done'\
>> /app/start.sh
RUN chmod +x /app/start.sh
CMD ["/bin/sh", "/app/start.sh"]
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ A [Telegram][0] transport for [winston][1].
[![Coverage Status](https://coveralls.io/repos/github/ivanmarban/winston-telegram/badge.svg?branch=master)](https://coveralls.io/github/ivanmarban/winston-telegram?branch=master)
[![dependencies Status](https://david-dm.org/ivanmarban/winston-telegram/status.svg)](https://david-dm.org/ivanmarban/winston-telegram)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SJLW6PTHQQNBS)

## winston-telegram@2

Expand Down Expand Up @@ -53,7 +54,18 @@ String template is based on named arguments:

## Examples

Take a look at the [examples][3]
Follow below steps to run the examples:

``` sh
$ git clone [email protected]:ivanmarban/winston-telegram.git -b master --single-branch
$ npm install
```

Replace `TELEGRAM_TOKEN` and `CHAT_ID` with appropiate values, then run whatever example you want:

``` sh
$ node examples/default-logger.js
```

[0]: https://telegram.org/
[1]: https://github.com/flatiron/winston
Expand Down
2 changes: 1 addition & 1 deletion examples/batching-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const logger = require('winston')
const TelegramLogger = require('winston-telegram')
const TelegramLogger = require('../lib/winston-telegram')

logger.add(
new TelegramLogger({
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const logger = require('winston')
const TelegramLogger = require('winston-telegram')
const TelegramLogger = require('../lib/winston-telegram')

logger.add(
new TelegramLogger({
Expand Down
2 changes: 1 addition & 1 deletion examples/template-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const logger = require('winston')
const TelegramLogger = require('winston-telegram')
const TelegramLogger = require('../lib/winston-telegram')

logger.add(
new TelegramLogger({
Expand Down
87 changes: 54 additions & 33 deletions lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* MIT LICENCE
*/

const request = require('request')
const https = require('https')
const format = require('sf')
const Transport = require('winston-transport')

Expand Down Expand Up @@ -34,9 +34,7 @@ module.exports = class Telegram extends Transport {
throw new Error("winston-telegram requires 'token' and 'chatId' property")
}
if (options.formatMessage && typeof options.formatMessage !== 'function') {
throw new Error(
"winston-telegram 'formatMessage' property should be function"
)
throw new Error("winston-telegram 'formatMessage' property should be function")
}

this.token = options.token
Expand All @@ -58,8 +56,8 @@ module.exports = class Telegram extends Transport {
/**
* Core logging method exposed to Winston.
*
* @param {Object} info - TODO: add param description.
* @param {Function} callback - TODO: add param description.
* @param {object} info - An object representing the log message.
* @param {Function} callback - Continuation to respond to when complete.
* @returns {undefined}
*/
log (info, callback) {
Expand All @@ -85,9 +83,7 @@ module.exports = class Telegram extends Transport {

if (!this.batchingTimeout) {
this.batchingTimeout = setTimeout(function () {
let combinedMessages = self.batchedMessages.join(
self.batchingSeparator
)
let combinedMessages = self.batchedMessages.join(self.batchingSeparator)
self.send(combinedMessages)
self.batchedMessages = []
self.batchingTimeout = 0
Expand All @@ -97,7 +93,7 @@ module.exports = class Telegram extends Transport {
self.send(messageText)
}

return callback(null, true)
callback(null, true)
}

/**
Expand All @@ -110,29 +106,54 @@ module.exports = class Telegram extends Transport {
send (messageText) {
let self = this

request(
{
url: 'https://api.telegram.org/bot' + this.token + '/sendMessage',
method: 'POST',
json: {
chat_id: this.chatId,
text: messageText,
disable_notification: this.disableNotification
}
},
function (error, response, body) {
if (error) {
self.emit('error', error)
}
if (response && response.statusCode !== 200) {
self.emit(
'error',
response.statusCode +
((body && body.description && ': ' + body.description) || '')
)
}
self.emit('logged')
const requestData = JSON.stringify({
chat_id: this.chatId,
text: messageText,
disable_notification: this.disableNotification
})

const options = {
hostname: 'api.telegram.org',
path: '/bot' + this.token + '/sendMessage',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': requestData.length
}
}

self.request(options, requestData, function (error, response, body) {
if (error) {
self.emit('error', error)
}
if (response && response.statusCode !== 200) {
self.emit('error', response.statusCode + ((body && body.description && ': ' + body.description) || ''))
}
)
self.emit('logged')
})
}

/**
* Performs a https request to a server.
*
* @function request
* @param {object} options - Request options.
* @param {object} requestData - Request data.
* @param {Function} callback - Continuation to respond to when complete.
* @private
*/
request (options, requestData, callback) {
const request = https.request(options, function (response) {
let responseData = ''
response.on('data', function (chunk) {
responseData += chunk
})
response.on('end', function () {
callback(null, response, JSON.parse(responseData))
})
})
request.on('error', callback)
request.write(requestData, 'utf-8')
request.end()
}
}
Loading

0 comments on commit 6a46130

Please sign in to comment.