mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 18:01:04 +00:00
* Refactor and improve bot messages
Refactored bot code to use aiogram enumerations and enhanced chat messages with markdown beautifications for a more user-friendly display.
CommandStart() is now used instead of Command('start') for readability.
Furthermore, the bot's 'stop' command was improved, ensuring it executes appropriately during KeyboardInterrupt or SystemExit.
Additionally, the bot's logging was adjusted to output to sys.stdout for better logs' readability.
* Added Changelog
* Add guidance comments on obtaining bot tokens from environment variables
* Remove hardcoded tokens, opt for environment variable
* Remove unnecessary spaces and reorganize imports
* Fix error, switch default storage from Redis to Memory, and add logging to multibot example
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import asyncio
|
|
import logging
|
|
import sys
|
|
from os import getenv
|
|
|
|
from aiogram import Bot, Dispatcher, Router, types
|
|
from aiogram.enums import ParseMode
|
|
from aiogram.filters import CommandStart
|
|
from aiogram.types import Message
|
|
from aiogram.utils.markdown import hbold
|
|
|
|
# Bot token can be obtained via https://t.me/BotFather
|
|
TOKEN = getenv("BOT_TOKEN")
|
|
|
|
# All handlers should be attached to the Router (or Dispatcher)
|
|
router = Router()
|
|
|
|
|
|
@router.message(CommandStart())
|
|
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, {hbold(message.from_user.full_name)}!")
|
|
|
|
|
|
@router.message()
|
|
async def echo_handler(message: types.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!")
|
|
|
|
|
|
async def main() -> None:
|
|
# Dispatcher is a root router
|
|
dp = Dispatcher()
|
|
# ... and all other routers should be attached to Dispatcher
|
|
dp.include_router(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, stream=sys.stdout)
|
|
try:
|
|
asyncio.run(main())
|
|
except (KeyboardInterrupt, SystemExit):
|
|
logging.info("Bot stopped!")
|