requested changes

This commit is contained in:
darksidecat 2021-08-21 20:46:38 +03:00
parent 009f731532
commit be2a681889
2 changed files with 34 additions and 19 deletions

View file

@ -188,7 +188,8 @@ class Dispatcher(Router):
:return:
"""
try:
update_type, event = update.content()
update_type = update.event_type
event = update.event
except UpdateTypeLookupError:
warnings.warn(
"Detected unknown update type.\n"

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Optional, Tuple
import functools
from typing import TYPE_CHECKING, Callable, Optional, TypeVar, cast
from .base import TelegramObject
@ -15,6 +16,12 @@ if TYPE_CHECKING: # pragma: no cover
from .pre_checkout_query import PreCheckoutQuery
from .shipping_query import ShippingQuery
T = TypeVar("T")
def lru_cache(func: Callable[..., T]) -> T:
return functools.lru_cache()(func) # type: ignore
class Update(TelegramObject):
"""
@ -54,44 +61,51 @@ class Update(TelegramObject):
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."""
def content(self) -> Tuple[str, TelegramObject]:
"""
Detect content type
def __hash__(self) -> int:
return hash((type(self), self.update_id))
Return update type and content
@property # type: ignore
@lru_cache
def event_type(self) -> str:
"""
Detect update type
If update type is unknown, raise UpdateTypeLookupError
:return:
"""
if self.message:
return "message", self.message
return "message"
if self.edited_message:
return "edited_message", self.edited_message
return "edited_message"
if self.channel_post:
return "channel_post", self.channel_post
return "channel_post"
if self.edited_channel_post:
return "edited_channel_post", self.edited_channel_post
return "edited_channel_post"
if self.inline_query:
return "inline_query", self.inline_query
return "inline_query"
if self.chosen_inline_result:
return "chosen_inline_result", self.chosen_inline_result
return "chosen_inline_result"
if self.callback_query:
return "callback_query", self.callback_query
return "callback_query"
if self.shipping_query:
return "shipping_query", self.shipping_query
return "shipping_query"
if self.pre_checkout_query:
return "pre_checkout_query", self.pre_checkout_query
return "pre_checkout_query"
if self.poll:
return "poll", self.poll
return "poll"
if self.poll_answer:
return "poll_answer", self.poll_answer
return "poll_answer"
if self.my_chat_member:
return "my_chat_member", self.my_chat_member
return "my_chat_member"
if self.chat_member:
return "chat_member", self.chat_member
return "chat_member"
raise UpdateTypeLookupError("Unknown update type")
@property
def event(self) -> TelegramObject:
return cast(TelegramObject, getattr(self, self.event_type))
class UpdateTypeLookupError(LookupError):
pass