diff --git a/aiogram/utils/executor.py b/aiogram/utils/executor.py index 0679de8c..7188773c 100644 --- a/aiogram/utils/executor.py +++ b/aiogram/utils/executor.py @@ -252,7 +252,7 @@ class Executor: pass finally: loop.run_until_complete(self._shutdown_polling()) - log.warning("Goodbye!") + log.warning("Goodbye!") def start(self, future): """ @@ -275,7 +275,7 @@ class Executor: loop.stop() finally: loop.run_until_complete(self._shutdown_polling()) - log.warning("Goodbye!") + log.warning("Goodbye!") return result async def _skip_updates(self): diff --git a/docs/source/examples/adwanced_executor_example.rst b/docs/source/examples/adwanced_executor_example.rst index 57925c0d..88011e23 100644 --- a/docs/source/examples/adwanced_executor_example.rst +++ b/docs/source/examples/adwanced_executor_example.rst @@ -1,10 +1,11 @@ -.. Autogenerated file at 2018-09-08 02:07:37.572483 +.. Autogenerated file at 2018-10-28 19:31:48.335963 ========================= Adwanced executor example ========================= !/usr/bin/env python3 +**This example is outdated** In this example used ArgumentParser for configuring Your bot. Provided to start bot with webhook: python adwanced_executor_example.py \ @@ -18,10 +19,10 @@ python adwanced_executor_example.py --token TOKEN_HERE So... In this example found small trouble: can't get bot instance in handlers. If you want to automatic change getting updates method use executor utils (from aiogram.utils.executor) -TODO: Move token to environment variables. + TODO: Move token to environment variables. .. literalinclude:: ../../../examples/adwanced_executor_example.py :caption: adwanced_executor_example.py :language: python :linenos: - :lines: 24- + :lines: 25- diff --git a/docs/source/examples/webhook_example.rst b/docs/source/examples/webhook_example.rst index 4979087f..48de8622 100644 --- a/docs/source/examples/webhook_example.rst +++ b/docs/source/examples/webhook_example.rst @@ -1,10 +1,13 @@ -.. Autogenerated file at 2018-09-08 02:07:37.587860 +.. Autogenerated file at 2018-10-28 19:31:48.341172 =============== Webhook example =============== +Example outdated + .. literalinclude:: ../../../examples/webhook_example.py :caption: webhook_example.py :language: python :linenos: + :lines: 5- diff --git a/docs/source/migration_1_to_2.rst b/docs/source/migration_1_to_2.rst index beda3737..eafc2561 100644 --- a/docs/source/migration_1_to_2.rst +++ b/docs/source/migration_1_to_2.rst @@ -23,10 +23,7 @@ Changelog - Used `aiohttp_socks` instead of `aiosocksy` for Socks4/5 proxy; - `types.ContentType` was divided to `types.ContentType` and `types.ContentTypes`; - Allowed to use rapidjson instead of ujson/json; -- (**in process**) Implemented utils for Telegram Passport; -- (**in process**) Webhook security improvements; -- (**in process**) Updated examples. - +- `.current()` method in bot and dispatcher objects was renamed to `get_current()`; Instructions ============ @@ -58,6 +55,7 @@ Now `func` keyword argument can't be used for passing filters to the list of fil @dp.message_handler(types.ChatType.is_private, my_filter) async def ... +(func filter is still available until v2.1) Filters factory ~~~~~~~~~~~~~~~ @@ -97,6 +95,24 @@ Passing data from filters as keyword arguments to the handlers You can pass any data from any filter to the handler by returning :obj:`dict` If any key from the received dictionary not in the handler specification the key will be skipped and and will be unavailable from the handler +Before (<=v1.4) + +.. code-block:: python + + async def my_filter(message: types.Message): + # do something here + message.conf['foo'] = 'foo' + message.conf['bar'] = 42 + return True + + @dp.message_handler(func=my_filter) + async def my_message_handler(message: types.Message): + bar = message.conf["bar"] + await message.reply(f'bar = {bar}') + + +Now (v2.0) + .. code-block:: python async def my_filter(message: types.Message): @@ -107,6 +123,7 @@ If any key from the received dictionary not in the handler specification the key async def my_message_handler(message: types.Message, bar: int): await message.reply(f'bar = {bar}') + Other ~~~~~ Filters can also be used as logical expressions: @@ -156,6 +173,9 @@ Usage: return await message.reply(f"Counter: {proxy['counter']}") +This method is not recommended in high-load solutions in reason named "race-condition". + + File uploading mechanism ------------------------ Fixed uploading files. Removed `BaseBot.send_file` method. This allowed to send the `thumb` field. @@ -183,6 +203,8 @@ I18n Middleware --------------- You can internalize your bot by following next steps: +(Code snippets in this example related with `examples/i18n_example.py`) + First usage ~~~~~~~~~~~ 1. Extract texts diff --git a/examples/adwanced_executor_example.py b/examples/adwanced_executor_example.py index eb4694a7..521783f1 100644 --- a/examples/adwanced_executor_example.py +++ b/examples/adwanced_executor_example.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """ +**This example is outdated** In this example used ArgumentParser for configuring Your bot. Provided to start bot with webhook: diff --git a/examples/echo_bot.py b/examples/echo_bot.py index 617dbad7..27dc70d9 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -1,19 +1,27 @@ -import asyncio +""" +This is a echo bot. +It echoes any incoming text messages. +""" + import logging -from aiogram import Bot, types, Dispatcher, executor +from aiogram import Bot, Dispatcher, executor, types API_TOKEN = 'BOT TOKEN HERE' +# Configure logging logging.basicConfig(level=logging.INFO) -loop = asyncio.get_event_loop() -bot = Bot(token=API_TOKEN, loop=loop) +# Initialize bot and dispatcher +bot = Bot(token=API_TOKEN) dp = Dispatcher(bot) @dp.message_handler(commands=['start', 'help']) async def send_welcome(message: types.Message): + """ + This handler will be called when client send `/start` or `/help` commands. + """ await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.") @@ -30,4 +38,4 @@ async def echo(message: types.Message): if __name__ == '__main__': - executor.start_polling(dp, loop=loop, skip_updates=True) + executor.start_polling(dp, skip_updates=True) diff --git a/examples/regexp_commands_filter_example.py b/examples/regexp_commands_filter_example.py index 3cd859db..86ccba55 100644 --- a/examples/regexp_commands_filter_example.py +++ b/examples/regexp_commands_filter_example.py @@ -7,8 +7,7 @@ dp = Dispatcher(bot) @dp.message_handler(filters.RegexpCommandsFilter(regexp_commands=['item_([0-9]*)'])) -async def send_welcome(message: types.Message): - regexp_command = message.conf['regexp_command'] +async def send_welcome(message: types.Message, regexp_command): await message.reply("You have requested an item with number: {}".format(regexp_command.group(1))) diff --git a/examples/webhook_example.py b/examples/webhook_example.py index a1b48c0f..fb0046ef 100644 --- a/examples/webhook_example.py +++ b/examples/webhook_example.py @@ -1,3 +1,7 @@ +""" +Example outdated +""" + import asyncio import ssl import sys @@ -5,7 +9,7 @@ import sys from aiohttp import web import aiogram -from aiogram import Bot, types, Version +from aiogram import Bot, types from aiogram.contrib.fsm_storage.memory import MemoryStorage from aiogram.dispatcher import Dispatcher from aiogram.dispatcher.webhook import get_new_configured_app, SendMessage diff --git a/examples/webhook_example_2.py b/examples/webhook_example_2.py index 1a6fc2a1..75b29c75 100644 --- a/examples/webhook_example_2.py +++ b/examples/webhook_example_2.py @@ -31,13 +31,11 @@ async def echo(message: types.Message): async def on_startup(dp): await bot.set_webhook(WEBHOOK_URL) # insert code here to run it after start - # async def on_shutdown(dp): # insert code here to run it before shutdown - # - await bot.close() + pass if __name__ == '__main__':