Skip to content

Commit 1a0fd92

Browse files
committed
Split long telegram messages
1 parent dd2321a commit 1a0fd92

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

src/notifier/__tests__/notifier.spec.js

+43
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,47 @@ describe('_notify', () => {
208208
expect(telegram.sendMessage.mock.calls.length).toBe(0)
209209
})
210210
})
211+
212+
test('splits long telegram messages', async () => {
213+
const fetchData = async () => {
214+
const match = {
215+
stars: 2,
216+
id: 2341452,
217+
event: 'CLUTCH Season 2',
218+
href:
219+
'https://www.hltv.org/matches/2341452/w7m-vs-red-canids-clutch-season-2',
220+
title: 'W7M vs RED Canids',
221+
unixTimestamp: 1589749051000,
222+
}
223+
const matches = []
224+
225+
while (matches.length < 100) {
226+
matches.push(match)
227+
}
228+
229+
return {
230+
matches,
231+
timeZoneOffsetsMap: {
232+
'Europe/Moscow': 10800,
233+
},
234+
users: {
235+
0: {
236+
filter: 1,
237+
location: {
238+
timeZoneId: 'Europe/Moscow',
239+
},
240+
},
241+
},
242+
}
243+
}
244+
245+
await _notify({
246+
fetchData,
247+
log,
248+
telegram,
249+
updateUser,
250+
})
251+
252+
expect(telegram.sendMessage.mock.calls.length).toBe(2)
253+
})
211254
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { makeUsersTelegramMessages } = require('../makeUsersTelegramMessages')
2+
const {
3+
EmptyFeaturedMatchesMessage,
4+
FeaturedMatchesMessage,
5+
} = require('../messages')
6+
const { splitLongTelegramMessage } = require('../splitLongTelegramMessage')
7+
8+
const LONG_MESSAGE = `ABC vs XYZ\n\n`.repeat(833) + 'abcdef\n\n123456'
9+
const UNSPLITABLE_MESSAGE = '0123456789'.repeat(10001)
10+
11+
test('throws `Split function is not defined` error', () => {
12+
const message = makeTelegramMessage(
13+
new EmptyFeaturedMatchesMessage({ text: LONG_MESSAGE }),
14+
)
15+
16+
expect(() => splitLongTelegramMessage(message)).toThrow()
17+
})
18+
19+
test('throws `Unsplitable message` error', () => {
20+
const message = makeTelegramMessage(
21+
new FeaturedMatchesMessage({ text: UNSPLITABLE_MESSAGE }),
22+
)
23+
24+
expect(() => splitLongTelegramMessage(message)).toThrow()
25+
})
26+
27+
test('splits featured matches message', () => {
28+
const message = makeTelegramMessage(
29+
new FeaturedMatchesMessage({ text: LONG_MESSAGE }),
30+
)
31+
32+
const result = splitLongTelegramMessage(message)
33+
34+
expect(result[0]).toBe(`ABC vs XYZ\n\n`.repeat(833).trim())
35+
expect(result[1]).toBe('abcdef\n\n123456')
36+
})
37+
38+
function makeTelegramMessage(smartMessage) {
39+
const [{ messages }] = makeUsersTelegramMessages([
40+
{
41+
messages: [smartMessage],
42+
},
43+
])
44+
const [message] = messages
45+
46+
return message
47+
}

src/notifier/sendMessages.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { splitLongTelegramMessage } = require('./splitLongTelegramMessage')
2+
13
exports.sendMessages = async ({
24
alerter,
35
log,
@@ -49,7 +51,12 @@ exports.sendMessages = async ({
4951
)
5052

5153
async function sendTelegramMessages({ message, userId }) {
52-
return telegram.sendMessage(userId, message.getText(), message.getExtra())
54+
const extra = message.getExtra()
55+
const texts = splitLongTelegramMessage(message)
56+
57+
for (const text of texts) {
58+
await telegram.sendMessage(userId, text, extra)
59+
}
5360
}
5461

5562
async function sendTelegramMessagesDevelopmentEnviroment(data) {
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { MESSAGE_TYPES } = require('./consts')
2+
const MAX_TELEGRAM_MESSAGE_LENGTH = 10000
3+
4+
const splitFunctions = {
5+
[MESSAGE_TYPES.favoriteTeamsMatches]: splitMatchesMessage,
6+
[MESSAGE_TYPES.featuredMatches]: splitMatchesMessage,
7+
}
8+
9+
exports.splitLongTelegramMessage = (message) => {
10+
const text = message.getText()
11+
12+
if (text.length <= MAX_TELEGRAM_MESSAGE_LENGTH) {
13+
return [text]
14+
}
15+
16+
const type = message.getType()
17+
const splitFn = splitFunctions[type]
18+
19+
if (!splitFn) {
20+
throw new Error(`Split function is not defined. Message type='${type}'`)
21+
}
22+
23+
return splitFn(text)
24+
}
25+
26+
function splitMatchesMessage(text) {
27+
if (text.length <= MAX_TELEGRAM_MESSAGE_LENGTH) {
28+
return [text]
29+
}
30+
31+
const substring = text.substring(0, MAX_TELEGRAM_MESSAGE_LENGTH).trim()
32+
const index = substring.search(/\n\n[^\n]+$/)
33+
34+
if (-1 === index) {
35+
throw new Error('Unsplitable message: ' + text)
36+
}
37+
38+
return [
39+
substring.substring(0, index),
40+
...splitMatchesMessage(text.substring(index).trim()),
41+
]
42+
}

0 commit comments

Comments
 (0)