mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-22 18:07:16 +00:00
[telegram] Expose TelegramBot#processUpdate()
Feature: We are exposing TelegramBot#processUpdate() to allow instances to be used, with mechanisms of fetching updates, other than those provided by the library. References: * Example use case: https://github.com/GochoMugo/tgfancy/pull/7
This commit is contained in:
parent
0cd993f3e8
commit
eba70cd3d7
15
README.md
15
README.md
@ -80,6 +80,7 @@ TelegramBot
|
||||
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code>
|
||||
* [.setWebHook(url, [cert])](#TelegramBot+setWebHook)
|
||||
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
|
||||
* [.processUpdate(update)](#TelegramBot+processUpdate)
|
||||
* [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ <code>Promise</code>
|
||||
* [.answerInlineQuery(inlineQueryId, results, [options])](#TelegramBot+answerInlineQuery) ⇒ <code>Promise</code>
|
||||
* [.forwardMessage(chatId, fromChatId, messageId)](#TelegramBot+forwardMessage) ⇒ <code>Promise</code>
|
||||
@ -213,6 +214,20 @@ Use this method to receive incoming updates using long polling
|
||||
| [limit] | <code>Number</code> | <code>String</code> | Limits the number of updates to be retrieved. |
|
||||
| [offset] | <code>Number</code> | <code>String</code> | Identifier of the first update to be returned. |
|
||||
|
||||
<a name="TelegramBot+processUpdate"></a>
|
||||
|
||||
### telegramBot.processUpdate(update)
|
||||
Process an update; emitting the proper events and executing regexp
|
||||
callbacks. This method is useful should you be using a different
|
||||
way to fetch updates, other than those provided by TelegramBot.
|
||||
|
||||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
|
||||
**See**: https://core.telegram.org/bots/api#update
|
||||
|
||||
| Param | Type |
|
||||
| --- | --- |
|
||||
| update | <code>Object</code> |
|
||||
|
||||
<a name="TelegramBot+sendMessage"></a>
|
||||
|
||||
### telegramBot.sendMessage(chatId, text, [options]) ⇒ <code>Promise</code>
|
||||
|
177
src/telegram.js
177
src/telegram.js
@ -78,92 +78,6 @@ class TelegramBot extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an update; emitting the proper events and executing regexp
|
||||
* callbacks
|
||||
* @param {Object} update
|
||||
* @private
|
||||
*/
|
||||
_processUpdate(update) {
|
||||
debug('Process Update %j', update);
|
||||
const message = update.message;
|
||||
const editedMessage = update.edited_message;
|
||||
const channelPost = update.channel_post;
|
||||
const editedChannelPost = update.edited_channel_post;
|
||||
const inlineQuery = update.inline_query;
|
||||
const chosenInlineResult = update.chosen_inline_result;
|
||||
const callbackQuery = update.callback_query;
|
||||
|
||||
if (message) {
|
||||
debug('Process Update message %j', message);
|
||||
this.emit('message', message);
|
||||
const processMessageType = messageType => {
|
||||
if (message[messageType]) {
|
||||
debug('Emitting %s: %j', messageType, message);
|
||||
this.emit(messageType, message);
|
||||
}
|
||||
};
|
||||
TelegramBot.messageTypes.forEach(processMessageType);
|
||||
if (message.text) {
|
||||
debug('Text message');
|
||||
this._textRegexpCallbacks.some(reg => {
|
||||
debug('Matching %s with %s', message.text, reg.regexp);
|
||||
const result = reg.regexp.exec(message.text);
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
debug('Matches %s', reg.regexp);
|
||||
reg.callback(message, result);
|
||||
// returning truthy value exits .some
|
||||
return this.options.onlyFirstMatch;
|
||||
});
|
||||
}
|
||||
if (message.reply_to_message) {
|
||||
// Only callbacks waiting for this message
|
||||
this._onReplyToMessages.forEach(reply => {
|
||||
// Message from the same chat
|
||||
if (reply.chatId === message.chat.id) {
|
||||
// Responding to that message
|
||||
if (reply.messageId === message.reply_to_message.message_id) {
|
||||
// Resolve the promise
|
||||
reply.callback(message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (editedMessage) {
|
||||
debug('Process Update edited_message %j', editedMessage);
|
||||
this.emit('edited_message', editedMessage);
|
||||
if (editedMessage.text) {
|
||||
this.emit('edited_message_text', editedMessage);
|
||||
}
|
||||
if (editedMessage.caption) {
|
||||
this.emit('edited_message_caption', editedMessage);
|
||||
}
|
||||
} else if (channelPost) {
|
||||
debug('Process Update channel_post %j', channelPost);
|
||||
this.emit('channel_post', channelPost);
|
||||
} else if (editedChannelPost) {
|
||||
debug('Process Update edited_channel_post %j', editedChannelPost);
|
||||
this.emit('edited_channel_post', editedChannelPost);
|
||||
if (editedChannelPost.text) {
|
||||
this.emit('edited_channel_post_text', editedChannelPost);
|
||||
}
|
||||
if (editedChannelPost.caption) {
|
||||
this.emit('edited_channel_post_caption', editedChannelPost);
|
||||
}
|
||||
} else if (inlineQuery) {
|
||||
debug('Process Update inline_query %j', inlineQuery);
|
||||
this.emit('inline_query', inlineQuery);
|
||||
} else if (chosenInlineResult) {
|
||||
debug('Process Update chosen_inline_result %j', chosenInlineResult);
|
||||
this.emit('chosen_inline_result', chosenInlineResult);
|
||||
} else if (callbackQuery) {
|
||||
debug('Process Update callback_query %j', callbackQuery);
|
||||
this.emit('callback_query', callbackQuery);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates url with bot token and provided path/method you want to be got/executed by bot
|
||||
* @param {String} path
|
||||
@ -308,7 +222,7 @@ class TelegramBot extends EventEmitter {
|
||||
reason: 'Polling restart',
|
||||
});
|
||||
}
|
||||
this._polling = new TelegramBotPolling(this._request.bind(this), this.options.polling, this._processUpdate.bind(this));
|
||||
this._polling = new TelegramBotPolling(this._request.bind(this), this.options.polling, this.processUpdate.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -339,7 +253,7 @@ class TelegramBot extends EventEmitter {
|
||||
if (this._webHook) {
|
||||
return;
|
||||
}
|
||||
this._webHook = new TelegramBotWebHook(this.token, this.options.webHook, this._processUpdate.bind(this));
|
||||
this._webHook = new TelegramBotWebHook(this.token, this.options.webHook, this.processUpdate.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -419,6 +333,93 @@ class TelegramBot extends EventEmitter {
|
||||
return this._request('getUpdates', { form });
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an update; emitting the proper events and executing regexp
|
||||
* callbacks. This method is useful should you be using a different
|
||||
* way to fetch updates, other than those provided by TelegramBot.
|
||||
* @param {Object} update
|
||||
* @see https://core.telegram.org/bots/api#update
|
||||
*/
|
||||
processUpdate(update) {
|
||||
debug('Process Update %j', update);
|
||||
const message = update.message;
|
||||
const editedMessage = update.edited_message;
|
||||
const channelPost = update.channel_post;
|
||||
const editedChannelPost = update.edited_channel_post;
|
||||
const inlineQuery = update.inline_query;
|
||||
const chosenInlineResult = update.chosen_inline_result;
|
||||
const callbackQuery = update.callback_query;
|
||||
|
||||
if (message) {
|
||||
debug('Process Update message %j', message);
|
||||
this.emit('message', message);
|
||||
const processMessageType = messageType => {
|
||||
if (message[messageType]) {
|
||||
debug('Emitting %s: %j', messageType, message);
|
||||
this.emit(messageType, message);
|
||||
}
|
||||
};
|
||||
TelegramBot.messageTypes.forEach(processMessageType);
|
||||
if (message.text) {
|
||||
debug('Text message');
|
||||
this._textRegexpCallbacks.some(reg => {
|
||||
debug('Matching %s with %s', message.text, reg.regexp);
|
||||
const result = reg.regexp.exec(message.text);
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
debug('Matches %s', reg.regexp);
|
||||
reg.callback(message, result);
|
||||
// returning truthy value exits .some
|
||||
return this.options.onlyFirstMatch;
|
||||
});
|
||||
}
|
||||
if (message.reply_to_message) {
|
||||
// Only callbacks waiting for this message
|
||||
this._onReplyToMessages.forEach(reply => {
|
||||
// Message from the same chat
|
||||
if (reply.chatId === message.chat.id) {
|
||||
// Responding to that message
|
||||
if (reply.messageId === message.reply_to_message.message_id) {
|
||||
// Resolve the promise
|
||||
reply.callback(message);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else if (editedMessage) {
|
||||
debug('Process Update edited_message %j', editedMessage);
|
||||
this.emit('edited_message', editedMessage);
|
||||
if (editedMessage.text) {
|
||||
this.emit('edited_message_text', editedMessage);
|
||||
}
|
||||
if (editedMessage.caption) {
|
||||
this.emit('edited_message_caption', editedMessage);
|
||||
}
|
||||
} else if (channelPost) {
|
||||
debug('Process Update channel_post %j', channelPost);
|
||||
this.emit('channel_post', channelPost);
|
||||
} else if (editedChannelPost) {
|
||||
debug('Process Update edited_channel_post %j', editedChannelPost);
|
||||
this.emit('edited_channel_post', editedChannelPost);
|
||||
if (editedChannelPost.text) {
|
||||
this.emit('edited_channel_post_text', editedChannelPost);
|
||||
}
|
||||
if (editedChannelPost.caption) {
|
||||
this.emit('edited_channel_post_caption', editedChannelPost);
|
||||
}
|
||||
} else if (inlineQuery) {
|
||||
debug('Process Update inline_query %j', inlineQuery);
|
||||
this.emit('inline_query', inlineQuery);
|
||||
} else if (chosenInlineResult) {
|
||||
debug('Process Update chosen_inline_result %j', chosenInlineResult);
|
||||
this.emit('chosen_inline_result', chosenInlineResult);
|
||||
} else if (callbackQuery) {
|
||||
debug('Process Update callback_query %j', callbackQuery);
|
||||
this.emit('callback_query', callbackQuery);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send text message.
|
||||
* @param {Number|String} chatId Unique identifier for the message recipient
|
||||
|
Loading…
x
Reference in New Issue
Block a user