Allow sending messages longer than 4096 chars #41

Merged
laurynas-karvelis merged 2 commits from master into master 2018-03-02 08:05:37 +00:00
2 changed files with 46 additions and 24 deletions
Showing only changes of commit e29d2d3600 - Show all commits

View File

@ -1,7 +1,6 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
const ANSWER_THRESHOLD = 10; const ANSWER_THRESHOLD = 10;
const MSG_MAX_LENGTH = 4096;
/** /**
* Base class of all classes * Base class of all classes
@ -36,25 +35,12 @@ export default class Base extends EventEmitter {
let messageId; let messageId;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let promiseChain; this._apiSend(bot)
.then(response => {
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; messageId = response.result.message_id;
this.emit('message:sent', response); this.emit('message:sent', response);
}).catch(reject); })
.catch(reject);
if (this._keyboard.one_time_keyboard) { if (this._keyboard.one_time_keyboard) {
this._keyboard.replyMarkup = ''; this._keyboard.replyMarkup = '';
@ -62,6 +48,7 @@ export default class Base extends EventEmitter {
const chat = this.properties.chat_id; const chat = this.properties.chat_id;
let answers = 0; let answers = 0;
bot.on('update', function listener(result) { bot.on('update', function listener(result) {
answers += result.length; answers += result.length;
@ -78,9 +65,7 @@ export default class Base extends EventEmitter {
if (update) { if (update) {
resolve(update.message); resolve(update.message);
this.emit('message:answer', update.message); this.emit('message:answer', update.message);
bot.removeListener('update', listener); 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 * Returns properties of the object
* @return {object} properties of object * @return {object} properties of object

View File

@ -1,5 +1,18 @@
import Base from './Base'; 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 * Message class, used to send message to a chat
*/ */
@ -93,5 +106,25 @@ export default class Message extends Base {
return this; 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 // This class inherits Base's send method
} }