#564: Added possibility to use allowed_updates argument in Polling mode

This commit is contained in:
Alex Root Junior 2021-04-12 00:32:40 +03:00
parent 79c59b34f9
commit 69e4ecc606
4 changed files with 36 additions and 9 deletions

View file

@ -43,5 +43,5 @@ __all__ = (
'utils',
)
__version__ = '2.12.1'
__version__ = '2.12.2'
__api_version__ = '5.1'

View file

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

View file

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

View file

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