Skip to content

Commit 0b845ee

Browse files
committedNov 9, 2021
Merge branch 'release/v2.5.0'
2 parents 31fdea7 + c4771c6 commit 0b845ee

5 files changed

+54
-4
lines changed
 

‎CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
## [v2.5.0] - 2021-11-9
11+
### Added
12+
- [#29](https://github.com/ivanmarban/winston-telegram/issues/29) Split long messages.
13+
1014
## [v2.4.1] - 2021-09-26
1115
### Changed
1216
- Revert back sf dependency to latest version.
@@ -140,7 +144,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
140144
## [v0.1.0] - 2015-11-12
141145
- First version.
142146

143-
[unreleased]: https://github.com/ivanmarban/winston-telegram/compare/v2.4.1...develop
147+
[unreleased]: https://github.com/ivanmarban/winston-telegram/compare/v2.5.0...develop
148+
[v2.5.0]: https://github.com/ivanmarban/winston-telegram/compare/v2.4.1...v2.5.0
144149
[v2.4.1]: https://github.com/ivanmarban/winston-telegram/compare/v2.4.0...v2.4.1
145150
[v2.4.0]: https://github.com/ivanmarban/winston-telegram/compare/v2.3.5...v2.4.0
146151
[v2.3.5]: https://github.com/ivanmarban/winston-telegram/compare/v2.3.4...v2.3.5

‎lib/winston-telegram.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const https = require('https')
99
const format = require('sf')
1010
const Transport = require('winston-transport')
11+
const MAX_MESSAGE_LENGTH = 4096
1112

1213
/**
1314
* @constructs
@@ -99,7 +100,7 @@ module.exports = class Telegram extends Transport {
99100
}
100101

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

112+
if (messageText.length < MAX_MESSAGE_LENGTH) {
113+
self.sendMessage(messageText)
114+
} else {
115+
const size = Math.ceil(messageText.length / MAX_MESSAGE_LENGTH)
116+
const arr = Array(size)
117+
let offset = 0
118+
119+
for (let i = 0; i < size; i++) {
120+
arr[i] = messageText.substr(offset, MAX_MESSAGE_LENGTH)
121+
offset += MAX_MESSAGE_LENGTH
122+
}
123+
arr.forEach(message => self.sendMessage(message))
124+
}
125+
}
126+
127+
/**
128+
* Actual method that sends the given message to Telegram.
129+
*
130+
* @function sendMessage
131+
* @param {string} messageText - Formatted text to log.
132+
* @private
133+
*/
134+
sendMessage (messageText) {
135+
const self = this
136+
111137
const requestData = JSON.stringify({
112138
chat_id: this.chatId,
113139
text: messageText,

‎package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "winston-telegram",
33
"description": "A Telegram transport for winston",
4-
"version": "2.4.1",
4+
"version": "2.5.0",
55
"author": "Ivan Marban",
66
"repository": {
77
"type": "git",

‎test/winston-telegram.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ describe('winston-telegram', function () {
269269
assert.ok(spy.callCount === 2)
270270
done()
271271
})
272+
273+
it('Should send splited message', function (done) {
274+
nock('https://api.telegram.org')
275+
.post('/botfoo/sendMessage')
276+
.times(2)
277+
.reply(200, { ok: true, result: {} })
278+
winston.add(
279+
new Transport({
280+
token: 'foo',
281+
chatId: 'bar',
282+
level: 'error'
283+
})
284+
)
285+
winston.error('a'.repeat(5000))
286+
assert.strictEqual(JSON.parse(spy.getCalls()[0].args[1]).text.length, 4096)
287+
assert.strictEqual(JSON.parse(spy.getCalls()[1].args[1]).text.length, 912)
288+
assert.ok(spy.callCount === 2)
289+
done()
290+
})
272291
})
273292

274293
describe('Emitting errors', function () {

0 commit comments

Comments
 (0)
Please sign in to comment.