From f58e3b154ad957a9b75f7bd4a8b72370a61cb4f3 Mon Sep 17 00:00:00 2001 From: darksidecat Date: Fri, 20 Aug 2021 11:44:03 +0300 Subject: [PATCH] move update type detecting to Update --- CHANGES/669.misc | 1 + aiogram/dispatcher/dispatcher.py | 47 +++--------------------- aiogram/types/update.py | 61 +++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 CHANGES/669.misc diff --git a/CHANGES/669.misc b/CHANGES/669.misc new file mode 100644 index 00000000..8f910d20 --- /dev/null +++ b/CHANGES/669.misc @@ -0,0 +1 @@ +move update type detecting to Update diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 2f4bb1ba..22c8d2ac 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -9,7 +9,8 @@ from typing import Any, AsyncGenerator, Dict, List, Optional, Union from .. import loggers from ..client.bot import Bot from ..methods import GetUpdates, TelegramMethod -from ..types import TelegramObject, Update, User +from ..types import Update, User +from ..types.update import UpdateTypeLookupError from ..utils.backoff import Backoff, BackoffConfig from ..utils.exceptions.base import TelegramAPIError from ..utils.exceptions.network import NetworkError @@ -186,47 +187,9 @@ class Dispatcher(Router): :param kwargs: :return: """ - event: TelegramObject - if update.message: - update_type = "message" - event = update.message - elif update.edited_message: - update_type = "edited_message" - event = update.edited_message - elif update.channel_post: - update_type = "channel_post" - event = update.channel_post - elif update.edited_channel_post: - update_type = "edited_channel_post" - event = update.edited_channel_post - elif update.inline_query: - update_type = "inline_query" - event = update.inline_query - elif update.chosen_inline_result: - update_type = "chosen_inline_result" - event = update.chosen_inline_result - elif update.callback_query: - update_type = "callback_query" - event = update.callback_query - elif update.shipping_query: - update_type = "shipping_query" - event = update.shipping_query - elif update.pre_checkout_query: - update_type = "pre_checkout_query" - event = update.pre_checkout_query - elif update.poll: - update_type = "poll" - event = update.poll - elif update.poll_answer: - update_type = "poll_answer" - event = update.poll_answer - elif update.my_chat_member: - update_type = "my_chat_member" - event = update.my_chat_member - elif update.chat_member: - update_type = "chat_member" - event = update.chat_member - else: + try: + update_type, event = update.event + except UpdateTypeLookupError: warnings.warn( "Detected unknown update type.\n" "Seems like Telegram Bot API was updated and you have " diff --git a/aiogram/types/update.py b/aiogram/types/update.py index 3e43a316..b1b55d9a 100644 --- a/aiogram/types/update.py +++ b/aiogram/types/update.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Optional, Tuple from .base import TelegramObject @@ -53,3 +53,62 @@ class Update(TelegramObject): """*Optional*. The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.""" chat_member: Optional[ChatMemberUpdated] = None """*Optional*. A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify 'chat_member' in the list of *allowed_updates* to receive these updates.""" + + @property + def event(self) -> Tuple[str, TelegramObject]: + """ + Detect content type + + Return update type and event + If update type unknown raise UpdateTypeLookupError + + :return: + """ + event: TelegramObject + if self.message: + update_type = "message" + event = self.message + elif self.edited_message: + update_type = "edited_message" + event = self.edited_message + elif self.channel_post: + update_type = "channel_post" + event = self.channel_post + elif self.edited_channel_post: + update_type = "edited_channel_post" + event = self.edited_channel_post + elif self.inline_query: + update_type = "inline_query" + event = self.inline_query + elif self.chosen_inline_result: + update_type = "chosen_inline_result" + event = self.chosen_inline_result + elif self.callback_query: + update_type = "callback_query" + event = self.callback_query + elif self.shipping_query: + update_type = "shipping_query" + event = self.shipping_query + elif self.pre_checkout_query: + update_type = "pre_checkout_query" + event = self.pre_checkout_query + elif self.poll: + update_type = "poll" + event = self.poll + elif self.poll_answer: + update_type = "poll_answer" + event = self.poll_answer + elif self.my_chat_member: + update_type = "my_chat_member" + event = self.my_chat_member + elif self.chat_member: + update_type = "chat_member" + event = self.chat_member + else: + raise UpdateTypeLookupError("Unknown update type") + + return update_type, event + + +class UpdateTypeLookupError(LookupError): + pass