mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Refactor code for specifying updates example.
This commit is contained in:
parent
68c0516f69
commit
dac189d0d6
2 changed files with 20 additions and 8 deletions
1
CHANGES/5778.doc.rst
Normal file
1
CHANGES/5778.doc.rst
Normal file
|
|
@ -0,0 +1 @@
|
|||
Refactor code for specifying updates example.
|
||||
|
|
@ -2,7 +2,8 @@ import asyncio
|
|||
import logging
|
||||
|
||||
from aiogram import Bot, Dispatcher, Router
|
||||
from aiogram.filters import Command
|
||||
from aiogram.enums import ParseMode, UpdateType
|
||||
from aiogram.filters import LEAVE_TRANSITION, ChatMemberUpdatedFilter, CommandStart
|
||||
from aiogram.types import (
|
||||
CallbackQuery,
|
||||
ChatMemberUpdated,
|
||||
|
|
@ -19,7 +20,7 @@ logging.basicConfig(level=logging.INFO)
|
|||
router = Router()
|
||||
|
||||
|
||||
@router.message(Command("start"))
|
||||
@router.message(CommandStart())
|
||||
async def command_start_handler(message: Message) -> None:
|
||||
"""
|
||||
This handler receives messages with `/start` command
|
||||
|
|
@ -57,25 +58,25 @@ sub_sub_router = Router()
|
|||
|
||||
@sub_sub_router.edited_message()
|
||||
async def edited_message_handler(edited_message: Message) -> None:
|
||||
await edited_message.reply("Message was edited, big brother watch you")
|
||||
await edited_message.reply("Message was edited, Big Brother watches you")
|
||||
|
||||
|
||||
# this router will use only my_chat_member updates
|
||||
deep_dark_router = Router()
|
||||
|
||||
|
||||
@deep_dark_router.my_chat_member()
|
||||
@deep_dark_router.my_chat_member(~ChatMemberUpdatedFilter(LEAVE_TRANSITION))
|
||||
async def my_chat_member_change(chat_member: ChatMemberUpdated, bot: Bot) -> None:
|
||||
await bot.send_message(
|
||||
chat_member.chat.id,
|
||||
"Member was changed from "
|
||||
"This Bot`s status was changed from "
|
||||
+ f"{chat_member.old_chat_member.status} to {chat_member.new_chat_member.status}",
|
||||
)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# Initialize Bot instance with a default parse mode which will be passed to all API calls
|
||||
bot = Bot(TOKEN, parse_mode="HTML")
|
||||
bot = Bot(TOKEN, parse_mode=ParseMode.HTML)
|
||||
|
||||
dp = Dispatcher()
|
||||
dp.include_router(router)
|
||||
|
|
@ -83,11 +84,21 @@ async def main() -> None:
|
|||
router.include_router(sub_router)
|
||||
router.include_router(sub_sub_router)
|
||||
|
||||
useful_updates = dp.resolve_used_update_types()
|
||||
# Specify which updates should be handled by Dispatcher.
|
||||
# By default, all types, that used in handlers will be used.
|
||||
useful_updates = [
|
||||
UpdateType.MESSAGE,
|
||||
UpdateType.CALLBACK_QUERY,
|
||||
UpdateType.EDITED_MESSAGE,
|
||||
# UpdateType.MY_CHAT_MEMBER, # Here we decided to skip the my_chat_member updates, even though it's used in our handlers. Uncomment this line to enable it.
|
||||
]
|
||||
|
||||
# And the run events dispatching
|
||||
# Start event dispatching
|
||||
await dp.start_polling(bot, allowed_updates=useful_updates)
|
||||
|
||||
# If you want to use all the update types registered in the dispatcher, you should not specify the `allowed_updates` parameter:
|
||||
# await dp.start_polling(bot)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue