From 2371404f1c67b37ae41c124b32dcb2ca9008ae66 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Mon, 5 Feb 2018 21:46:29 +0330 Subject: [PATCH 1/5] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 255895b..5746467 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "0.9.1", + "version": "1.0.0", "description": "Control Telegram bots easily using the new Telegram API", "main": "build/index.js", "scripts": { -- 2.34.1 From 9d2c4be69aea63ee66399fe994eed6c6a088f348 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Wed, 14 Feb 2018 14:09:25 +0330 Subject: [PATCH 2/5] chore: bump version to 1.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5746467..bc265bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "1.0.0", + "version": "1.0.1", "description": "Control Telegram bots easily using the new Telegram API", "main": "build/index.js", "scripts": { -- 2.34.1 From 55c375bc56610937c1fe6ecf663fa4e65d8e7ea2 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Sun, 18 Feb 2018 20:52:36 +0330 Subject: [PATCH 3/5] chore: bump version to 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bc265bb..d7e11ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telegram-api", - "version": "1.0.1", + "version": "2.0.0", "description": "Control Telegram bots easily using the new Telegram API", "main": "build/index.js", "scripts": { -- 2.34.1 From 530a6b8800d799e8a1c2c493d5bd6ec881fc246c Mon Sep 17 00:00:00 2001 From: Laurynas Karvelis Date: Tue, 27 Feb 2018 14:02:57 +0200 Subject: [PATCH 4/5] 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); -- 2.34.1 From e29d2d36002784e830c48206427f549a035f4b80 Mon Sep 17 00:00:00 2001 From: Laurynas Karvelis Date: Tue, 27 Feb 2018 18:18:59 +0200 Subject: [PATCH 5/5] Refactor api.send Message related logic into a separate private method --- src/types/Base.js | 37 +++++++++++++------------------------ src/types/Message.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/types/Base.js b/src/types/Base.js index 634139d..23a259c 100644 --- a/src/types/Base.js +++ b/src/types/Base.js @@ -1,7 +1,6 @@ import { EventEmitter } from 'events'; const ANSWER_THRESHOLD = 10; -const MSG_MAX_LENGTH = 4096; /** * Base class of all classes @@ -36,25 +35,12 @@ export default class Base extends EventEmitter { let messageId; return new Promise((resolve, reject) => { - 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); + this._apiSend(bot) + .then(response => { + messageId = response.result.message_id; + this.emit('message:sent', response); + }) + .catch(reject); if (this._keyboard.one_time_keyboard) { this._keyboard.replyMarkup = ''; @@ -62,6 +48,7 @@ export default class Base extends EventEmitter { const chat = this.properties.chat_id; let answers = 0; + bot.on('update', function listener(result) { answers += result.length; @@ -69,8 +56,8 @@ export default class Base extends EventEmitter { // if in a group, there will be a reply to this message if (chat < 0) { return message.chat.id === chat - && message.reply_to_message - && message.reply_to_message.message_id === messageId; + && message.reply_to_message + && message.reply_to_message.message_id === messageId; } return message && message.chat.id === chat; @@ -78,9 +65,7 @@ export default class Base extends EventEmitter { if (update) { resolve(update.message); - this.emit('message:answer', update.message); - bot.removeListener('update', listener); } @@ -91,6 +76,10 @@ export default class Base extends EventEmitter { }); } + _apiSend(bot) { + return bot.api[this.method](this.properties); + } + /** * Returns properties of the object * @return {object} properties of object diff --git a/src/types/Message.js b/src/types/Message.js index ab3299a..9879269 100644 --- a/src/types/Message.js +++ b/src/types/Message.js @@ -1,5 +1,18 @@ import Base from './Base'; +const MSG_MAX_LENGTH = 4096; + +function splitToChunks(str, size) { + const numChunks = Math.ceil(str.length / size); + const chunks = new Array(numChunks); + + for (let i = 0, o = 0; i < numChunks; ++i, o += size) { + chunks[i] = str.substr(o, size); + } + + return chunks; +} + /** * Message class, used to send message to a chat */ @@ -93,5 +106,25 @@ export default class Message extends Base { return this; } + // override Base.prototype._apiSend() method + _apiSend(bot) { + if (this.properties.text && this.properties.text.length > MSG_MAX_LENGTH) { + let promiseChain = Promise.resolve(); + const textChunks = splitToChunks(this.properties.text, MSG_MAX_LENGTH); + + textChunks.forEach(chunk => { + const properties = Object.assign({}, this.properties, { text: chunk }); + // any unclosed tags, text modifiers will not send out, send as pure text + delete properties.parse_mode; + + promiseChain = promiseChain.then(() => bot.api[this.method](properties)); + }); + + return promiseChain; + } + + return bot.api[this.method](this.properties); + } + // This class inherits Base's send method } -- 2.34.1