From ca9a4440d1542267f07bc33fe9096a3965a7b6ef Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Thu, 23 Jan 2020 22:37:54 +0200 Subject: [PATCH] - Update docstring of KeyboardButton - Fix PollAnswer model and export it from `aiogram.types` - Fix dispatcher poll_answer handlers registration - Add actions to LoggingMiddleware --- aiogram/contrib/middlewares/logging.py | 14 ++++++++++++++ aiogram/dispatcher/dispatcher.py | 2 +- aiogram/types/__init__.py | 3 ++- aiogram/types/poll.py | 2 +- aiogram/types/reply_keyboard.py | 10 +++++++--- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/aiogram/contrib/middlewares/logging.py b/aiogram/contrib/middlewares/logging.py index 9f389b60..308d0e10 100644 --- a/aiogram/contrib/middlewares/logging.py +++ b/aiogram/contrib/middlewares/logging.py @@ -146,6 +146,20 @@ class LoggingMiddleware(BaseMiddleware): if timeout > 0: self.logger.info(f"Process update [ID:{update.update_id}]: [failed] (in {timeout} ms)") + async def on_pre_process_poll(self, poll, data): + self.logger.info(f"Received poll [ID:{poll.id}]") + + async def on_post_process_poll(self, poll, results, data): + self.logger.debug(f"{HANDLED_STR[bool(len(results))]} poll [ID:{poll.id}]") + + async def on_pre_process_poll_answer(self, poll_answer, data): + self.logger.info(f"Received poll answer [ID:{poll_answer.poll_id}] " + f"from user [ID:{poll_answer.user.id}]") + + async def on_post_process_poll_answer(self, poll_answer, results, data): + self.logger.debug(f"{HANDLED_STR[bool(len(results))]} poll answer [ID:{poll_answer.poll_id}] " + f"from user [ID:{poll_answer.user.id}]") + class LoggingFilter(logging.Filter): """ diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index af39bbc7..dc793b76 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -917,7 +917,7 @@ class Dispatcher(DataMixin, ContextInstanceMixin): filters_set = self.filters_factory.resolve(self.poll_answer_handlers, *custom_filters, **kwargs) - self.poll_handlers.register(self._wrap_async_task(callback, run_task), filters_set) + self.poll_answer_handlers.register(self._wrap_async_task(callback, run_task), filters_set) def poll_answer_handler(self, *custom_filters, run_task=None, **kwargs): """ diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index 37dc4b3e..018c593a 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -45,7 +45,7 @@ from .passport_element_error import PassportElementError, PassportElementErrorDa PassportElementErrorSelfie from .passport_file import PassportFile from .photo_size import PhotoSize -from .poll import PollOption, Poll +from .poll import PollOption, Poll, PollAnswer from .pre_checkout_query import PreCheckoutQuery from .reply_keyboard import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove from .response_parameters import ResponseParameters @@ -147,6 +147,7 @@ __all__ = ( 'PassportFile', 'PhotoSize', 'Poll', + 'PollAnswer', 'PollOption', 'PreCheckoutQuery', 'ReplyKeyboardMarkup', diff --git a/aiogram/types/poll.py b/aiogram/types/poll.py index 84fde48d..e5a485d4 100644 --- a/aiogram/types/poll.py +++ b/aiogram/types/poll.py @@ -22,7 +22,7 @@ class PollAnswer(base.TelegramObject): https://core.telegram.org/bots/api#pollanswer """ poll_id: base.String = fields.Field() - user: User = fields.Field() + user: User = fields.Field(base=User) option_ids: typing.List[base.Integer] = fields.ListField() diff --git a/aiogram/types/reply_keyboard.py b/aiogram/types/reply_keyboard.py index 7d2283ec..59804f08 100644 --- a/aiogram/types/reply_keyboard.py +++ b/aiogram/types/reply_keyboard.py @@ -93,9 +93,13 @@ class ReplyKeyboardMarkup(base.TelegramObject): class KeyboardButton(base.TelegramObject): """ - This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields request_contact, request_location, and request_poll are mutually exclusive. - Note: request_contact and request_location options will only work in Telegram versions released after 9 April, 2016. Older clients will ignore them. - Note: request_poll option will only work in Telegram versions released after 23 January, 2020. Older clients will receive unsupported message. + This object represents one button of the reply keyboard. + For simple text buttons String can be used instead of this object to specify text of the button. + Optional fields request_contact, request_location, and request_poll are mutually exclusive. + Note: request_contact and request_location options will only work in Telegram versions released after 9 April, 2016. + Older clients will ignore them. + Note: request_poll option will only work in Telegram versions released after 23 January, 2020. + Older clients will receive unsupported message. https://core.telegram.org/bots/api#keyboardbutton """