diff --git a/aiogram/dispatcher/__init__.py b/aiogram/dispatcher/__init__.py index 5181a7f8..afb4ecc7 100644 --- a/aiogram/dispatcher/__init__.py +++ b/aiogram/dispatcher/__init__.py @@ -11,12 +11,13 @@ from .webhook import BaseResponse from ..bot import Bot from ..types.message import ContentType from ..utils import context +from ..utils.deprecated import deprecated from ..utils.exceptions import NetworkError, TelegramAPIError log = logging.getLogger(__name__) MODE = 'MODE' -LONG_POOLING = 'long-pooling' +LONG_POLLING = 'long-polling' UPDATE_OBJECT = 'update_object' @@ -30,7 +31,7 @@ class Dispatcher: """ def __init__(self, bot, loop=None, storage: typing.Optional[BaseStorage] = None, - run_tasks_by_default: bool=False): + run_tasks_by_default: bool = False): if loop is None: loop = bot.loop if storage is None: @@ -58,10 +59,10 @@ class Dispatcher: self.errors_handlers = Handler(self, once=False) - self._pooling = False + self._polling = False def __del__(self): - self._pooling = False + self._polling = False @property def data(self): @@ -196,9 +197,20 @@ class Dispatcher: return await self.bot.delete_webhook() - async def start_pooling(self, timeout=20, relax=0.1, limit=None, reset_webhook=True): + @deprecated('The old method was renamed to `start_polling`') + async def start_pooling(self, *args, **kwargs): """ - Start long-pooling + Start long-lopping + + :param args: + :param kwargs: + :return: + """ + return await self.start_polling(*args, **kwargs) + + async def start_polling(self, timeout=20, relax=0.1, limit=None, reset_webhook=True): + """ + Start long-polling :param timeout: :param relax: @@ -206,21 +218,21 @@ class Dispatcher: :param reset_webhook: :return: """ - if self._pooling: - raise RuntimeError('Pooling already started') + if self._polling: + raise RuntimeError('Polling already started') - log.info('Start pooling.') + log.info('Start polling.') - context.set_value(MODE, LONG_POOLING) + context.set_value(MODE, LONG_POLLING) context.set_value('dispatcher', self) context.set_value('bot', self.bot) if reset_webhook: await self.reset_webhook(check=True) - self._pooling = True + self._polling = True offset = None - while self._pooling: + while self._polling: try: updates = await self.bot.get_updates(limit=limit, offset=offset, timeout=timeout) except NetworkError: @@ -232,16 +244,16 @@ class Dispatcher: log.info("Received {0} updates.".format(len(updates))) offset = updates[-1].update_id + 1 - self.loop.create_task(self._process_pooling_updates(updates)) + self.loop.create_task(self._process_polling_updates(updates)) if relax: await asyncio.sleep(relax) - log.warning('Pooling is stopped.') + log.warning('Polling is stopped.') - async def _process_pooling_updates(self, updates): + async def _process_polling_updates(self, updates): """ - Process updates received from long-pooling. + Process updates received from long-polling. :param updates: list of updates. """ @@ -257,22 +269,30 @@ class Dispatcher: except TelegramAPIError: log.exception('Cause exception while processing updates.') + @deprecated('The old method was renamed to `stop_polling`') def stop_pooling(self): + return self.stop_polling() + + def stop_polling(self): """ - Break long-pooling process. + Break long-polling process. :return: """ - if self._pooling: - log.info('Stop pooling.') - self._pooling = False + if self._polling: + log.info('Stop polling.') + self._polling = False + @deprecated('The old method was renamed to `is_polling`') def is_pooling(self): + return self.is_polling() + + def is_polling(self): """ - Check pooling is enabled? + Check polling is enabled? :return: """ - return self._pooling + return self._polling def register_message_handler(self, callback, *, commands=None, regexp=None, content_types=None, func=None, state=None, custom_filters=None, run_task=None, **kwargs): diff --git a/aiogram/utils/executor.py b/aiogram/utils/executor.py index b04af683..45fdf7da 100644 --- a/aiogram/utils/executor.py +++ b/aiogram/utils/executor.py @@ -1,11 +1,10 @@ -import asyncio - from aiohttp import web -from aiogram.bot.api import log -from aiogram.dispatcher import Dispatcher -from aiogram.dispatcher.webhook import BOT_DISPATCHER_KEY, get_new_configured_app -from aiogram.utils import context +from . import context +from .deprecated import deprecated +from ..bot.api import log +from ..dispatcher import Dispatcher +from ..dispatcher.webhook import BOT_DISPATCHER_KEY, get_new_configured_app async def _startup(dispatcher: Dispatcher, skip_updates=False, callback=None): @@ -33,8 +32,8 @@ async def _shutdown(dispatcher: Dispatcher, callback=None): if callable(callback): await callback(dispatcher) - if dispatcher.is_pooling(): - dispatcher.stop_pooling() + if dispatcher.is_polling(): + dispatcher.stop_polling() await dispatcher.storage.close() await dispatcher.storage.wait_closed() @@ -46,8 +45,13 @@ async def _wh_shutdown(app): await _shutdown(dispatcher, callback=callback) -def start_pooling(dispatcher, *, loop=None, skip_updates=False, on_startup=None, on_shutdown=None): - log.warning('Start bot with long-pooling.') +@deprecated('The old function was renamed to `start_polling`') +def start_pooling(*args, **kwargs): + return start_polling(*args, **kwargs) + + +def start_polling(dispatcher, *, loop=None, skip_updates=False, on_startup=None, on_shutdown=None): + log.warning('Start bot with long-polling.') if loop is None: loop = dispatcher.loop @@ -55,7 +59,7 @@ def start_pooling(dispatcher, *, loop=None, skip_updates=False, on_startup=None, try: loop.run_until_complete(_startup(dispatcher, skip_updates=skip_updates, callback=on_startup)) - loop.create_task(dispatcher.start_pooling(reset_webhook=True)) + loop.create_task(dispatcher.start_polling(reset_webhook=True)) loop.run_forever() except (KeyboardInterrupt, SystemExit): pass diff --git a/docs/source/quick_start.rst b/docs/source/quick_start.rst index ee241a38..ec0bb1dc 100644 --- a/docs/source/quick_start.rst +++ b/docs/source/quick_start.rst @@ -29,12 +29,12 @@ And next: all bots is needed command for starting interaction with bot. Registe async def send_welcome(message: types.Message): await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.") -And last step - run long pooling. +And last step - run long polling. .. code-block:: python3 if __name__ == '__main__': - executor.start_pooling(dp, on_startup=startup) + executor.start_polling(dp, on_startup=startup) Summary ------- @@ -49,5 +49,4 @@ Summary dp = Dispatcher(bot) if __name__ == '__main__': - executor.start_pooling(dp) - + executor.start_polling(dp) diff --git a/examples/adwanced_executor_example.py b/examples/adwanced_executor_example.py index 16194059..9dd873b8 100644 --- a/examples/adwanced_executor_example.py +++ b/examples/adwanced_executor_example.py @@ -10,7 +10,7 @@ Provided to start bot with webhook: --host-name example.com \ --webhook-port 443 -Or long pooling: +Or long polling: python adwanced_executor_example.py --token TOKEN_HERE So... In this example found small trouble: @@ -29,7 +29,7 @@ import sys from aiogram import Bot from aiogram.dispatcher import Dispatcher from aiogram.dispatcher.webhook import * -from aiogram.utils.executor import start_pooling, start_webhook +from aiogram.utils.executor import start_polling, start_webhook logging.basicConfig(level=logging.INFO) @@ -120,7 +120,7 @@ def main(arguments): on_shutdown=on_shutdown, host=host, port=port, path=sock, ssl_context=ssl_context) else: - start_pooling(dispatcher, on_startup=on_startup, on_shutdown=on_shutdown) + start_polling(dispatcher, on_startup=on_startup, on_shutdown=on_shutdown) if __name__ == '__main__': diff --git a/examples/check_user_language.py b/examples/check_user_language.py index 091e2d0f..1e1046a9 100644 --- a/examples/check_user_language.py +++ b/examples/check_user_language.py @@ -8,7 +8,7 @@ import logging from aiogram import Bot, types from aiogram.dispatcher import Dispatcher from aiogram.types import ParseMode -from aiogram.utils.executor import start_pooling +from aiogram.utils.executor import start_polling from aiogram.utils.markdown import * API_TOKEN = 'BOT TOKEN HERE' @@ -33,11 +33,5 @@ async def check_language(message: types.Message): sep='\n'), parse_mode=ParseMode.MARKDOWN) -async def main(): - count = await dp.skip_updates() - print(f"Skipped {count} updates.") - await dp.start_pooling() - - if __name__ == '__main__': - start_pooling(dp, loop=loop, skip_updates=True) + start_polling(dp, loop=loop, skip_updates=True) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 8452b8a5..7f4b0324 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -3,7 +3,7 @@ import logging from aiogram import Bot, types from aiogram.dispatcher import Dispatcher -from aiogram.utils.executor import start_pooling +from aiogram.utils.executor import start_polling API_TOKEN = 'BOT TOKEN HERE' @@ -32,7 +32,7 @@ async def echo(message: types.Message): if __name__ == '__main__': - start_pooling(dp, loop=loop, skip_updates=True) + start_polling(dp, loop=loop, skip_updates=True) # Also you can use another execution method # >>> try: diff --git a/examples/finite_state_machine_example.py b/examples/finite_state_machine_example.py index 102b1ef3..d416c714 100644 --- a/examples/finite_state_machine_example.py +++ b/examples/finite_state_machine_example.py @@ -128,4 +128,4 @@ async def shutdown(dispatcher: Dispatcher): if __name__ == '__main__': - executor.start_pooling(dp, loop=loop, skip_updates=True, on_shutdown=shutdown) + executor.start_polling(dp, loop=loop, skip_updates=True, on_shutdown=shutdown) diff --git a/examples/inline_bot.py b/examples/inline_bot.py index d60bfe66..0fad8cd3 100644 --- a/examples/inline_bot.py +++ b/examples/inline_bot.py @@ -3,7 +3,7 @@ import logging from aiogram import Bot, types from aiogram.dispatcher import Dispatcher -from aiogram.utils.executor import start_pooling +from aiogram.utils.executor import start_polling API_TOKEN = 'BOT TOKEN HERE' @@ -22,4 +22,4 @@ async def inline_echo(inline_query: types.InlineQuery): if __name__ == '__main__': - start_pooling(dp, loop=loop, skip_updates=True) + start_polling(dp, loop=loop, skip_updates=True) diff --git a/examples/payments.py b/examples/payments.py index 2477998a..997d7057 100644 --- a/examples/payments.py +++ b/examples/payments.py @@ -95,4 +95,4 @@ async def got_payment(message: types.Message): if __name__ == '__main__': - executor.start_pooling(dp, loop=loop) + executor.start_polling(dp, loop=loop) diff --git a/examples/proxy_and_emojize.py b/examples/proxy_and_emojize.py index 362ecff5..e9c122c0 100644 --- a/examples/proxy_and_emojize.py +++ b/examples/proxy_and_emojize.py @@ -1,14 +1,12 @@ import asyncio import logging -import aiohttp - from aiogram import Bot, types from aiogram.dispatcher import Dispatcher from aiogram.types import ParseMode from aiogram.utils.emoji import emojize -from aiogram.utils.executor import start_pooling -from aiogram.utils.markdown import text, bold, italic, code +from aiogram.utils.executor import start_polling +from aiogram.utils.markdown import bold, code, italic, text # Configure bot here API_TOKEN = 'BOT TOKEN HERE' @@ -60,11 +58,5 @@ async def cmd_start(message: types.Message): # For example emojize('Moon face :new_moon_face:') is represents to 'Moon face 🌚' -async def main(): - count = await dp.skip_updates() - print(f"Skipped {count} updates.") - await dp.start_pooling() - - if __name__ == '__main__': - start_pooling(dp, loop=loop, skip_updates=True) + start_polling(dp, loop=loop, skip_updates=True)