mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
Bump examples, docs and executor (nothing)
This commit is contained in:
parent
21908739e9
commit
9f13e11e6b
9 changed files with 57 additions and 21 deletions
|
|
@ -252,7 +252,7 @@ class Executor:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
loop.run_until_complete(self._shutdown_polling())
|
loop.run_until_complete(self._shutdown_polling())
|
||||||
log.warning("Goodbye!")
|
log.warning("Goodbye!")
|
||||||
|
|
||||||
def start(self, future):
|
def start(self, future):
|
||||||
"""
|
"""
|
||||||
|
|
@ -275,7 +275,7 @@ class Executor:
|
||||||
loop.stop()
|
loop.stop()
|
||||||
finally:
|
finally:
|
||||||
loop.run_until_complete(self._shutdown_polling())
|
loop.run_until_complete(self._shutdown_polling())
|
||||||
log.warning("Goodbye!")
|
log.warning("Goodbye!")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def _skip_updates(self):
|
async def _skip_updates(self):
|
||||||
|
|
|
||||||
|
|
@ -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
|
Adwanced executor example
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
!/usr/bin/env python3
|
!/usr/bin/env python3
|
||||||
|
**This example is outdated**
|
||||||
In this example used ArgumentParser for configuring Your bot.
|
In this example used ArgumentParser for configuring Your bot.
|
||||||
Provided to start bot with webhook:
|
Provided to start bot with webhook:
|
||||||
python adwanced_executor_example.py \
|
python adwanced_executor_example.py \
|
||||||
|
|
@ -18,10 +19,10 @@ python adwanced_executor_example.py --token TOKEN_HERE
|
||||||
So... In this example found small trouble:
|
So... In this example found small trouble:
|
||||||
can't get bot instance in handlers.
|
can't get bot instance in handlers.
|
||||||
If you want to automatic change getting updates method use executor utils (from aiogram.utils.executor)
|
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
|
.. literalinclude:: ../../../examples/adwanced_executor_example.py
|
||||||
:caption: adwanced_executor_example.py
|
:caption: adwanced_executor_example.py
|
||||||
:language: python
|
:language: python
|
||||||
:linenos:
|
:linenos:
|
||||||
:lines: 24-
|
:lines: 25-
|
||||||
|
|
|
||||||
|
|
@ -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
|
Webhook example
|
||||||
===============
|
===============
|
||||||
|
|
||||||
|
Example outdated
|
||||||
|
|
||||||
.. literalinclude:: ../../../examples/webhook_example.py
|
.. literalinclude:: ../../../examples/webhook_example.py
|
||||||
:caption: webhook_example.py
|
:caption: webhook_example.py
|
||||||
:language: python
|
:language: python
|
||||||
:linenos:
|
:linenos:
|
||||||
|
:lines: 5-
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,7 @@ Changelog
|
||||||
- Used `aiohttp_socks` instead of `aiosocksy` for Socks4/5 proxy;
|
- Used `aiohttp_socks` instead of `aiosocksy` for Socks4/5 proxy;
|
||||||
- `types.ContentType` was divided to `types.ContentType` and `types.ContentTypes`;
|
- `types.ContentType` was divided to `types.ContentType` and `types.ContentTypes`;
|
||||||
- Allowed to use rapidjson instead of ujson/json;
|
- Allowed to use rapidjson instead of ujson/json;
|
||||||
- (**in process**) Implemented utils for Telegram Passport;
|
- `.current()` method in bot and dispatcher objects was renamed to `get_current()`;
|
||||||
- (**in process**) Webhook security improvements;
|
|
||||||
- (**in process**) Updated examples.
|
|
||||||
|
|
||||||
|
|
||||||
Instructions
|
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)
|
@dp.message_handler(types.ChatType.is_private, my_filter)
|
||||||
async def ...
|
async def ...
|
||||||
|
|
||||||
|
(func filter is still available until v2.1)
|
||||||
|
|
||||||
Filters factory
|
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`
|
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
|
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
|
.. code-block:: python
|
||||||
|
|
||||||
async def my_filter(message: types.Message):
|
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):
|
async def my_message_handler(message: types.Message, bar: int):
|
||||||
await message.reply(f'bar = {bar}')
|
await message.reply(f'bar = {bar}')
|
||||||
|
|
||||||
|
|
||||||
Other
|
Other
|
||||||
~~~~~
|
~~~~~
|
||||||
Filters can also be used as logical expressions:
|
Filters can also be used as logical expressions:
|
||||||
|
|
@ -156,6 +173,9 @@ Usage:
|
||||||
return await message.reply(f"Counter: {proxy['counter']}")
|
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
|
File uploading mechanism
|
||||||
------------------------
|
------------------------
|
||||||
Fixed uploading files. Removed `BaseBot.send_file` method. This allowed to send the `thumb` field.
|
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:
|
You can internalize your bot by following next steps:
|
||||||
|
|
||||||
|
(Code snippets in this example related with `examples/i18n_example.py`)
|
||||||
|
|
||||||
First usage
|
First usage
|
||||||
~~~~~~~~~~~
|
~~~~~~~~~~~
|
||||||
1. Extract texts
|
1. Extract texts
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
|
**This example is outdated**
|
||||||
In this example used ArgumentParser for configuring Your bot.
|
In this example used ArgumentParser for configuring Your bot.
|
||||||
|
|
||||||
Provided to start bot with webhook:
|
Provided to start bot with webhook:
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,27 @@
|
||||||
import asyncio
|
"""
|
||||||
|
This is a echo bot.
|
||||||
|
It echoes any incoming text messages.
|
||||||
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiogram import Bot, types, Dispatcher, executor
|
from aiogram import Bot, Dispatcher, executor, types
|
||||||
|
|
||||||
API_TOKEN = 'BOT TOKEN HERE'
|
API_TOKEN = 'BOT TOKEN HERE'
|
||||||
|
|
||||||
|
# Configure logging
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
# Initialize bot and dispatcher
|
||||||
bot = Bot(token=API_TOKEN, loop=loop)
|
bot = Bot(token=API_TOKEN)
|
||||||
dp = Dispatcher(bot)
|
dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler(commands=['start', 'help'])
|
@dp.message_handler(commands=['start', 'help'])
|
||||||
async def send_welcome(message: types.Message):
|
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.")
|
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -30,4 +38,4 @@ async def echo(message: types.Message):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
executor.start_polling(dp, loop=loop, skip_updates=True)
|
executor.start_polling(dp, skip_updates=True)
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,7 @@ dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler(filters.RegexpCommandsFilter(regexp_commands=['item_([0-9]*)']))
|
@dp.message_handler(filters.RegexpCommandsFilter(regexp_commands=['item_([0-9]*)']))
|
||||||
async def send_welcome(message: types.Message):
|
async def send_welcome(message: types.Message, regexp_command):
|
||||||
regexp_command = message.conf['regexp_command']
|
|
||||||
await message.reply("You have requested an item with number: {}".format(regexp_command.group(1)))
|
await message.reply("You have requested an item with number: {}".format(regexp_command.group(1)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
"""
|
||||||
|
Example outdated
|
||||||
|
"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
|
|
@ -5,7 +9,7 @@ import sys
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
import aiogram
|
import aiogram
|
||||||
from aiogram import Bot, types, Version
|
from aiogram import Bot, types
|
||||||
from aiogram.contrib.fsm_storage.memory import MemoryStorage
|
from aiogram.contrib.fsm_storage.memory import MemoryStorage
|
||||||
from aiogram.dispatcher import Dispatcher
|
from aiogram.dispatcher import Dispatcher
|
||||||
from aiogram.dispatcher.webhook import get_new_configured_app, SendMessage
|
from aiogram.dispatcher.webhook import get_new_configured_app, SendMessage
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,11 @@ async def echo(message: types.Message):
|
||||||
async def on_startup(dp):
|
async def on_startup(dp):
|
||||||
await bot.set_webhook(WEBHOOK_URL)
|
await bot.set_webhook(WEBHOOK_URL)
|
||||||
# insert code here to run it after start
|
# insert code here to run it after start
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
async def on_shutdown(dp):
|
async def on_shutdown(dp):
|
||||||
# insert code here to run it before shutdown
|
# insert code here to run it before shutdown
|
||||||
#
|
pass
|
||||||
await bot.close()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue