winston-telegram
Advanced tools
Comparing version 2.4.1 to 2.5.0
@@ -10,2 +10,6 @@ # Changelog | ||
## [v2.5.0] - 2021-11-9 | ||
### Added | ||
- [#29](https://github.com/ivanmarban/winston-telegram/issues/29) Split long messages. | ||
## [v2.4.1] - 2021-09-26 | ||
@@ -144,3 +148,4 @@ ### Changed | ||
[unreleased]: https://github.com/ivanmarban/winston-telegram/compare/v2.4.1...develop | ||
[unreleased]: https://github.com/ivanmarban/winston-telegram/compare/v2.5.0...develop | ||
[v2.5.0]: https://github.com/ivanmarban/winston-telegram/compare/v2.4.1...v2.5.0 | ||
[v2.4.1]: https://github.com/ivanmarban/winston-telegram/compare/v2.4.0...v2.4.1 | ||
@@ -147,0 +152,0 @@ [v2.4.0]: https://github.com/ivanmarban/winston-telegram/compare/v2.3.5...v2.4.0 |
@@ -11,2 +11,3 @@ /* | ||
const Transport = require('winston-transport') | ||
const MAX_MESSAGE_LENGTH = 4096 | ||
@@ -103,3 +104,3 @@ /** | ||
/** | ||
* Actual method that sends the given message to Telegram. | ||
* Sends the given message to Telegram splitted as needed. | ||
* | ||
@@ -113,2 +114,27 @@ * @function send | ||
if (messageText.length < MAX_MESSAGE_LENGTH) { | ||
self.sendMessage(messageText) | ||
} else { | ||
const size = Math.ceil(messageText.length / MAX_MESSAGE_LENGTH) | ||
const arr = Array(size) | ||
let offset = 0 | ||
for (let i = 0; i < size; i++) { | ||
arr[i] = messageText.substr(offset, MAX_MESSAGE_LENGTH) | ||
offset += MAX_MESSAGE_LENGTH | ||
} | ||
arr.forEach(message => self.sendMessage(message)) | ||
} | ||
} | ||
/** | ||
* Actual method that sends the given message to Telegram. | ||
* | ||
* @function sendMessage | ||
* @param {string} messageText - Formatted text to log. | ||
* @private | ||
*/ | ||
sendMessage (messageText) { | ||
const self = this | ||
const requestData = JSON.stringify({ | ||
@@ -115,0 +141,0 @@ chat_id: this.chatId, |
{ | ||
"name": "winston-telegram", | ||
"description": "A Telegram transport for winston", | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"author": "Ivan Marban", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -272,2 +272,21 @@ const should = require('chai').should() | ||
}) | ||
it('Should send splited message', function (done) { | ||
nock('https://api.telegram.org') | ||
.post('/botfoo/sendMessage') | ||
.times(2) | ||
.reply(200, { ok: true, result: {} }) | ||
winston.add( | ||
new Transport({ | ||
token: 'foo', | ||
chatId: 'bar', | ||
level: 'error' | ||
}) | ||
) | ||
winston.error('a'.repeat(5000)) | ||
assert.strictEqual(JSON.parse(spy.getCalls()[0].args[1]).text.length, 4096) | ||
assert.strictEqual(JSON.parse(spy.getCalls()[1].args[1]).text.length, 912) | ||
assert.ok(spy.callCount === 2) | ||
done() | ||
}) | ||
}) | ||
@@ -274,0 +293,0 @@ |
38262
665