From 530a6b8800d799e8a1c2c493d5bd6ec881fc246c Mon Sep 17 00:00:00 2001 From: Laurynas Karvelis Date: Tue, 27 Feb 2018 14:02:57 +0200 Subject: [PATCH] Allow sending messages longer than 4096 chars --- src/types/Base.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/types/Base.js b/src/types/Base.js index d43c043..634139d 100644 --- a/src/types/Base.js +++ b/src/types/Base.js @@ -1,6 +1,7 @@ import { EventEmitter } from 'events'; const ANSWER_THRESHOLD = 10; +const MSG_MAX_LENGTH = 4096; /** * Base class of all classes @@ -35,7 +36,22 @@ export default class Base extends EventEmitter { let messageId; return new Promise((resolve, reject) => { - bot.api[this.method](this.properties).then(response => { + let promiseChain; + + if (this.method === 'sendMessage' && this.properties.text.length > MSG_MAX_LENGTH) { + promiseChain = Promise.resolve(); + const textChunks = this.properties.text.match(new RegExp(`.{1,${MSG_MAX_LENGTH}}`, 'g')); + + textChunks.forEach(chunk => { + const properties = Object.assign({}, this.properties, { text: chunk }); + delete properties.parse_mode; // any unclosed tags, text modifiers will not send out, send as pure text + promiseChain = promiseChain.then(() => bot.api[this.method](properties)); + }); + } else { + promiseChain = bot.api[this.method](this.properties); + } + + promiseChain.then(response => { messageId = response.result.message_id; this.emit('message:sent', response); }).catch(reject);