welcomecenterbot/api/webhook.py
2023-09-18 11:47:09 +03:00

63 lines
1.9 KiB
Python

from bot.handlers.routing import handle_routing
from bot.handlers.callback_vouch import handle_button
from bot.handlers.callback_unlink import handle_unlink
from bot.handlers.handle_startup import handle_startup
from bot.handlers.handle_join_request import handle_join_request
from bot.api import register_webhook, send_message
from bot.config import FEEDBACK_CHAT_ID, WEBHOOK
from bot.state import State
from sanic.app import Sanic
from sanic.response import text
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
app = Sanic(name="welcomecenter")
app.config.LOGGING = True
app.config.REGISTERED = False
state = State()
@app.get("/")
async def register(req):
if not app.config.REGISTERED:
logger.info(register_webhook())
app.config.REGISTERED = True
await handle_startup()
return text("ok")
@app.post("/")
async def handle(req):
logger.debug(req)
try:
update = req.json
logger.debug(update)
# видимые сообщения
msg = update.get("message", update.get("edited_message"))
if msg:
msg["edit"] = "edited_message" in update
await handle_routing(msg)
# кнопки
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"])
# заявки
elif "chat_join_request" in update:
logger.info("chat join request")
await handle_join_request(update["chat_join_request"])
except Exception:
import traceback
await send_message(FEEDBACK_CHAT_ID, f"<pre>\n{traceback.format_exc()}\n</pre>")
traceback.print_exc()
logger.error(traceback.format_exc())
return text("ok")