Skip to content

Commit

Permalink
Merge branch 'release/v2.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmarban committed Nov 9, 2021
2 parents 31fdea7 + c4771c6 commit 0b845ee
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
7 changes: 6 additions & 1 deletion 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.5.0] - 2021-11-9
### Added
- [#29](https://github.com/ivanmarban/winston-telegram/issues/29) Split long messages.

## [v2.4.1] - 2021-09-26
### Changed
- Revert back sf dependency to latest version.
Expand Down Expand Up @@ -140,7 +144,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.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
[v2.4.0]: https://github.com/ivanmarban/winston-telegram/compare/v2.3.5...v2.4.0
[v2.3.5]: https://github.com/ivanmarban/winston-telegram/compare/v2.3.4...v2.3.5
Expand Down
28 changes: 27 additions & 1 deletion lib/winston-telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const https = require('https')
const format = require('sf')
const Transport = require('winston-transport')
const MAX_MESSAGE_LENGTH = 4096

/**
* @constructs
Expand Down Expand Up @@ -99,7 +100,7 @@ module.exports = class Telegram extends Transport {
}

/**
* Actual method that sends the given message to Telegram.
* Sends the given message to Telegram splitted as needed.
*
* @function send
* @param {string} messageText - Formatted text to log.
Expand All @@ -108,6 +109,31 @@ module.exports = class Telegram extends Transport {
send (messageText) {
const self = this

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({
chat_id: this.chatId,
text: messageText,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "winston-telegram",
"description": "A Telegram transport for winston",
"version": "2.4.1",
"version": "2.5.0",
"author": "Ivan Marban",
"repository": {
"type": "git",
Expand Down
19 changes: 19 additions & 0 deletions test/winston-telegram.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ describe('winston-telegram', function () {
assert.ok(spy.callCount === 2)
done()
})

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()
})
})

describe('Emitting errors', function () {
Expand Down

0 comments on commit 0b845ee

Please sign in to comment.