move update type detecting to Update

This commit is contained in:
darksidecat 2021-08-20 11:44:03 +03:00
parent 18a93aab60
commit f58e3b154a
3 changed files with 66 additions and 43 deletions

1
CHANGES/669.misc Normal file
View file

@ -0,0 +1 @@
move update type detecting to Update

View file

@ -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 "

View file

@ -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