From 77dc4e77521f0f6a0505b0317baae5d75a4bc927 Mon Sep 17 00:00:00 2001 From: latan Date: Thu, 10 Aug 2023 09:31:13 +0300 Subject: [PATCH] Add multi-file bot example This commit adds a multi-file bot example to the repository. . --- examples/multi_file_bot/bot.py | 30 ++++++++++++++++++++ examples/multi_file_bot/handlers/__init__.py | 2 ++ examples/multi_file_bot/handlers/echo.py | 20 +++++++++++++ examples/multi_file_bot/handlers/start.py | 18 ++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 examples/multi_file_bot/bot.py create mode 100644 examples/multi_file_bot/handlers/__init__.py create mode 100644 examples/multi_file_bot/handlers/echo.py create mode 100644 examples/multi_file_bot/handlers/start.py diff --git a/examples/multi_file_bot/bot.py b/examples/multi_file_bot/bot.py new file mode 100644 index 00000000..f1ac7607 --- /dev/null +++ b/examples/multi_file_bot/bot.py @@ -0,0 +1,30 @@ +import asyncio +import logging + +from aiogram import Bot, Dispatcher +from aiogram.enums import ParseMode +from examples.multi_file_bot import handlers + +# Bot token can be obtained via https://t.me/BotFather +TOKEN = "42:TOKEN" + + +async def main() -> None: + # Dispatcher is a root router + dp = Dispatcher() + # Register all the routers from handlers package (make sure they are imported into handlers/__init__.py) + dp.include_routers( + handlers.start.start_router, + handlers.echo.echo_router, + + ) + + # Initialize Bot instance with a default parse mode which will be passed to all API calls + bot = Bot(TOKEN, parse_mode=ParseMode.HTML) + # And the run events dispatching + await dp.start_polling(bot) + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + asyncio.run(main()) diff --git a/examples/multi_file_bot/handlers/__init__.py b/examples/multi_file_bot/handlers/__init__.py new file mode 100644 index 00000000..aa07a69b --- /dev/null +++ b/examples/multi_file_bot/handlers/__init__.py @@ -0,0 +1,2 @@ +from . import start +from . import echo diff --git a/examples/multi_file_bot/handlers/echo.py b/examples/multi_file_bot/handlers/echo.py new file mode 100644 index 00000000..5b6959ab --- /dev/null +++ b/examples/multi_file_bot/handlers/echo.py @@ -0,0 +1,20 @@ +from aiogram import Router +from aiogram.filters import Command +from aiogram.types import Message + +# For each module with handlers we can create a separate router. +echo_router = Router() + +@echo_router.message() +async def echo_handler(message: Message) -> None: + """ + Handler will forward receive a message back to the sender + + By default, message handler will handle all message types (like a text, photo, sticker etc.) + """ + try: + # Send a copy of the received message + await message.send_copy(chat_id=message.chat.id) + except TypeError: + # But not all the types is supported to be copied so need to handle it + await message.answer("Nice try!") diff --git a/examples/multi_file_bot/handlers/start.py b/examples/multi_file_bot/handlers/start.py new file mode 100644 index 00000000..804268c3 --- /dev/null +++ b/examples/multi_file_bot/handlers/start.py @@ -0,0 +1,18 @@ +from aiogram import Router +from aiogram.filters import Command +from aiogram.types import Message + +start_router = Router() + + +@start_router.message(Command("start")) +async def command_start_handler(message: Message) -> None: + """ + This handler receives messages with `/start` command + """ + # Most event objects have aliases for API methods that can be called in events' context + # For example if you want to answer to incoming message you can use `message.answer(...)` alias + # and the target chat will be passed to :ref:`aiogram.methods.send_message.SendMessage` + # method automatically or call API method directly via + # Bot instance: `bot.send_message(chat_id=message.chat.id, ...)` + await message.answer(f"Hello, {message.from_user.full_name}!")