Mount bot.on('update') event listener only if message was sent out successfully; Reset one time keyboard after api.send request has been done

This commit is contained in:
Laurynas Karvelis 2018-03-21 13:29:05 +02:00
parent 24097ac2b2
commit f6beebe241

View File

@ -25,7 +25,7 @@ export default class Base extends EventEmitter {
* gets the Update object containing message * gets the Update object containing message
* *
* @param {object} bot * @param {object} bot
* @return {promise} returns a promise, resolved with message:answer * @return {Promise} returns a promise, resolved with message:answer
*/ */
send(bot) { send(bot) {
if (this._keyboard) { if (this._keyboard) {
@ -33,19 +33,13 @@ export default class Base extends EventEmitter {
this.properties.reply_markup = replyMarkup; this.properties.reply_markup = replyMarkup;
} }
let messageId;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._apiSend(bot) this._apiSend(bot)
.then(response => { .then(response => {
messageId = response.result.message_id;
this.emit('message:sent', response); this.emit('message:sent', response);
return response.result.message_id;
}) })
.catch(reject); .then(messageId => {
if (this._keyboard.one_time_keyboard) {
this._keyboard.replyMarkup = '';
}
const chat = this.properties.chat_id; const chat = this.properties.chat_id;
let answers = 0; let answers = 0;
@ -73,6 +67,13 @@ export default class Base extends EventEmitter {
bot.removeListener('update', listener); bot.removeListener('update', listener);
} }
}); });
})
.catch(reject)
.finally(() => {
if (this._keyboard.one_time_keyboard) {
this._keyboard.replyMarkup = '';
}
});
}); });
} }