mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 18:01:04 +00:00
Refactor and improve bot examples (#1256)
* 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
This commit is contained in:
parent
68c0516f69
commit
c516ea9d6a
8 changed files with 89 additions and 56 deletions
|
|
@ -2,17 +2,20 @@
|
|||
This example shows how to use webhook on behind of any reverse proxy (nginx, traefik, ingress etc.)
|
||||
"""
|
||||
import logging
|
||||
import sys
|
||||
from os import getenv
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
from aiogram import Bot, Dispatcher, Router, types
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram.filters import Command
|
||||
from aiogram.filters import CommandStart
|
||||
from aiogram.types import Message
|
||||
from aiogram.utils.markdown import hbold
|
||||
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
|
||||
|
||||
# Bot token can be obtained via https://t.me/BotFather
|
||||
TOKEN = "42:TOKEN"
|
||||
TOKEN = getenv("BOT_TOKEN")
|
||||
|
||||
# Webserver settings
|
||||
# bind localhost only to prevent any external access
|
||||
|
|
@ -32,7 +35,7 @@ BASE_WEBHOOK_URL = "https://aiogram.dev/"
|
|||
router = Router()
|
||||
|
||||
|
||||
@router.message(Command(commands=["start"]))
|
||||
@router.message(CommandStart())
|
||||
async def command_start_handler(message: Message) -> None:
|
||||
"""
|
||||
This handler receives messages with `/start` command
|
||||
|
|
@ -42,7 +45,7 @@ async def command_start_handler(message: Message) -> None:
|
|||
# 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, <b>{message.from_user.full_name}!</b>")
|
||||
await message.answer(f"Hello, {hbold(message.from_user.full_name)}!")
|
||||
|
||||
|
||||
@router.message()
|
||||
|
|
@ -63,7 +66,7 @@ async def echo_handler(message: types.Message) -> None:
|
|||
async def on_startup(bot: Bot) -> None:
|
||||
# If you have a self-signed SSL certificate, then you will need to send a public
|
||||
# certificate to Telegram
|
||||
await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}")
|
||||
await bot.set_webhook(f"{BASE_WEBHOOK_URL}{WEBHOOK_PATH}", secret_token=WEBHOOK_SECRET)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
|
@ -100,5 +103,5 @@ def main() -> None:
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue