aiogram/examples/web_app/handlers.py
sdmway e3dc7d576b
misc: code consistency and bot instance creation (#1482)
* misc: code consistency and bot instance creation

* Changelog for aiogram#1482

* misc: consistency of comments and dispatcher instance creation

* misc: removed routers example

* Update CHANGES/1482.misc.rst

---------

Co-authored-by: Alex Root Junior <jroot.junior@gmail.com>
2024-05-31 20:11:21 +03:00

48 lines
1.5 KiB
Python

from aiogram import Bot, F, Router
from aiogram.filters import Command, CommandStart
from aiogram.types import (
InlineKeyboardButton,
InlineKeyboardMarkup,
MenuButtonWebApp,
Message,
WebAppInfo,
)
my_router = Router()
@my_router.message(CommandStart())
async def command_start(message: Message, bot: Bot, base_url: str):
await bot.set_chat_menu_button(
chat_id=message.chat.id,
menu_button=MenuButtonWebApp(text="Open Menu", web_app=WebAppInfo(url=f"{base_url}/demo")),
)
await message.answer("""Hi!\nSend me any type of message to start.\nOr just send /webview""")
@my_router.message(Command("webview"))
async def command_webview(message: Message, base_url: str):
await message.answer(
"Good. Now you can try to send it via Webview",
reply_markup=InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(
text="Open Webview", web_app=WebAppInfo(url=f"{base_url}/demo")
)
]
]
),
)
@my_router.message(~F.message.via_bot) # Echo to all messages except messages via bot
async def echo_all(message: Message, base_url: str):
await message.answer(
"Test webview",
reply_markup=InlineKeyboardMarkup(
inline_keyboard=[
[InlineKeyboardButton(text="Open", web_app=WebAppInfo(url=f"{base_url}/demo"))]
]
),
)