welcomecenterbot/api/webhook.py

63 lines
1.9 KiB
Python
Raw Normal View History

2023-09-18 08:47:09 +00:00
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
2023-09-11 18:40:24 +00:00
from sanic.app import Sanic
2023-09-18 08:47:09 +00:00
from sanic.response import text
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
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-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-18 08:47:09 +00:00
logger.info(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):
2023-09-18 08:47:09 +00:00
logger.debug(req)
2023-04-17 21:42:25 +00:00
try:
update = req.json
2023-09-18 08:47:09 +00:00
logger.debug(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:
2023-09-18 08:47:09 +00:00
logger.info("chat join request")
2023-09-11 20:04:53 +00:00
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-18 08:47:09 +00:00
logger.error(traceback.format_exc())
2023-09-11 20:04:53 +00:00
return text("ok")