welcomecenterbot/api/webhook.py

57 lines
1.7 KiB
Python
Raw Normal View History

2023-09-11 20:04:53 +00:00
from bot.handlers.routing import handle_routing
2023-09-11 15:52:35 +00:00
from bot.handlers.callback_vouch import handle_button
from bot.handlers.callback_unlink import handle_unlink
from bot.handlers.handle_startup import handle_startup
2023-09-11 20:04:53 +00:00
from bot.handlers.handle_join_request import handle_join_request
2023-09-11 15:52:35 +00:00
from bot.api import register_webhook, send_message
from bot.config import FEEDBACK_CHAT_ID
2023-09-11 18:40:24 +00:00
from sanic.app import Sanic
2023-04-17 21:42:25 +00:00
2023-04-23 17:04:13 +00:00
app = Sanic(name="welcomecenter")
2023-09-11 18:40:24 +00:00
app.config.LOGGING = True
2023-04-17 21:42:25 +00:00
app.config.REGISTERED = False
2023-09-11 17:02:29 +00:00
state = State()
2023-04-17 21:42:25 +00:00
2023-09-11 20:04:53 +00:00
@app.get("/")
2023-04-17 21:42:25 +00:00
async def register(req):
if not app.config.REGISTERED:
2023-09-06 10:08:05 +00:00
print(register_webhook())
2023-04-17 21:42:25 +00:00
app.config.REGISTERED = True
2023-09-11 20:04:53 +00:00
await handle_startup()
return text("ok")
2023-04-17 21:42:25 +00:00
2023-09-11 20:04:53 +00:00
@app.post("/")
2023-04-17 21:42:25 +00:00
async def handle(req):
print(req)
try:
update = req.json
print(update)
2023-04-23 16:54:58 +00:00
# видимые сообщения
2023-09-11 20:04:53 +00:00
msg = update.get("message", update.get("edited_message"))
2023-04-23 16:54:58 +00:00
if msg:
2023-09-11 20:04:53 +00:00
msg["edit"] = "edited_message" in update
await handle_routing(msg)
2023-04-23 16:54:58 +00:00
# кнопки
2023-09-11 20:04:53 +00:00
elif "callback_query" in update:
data = update["callback_query"]["data"]
if data.startswith("vouch"):
await handle_button(update["callback_query"])
elif data.startswith("unlink"):
await handle_unlink(update["callback_query"])
2023-04-23 16:54:58 +00:00
# заявки
2023-09-11 20:04:53 +00:00
elif "chat_join_request" in update:
print("chat join request")
await handle_join_request(update["chat_join_request"])
2023-04-23 16:54:58 +00:00
2023-04-17 21:42:25 +00:00
except Exception:
import traceback
2023-09-11 20:04:53 +00:00
await send_message(FEEDBACK_CHAT_ID, f"<pre>\n{traceback.format_exc()}\n</pre>")
2023-04-17 21:42:25 +00:00
traceback.print_exc()
2023-09-11 20:04:53 +00:00
return text("ok")