-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
64 lines (54 loc) · 1.96 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from pathlib import Path
from aiohttp.web_fileresponse import FileResponse
from aiohttp.web_request import Request
from aiohttp.web_response import json_response
from aiogram import Bot
from aiogram.types import (
InlineKeyboardButton,
InlineKeyboardMarkup,
InlineQueryResultArticle,
InputTextMessageContent,
WebAppInfo,
)
from aiogram.utils.web_app import check_webapp_signature, safe_parse_webapp_init_data
async def demo_handler(request: Request):
return FileResponse(Path(__file__).parent.resolve() / "demo.html")
async def check_data_handler(request: Request):
bot: Bot = request.app["bot"]
data = await request.post()
if check_webapp_signature(bot.token, data["_auth"]):
return json_response({"ok": True})
return json_response({"ok": False, "err": "Unauthorized"}, status=401)
async def send_message_handler(request: Request):
bot: Bot = request.app["bot"]
data = await request.post()
try:
web_app_init_data = safe_parse_webapp_init_data(token=bot.token, init_data=data["_auth"])
except ValueError:
return json_response({"ok": False, "err": "Unauthorized"}, status=401)
print(data)
reply_markup = None
if data["with_webview"] == "1":
reply_markup = InlineKeyboardMarkup(
inline_keyboard=[
[
InlineKeyboardButton(
text="Open",
web_app=WebAppInfo(url=str(request.url.with_scheme("https"))),
)
]
]
)
await bot.answer_web_app_query(
web_app_query_id=web_app_init_data.query_id,
result=InlineQueryResultArticle(
id=web_app_init_data.query_id,
title="Demo",
input_message_content=InputTextMessageContent(
message_text="Hello, World!",
parse_mode=None,
),
reply_markup=reply_markup,
),
)
return json_response({"ok": True})