misc: consistency of comments and dispatcher instance creation

This commit is contained in:
sdmway 2024-05-07 20:45:24 -06:00
parent 1cb0f38d2e
commit fd95745fef
9 changed files with 31 additions and 16 deletions

View file

@ -3,7 +3,7 @@ import logging
import sys
from os import getenv
from aiogram import Bot, Dispatcher, html
from aiogram import Bot, Dispatcher, Router, html
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
@ -13,10 +13,11 @@ from aiogram.types import Message
TOKEN = getenv("BOT_TOKEN")
# All handlers should be attached to the Router (or Dispatcher)
dp = Dispatcher()
echo_router = Router()
@dp.message(CommandStart())
@echo_router.message(CommandStart())
async def command_start_handler(message: Message) -> None:
"""
This handler receives messages with `/start` command
@ -29,7 +30,7 @@ async def command_start_handler(message: Message) -> None:
await message.answer(f"Hello, {html.bold(message.from_user.full_name)}!")
@dp.message()
@echo_router.message()
async def echo_handler(message: Message) -> None:
"""
Handler will forward receive a message back to the sender
@ -47,6 +48,11 @@ async def echo_handler(message: Message) -> None:
async def main() -> None:
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()
dp.include_router(echo_router)
# And the run events dispatching
await dp.start_polling(bot)

View file

@ -79,7 +79,7 @@ def main() -> None:
# Register startup hook to initialize webhook
dp.startup.register(on_startup)
# Initialize Bot instance with a default parse mode which will be passed to all API calls
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# Create aiohttp.web.Application instance

View file

@ -90,7 +90,7 @@ def main() -> None:
# Register startup hook to initialize webhook
dp.startup.register(on_startup)
# Initialize Bot instance with a default parse mode which will be passed to all API calls
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# Create aiohttp.web.Application instance

View file

@ -102,7 +102,7 @@ async def handle_set_name(message: types.Message, command: CommandObject) -> Non
async def main() -> None:
# Initialize Bot instance with a default parse mode which will be passed to all API calls
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)

View file

@ -125,10 +125,14 @@ async def show_summary(message: Message, data: Dict[str, Any], positive: bool =
async def main():
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()
dp.include_router(form_router)
# Start event dispatching
await dp.start_polling(bot)

View file

@ -22,8 +22,9 @@ async def main() -> None:
echo_router,
)
# Initialize Bot instance with a default parse mode which will be passed to all API calls
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)

View file

@ -289,13 +289,13 @@ def create_dispatcher():
async def main():
dispatcher = create_dispatcher()
dp = create_dispatcher()
bot = Bot(token=TOKEN)
await dispatcher.start_polling(bot)
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(main())
# Alternatively, you can use aiogram-cli:
# `aiogram run polling quiz_scene:create_dispatcher --log-level info --token BOT_TOKEN`
logging.basicConfig(level=logging.INFO)
asyncio.run(main())

View file

@ -197,9 +197,13 @@ def create_dispatcher() -> Dispatcher:
return dispatcher
if __name__ == "__main__":
# Recommended to use CLI instead of this snippet.
# `aiogram run polling scene_example:create_dispatcher --token BOT_TOKEN --log-level info`
def main() -> None:
dp = create_dispatcher()
bot = Bot(token=TOKEN)
dp.run_polling(bot)
if __name__ == "__main__":
# Recommended to use CLI instead of this snippet.
# `aiogram run polling scene_example:create_dispatcher --token BOT_TOKEN --log-level info`
main()

View file

@ -79,7 +79,7 @@ async def my_chat_member_change(chat_member: ChatMemberUpdated, bot: Bot) -> Non
async def main() -> None:
# Initialize Bot instance with a default parse mode which will be passed to all API calls
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
dp = Dispatcher()