From 69e4ecc6061e22b121cbcd580268045aa39c0ffc Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 12 Apr 2021 00:32:40 +0300 Subject: [PATCH] #564: Added possibility to use `allowed_updates` argument in Polling mode --- aiogram/__init__.py | 2 +- aiogram/dispatcher/dispatcher.py | 14 +++++++++++--- aiogram/types/update.py | 8 ++++++++ aiogram/utils/executor.py | 21 ++++++++++++++++----- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/aiogram/__init__.py b/aiogram/__init__.py index 590afc50..455aebe8 100644 --- a/aiogram/__init__.py +++ b/aiogram/__init__.py @@ -43,5 +43,5 @@ __all__ = ( 'utils', ) -__version__ = '2.12.1' +__version__ = '2.12.2' __api_version__ = '5.1' diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index d471fe86..8231c4f7 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -340,7 +340,8 @@ class Dispatcher(DataMixin, ContextInstanceMixin): limit=None, reset_webhook=None, fast: typing.Optional[bool] = True, - error_sleep: int = 5): + error_sleep: int = 5, + allowed_updates: typing.Optional[typing.List[str]] = None): """ Start long-polling @@ -349,6 +350,8 @@ class Dispatcher(DataMixin, ContextInstanceMixin): :param limit: :param reset_webhook: :param fast: + :param error_sleep: + :param allowed_updates: :return: """ if self._polling: @@ -377,10 +380,15 @@ class Dispatcher(DataMixin, ContextInstanceMixin): while self._polling: try: with self.bot.request_timeout(request_timeout): - updates = await self.bot.get_updates(limit=limit, offset=offset, timeout=timeout) + updates = await self.bot.get_updates( + limit=limit, + offset=offset, + timeout=timeout, + allowed_updates=allowed_updates + ) except asyncio.CancelledError: break - except: + except Exception as e: log.exception('Cause exception while getting updates.') await asyncio.sleep(error_sleep) continue diff --git a/aiogram/types/update.py b/aiogram/types/update.py index c8c4b58d..7cf616bb 100644 --- a/aiogram/types/update.py +++ b/aiogram/types/update.py @@ -1,5 +1,7 @@ from __future__ import annotations +from functools import lru_cache + from . import base from . import fields from .callback_query import CallbackQuery @@ -72,3 +74,9 @@ class AllowedUpdates(helper.Helper): "Use `CHOSEN_INLINE_RESULT`", new_value_getter=lambda cls: cls.CHOSEN_INLINE_RESULT, ) + + @classmethod + @lru_cache(1) + def default(cls): + excluded = cls.CHAT_MEMBER + cls.MY_CHAT_MEMBER + return list(filter(lambda item: item not in excluded, cls.all())) diff --git a/aiogram/utils/executor.py b/aiogram/utils/executor.py index 35107975..c74827b0 100644 --- a/aiogram/utils/executor.py +++ b/aiogram/utils/executor.py @@ -2,7 +2,7 @@ import asyncio import datetime import functools import secrets -from typing import Callable, Union, Optional, Any +from typing import Callable, Union, Optional, Any, List from warnings import warn from aiohttp import web @@ -23,7 +23,8 @@ def _setup_callbacks(executor: 'Executor', on_startup=None, on_shutdown=None): def start_polling(dispatcher, *, loop=None, skip_updates=False, reset_webhook=True, - on_startup=None, on_shutdown=None, timeout=20, relax=0.1, fast=True): + on_startup=None, on_shutdown=None, timeout=20, relax=0.1, fast=True, + allowed_updates: Optional[List[str]] = None): """ Start bot in long-polling mode @@ -34,11 +35,20 @@ def start_polling(dispatcher, *, loop=None, skip_updates=False, reset_webhook=Tr :param on_startup: :param on_shutdown: :param timeout: + :param relax: + :param fast: + :param allowed_updates: """ executor = Executor(dispatcher, skip_updates=skip_updates, loop=loop) _setup_callbacks(executor, on_startup, on_shutdown) - executor.start_polling(reset_webhook=reset_webhook, timeout=timeout, relax=relax, fast=fast) + executor.start_polling( + reset_webhook=reset_webhook, + timeout=timeout, + relax=relax, + fast=fast, + allowed_updates=allowed_updates + ) def set_webhook(dispatcher: Dispatcher, webhook_path: str, *, loop: Optional[asyncio.AbstractEventLoop] = None, @@ -295,7 +305,8 @@ class Executor: self.set_webhook(webhook_path=webhook_path, request_handler=request_handler, route_name=route_name) self.run_app(**kwargs) - def start_polling(self, reset_webhook=None, timeout=20, relax=0.1, fast=True): + def start_polling(self, reset_webhook=None, timeout=20, relax=0.1, fast=True, + allowed_updates: Optional[List[str]] = None): """ Start bot in long-polling mode @@ -308,7 +319,7 @@ class Executor: try: loop.run_until_complete(self._startup_polling()) loop.create_task(self.dispatcher.start_polling(reset_webhook=reset_webhook, timeout=timeout, - relax=relax, fast=fast)) + relax=relax, fast=fast, allowed_updates=allowed_updates)) loop.run_forever() except (KeyboardInterrupt, SystemExit): # loop.stop()