diff --git a/aiogram/bot.py b/aiogram/bot.py index af522a9b..5741bbb0 100644 --- a/aiogram/bot.py +++ b/aiogram/bot.py @@ -6,6 +6,7 @@ import aiohttp from . import api from .api import ApiMethods from .types.chat import Chat +from .types.update import Update from .types.user import User @@ -53,3 +54,27 @@ class AIOGramBot: payload = {'chat_id': chat_id} raw = await self.request(ApiMethods.GET_CHAT, payload) return self._prepare_object(Chat.de_json(raw)) + + async def get_updates(self, offset=None, limit=None, timeout=None, allowed_updates=None): + """ + offset Integer Optional Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will forgotten. + limit Integer Optional Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100. + timeout Integer Optional Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling should be used for testing purposes only. + allowed_updates Array of String Optional List the types of updates you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all updates regardless of type (default). If not specified, the previous setting will be used. + + Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time. + :return: + """ + payload = {} + + if offset: + payload['offset'] = offset + if limit: + payload['limit'] = limit + if timeout: + payload['timeout'] = timeout + if allowed_updates: + payload['allowed_updates'] = allowed_updates + + raw = await self.request(ApiMethods.GET_UPDATES, payload) + return [Update.de_json(raw_update) for raw_update in raw] diff --git a/aiogram/types/update.py b/aiogram/types/update.py new file mode 100644 index 00000000..564bba3a --- /dev/null +++ b/aiogram/types/update.py @@ -0,0 +1,56 @@ +from aiogram.types import Deserializable +from aiogram.types.message import Message + + +class Update(Deserializable): + def __init__(self, data, update_id, message, edited_message, channel_post, edited_channel_post, inline_query, + chosen_inline_result, callback_query, shipping_query, pre_checkout_query): + self.data = data + + self.update_id = update_id + self.message = message + self.edited_message = edited_message + self.channel_post = channel_post + self.edited_channel_post = edited_channel_post + self.inline_query = inline_query + self.chosen_inline_result = chosen_inline_result + self.callback_query = callback_query + self.shipping_query = shipping_query + self.pre_checkout_query = pre_checkout_query + + @classmethod + def _parse_message(cls, message): + return Message.de_json(message) if message else None + + @classmethod + def de_json(cls, data): + """ + update_id Integer The update‘s unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you’re using Webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. + message Message Optional. New incoming message of any kind — text, photo, sticker, etc. + edited_message Message Optional. New version of a message that is known to the bot and was edited + channel_post Message Optional. New incoming channel post of any kind — text, photo, sticker, etc. + edited_channel_post Message Optional. New version of a channel post that is known to the bot and was edited + inline_query InlineQuery Optional. New incoming inline query + chosen_inline_result ChosenInlineResult Optional. The result of an inline query that was chosen by a user and sent to their chat partner. + callback_query CallbackQuery Optional. New incoming callback query + shipping_query ShippingQuery Optional. New incoming shipping query. Only for invoices with flexible price + pre_checkout_query PreCheckoutQuery Optional. New incoming pre-checkout query. Contains full information about checkout + :param data: + :return: + """ + data = cls.check_json(data) + + update_id = data.get('update_id') + message = cls._parse_message(data.get('message')) + edited_message = cls._parse_message(data.get('edited_message')) + channel_post = cls._parse_message(data.get('channel_post')) + edited_channel_post = cls._parse_message(data.get('edited_channel_post')) + + inline_query = data.get('inline_query') + chosen_inline_result = data.get('chosen_inline_result') + callback_query = data.get('callback_query') + shipping_query = data.get('shipping_query') + pre_checkout_query = data.get('pre_checkout_query') + + return Update(data, update_id, message, edited_message, channel_post, edited_channel_post, inline_query, + chosen_inline_result, callback_query, shipping_query, pre_checkout_query)