Files
welcomecenterbot/api/webhook.py

63 lines
1.9 KiB
Python
Raw Normal View History

2023-09-18 11:47:09 +03: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 21:40:24 +03:00
from sanic.app import Sanic
2023-09-18 11:47:09 +03:00
from sanic.response import text
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
2023-04-18 00:42:25 +03:00
2023-04-23 20:04:13 +03:00
app = Sanic(name="welcomecenter")
2023-09-11 21:40:24 +03:00
app.config.LOGGING = True
2023-04-18 00:42:25 +03:00
app.config.REGISTERED = False
2023-09-11 20:02:29 +03:00
state = State()
2023-09-11 23:04:53 +03:00
@app.get("/")
2023-04-18 00:42:25 +03:00
async def register(req):
if not app.config.REGISTERED:
2023-09-18 11:47:09 +03:00
logger.info(register_webhook())
2023-04-18 00:42:25 +03:00
app.config.REGISTERED = True
2023-09-11 23:04:53 +03:00
await handle_startup()
return text("ok")
2023-04-18 00:42:25 +03:00
2023-09-11 23:04:53 +03:00
@app.post("/")
2023-04-18 00:42:25 +03:00
async def handle(req):
2023-09-18 11:47:09 +03:00
logger.debug(req)
2023-04-18 00:42:25 +03:00
try:
update = req.json
2023-09-18 11:47:09 +03:00
logger.debug(update)
2023-04-23 19:54:58 +03:00
# видимые сообщения
2023-09-11 23:04:53 +03:00
msg = update.get("message", update.get("edited_message"))
2023-04-23 19:54:58 +03:00
if msg:
2023-09-11 23:04:53 +03:00
msg["edit"] = "edited_message" in update
await handle_routing(msg)
2023-04-23 19:54:58 +03:00
# кнопки
2023-09-11 23:04:53 +03: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 19:54:58 +03:00
# заявки
2023-09-11 23:04:53 +03:00
elif "chat_join_request" in update:
2023-09-18 11:47:09 +03:00
logger.info("chat join request")
2023-09-11 23:04:53 +03:00
await handle_join_request(update["chat_join_request"])
2023-04-23 19:54:58 +03:00
2023-04-18 00:42:25 +03:00
except Exception:
import traceback
2023-09-11 23:04:53 +03:00
await send_message(FEEDBACK_CHAT_ID, f"<pre>\n{traceback.format_exc()}\n</pre>")
2023-04-18 00:42:25 +03:00
traceback.print_exc()
2023-09-18 11:47:09 +03:00
logger.error(traceback.format_exc())
2023-09-11 23:04:53 +03:00
return text("ok")