startup-fix

This commit is contained in:
2023-09-18 11:47:09 +03:00
parent 980b455585
commit 5d79bff891
5 changed files with 29 additions and 26 deletions

View File

@@ -1,22 +1,28 @@
from handlers.routing import handle_routing
from handlers.callback_vouch import handle_button
from handlers.callback_unlink import handle_unlink
from handlers.handle_startup import handle_startup
from handlers.handle_join_request import handle_join_request
from api import register_webhook, send_message
from config import FEEDBACK_CHAT_ID
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:
print(register_webhook())
logger.info(register_webhook())
app.config.REGISTERED = True
await handle_startup()
return text("ok")
@@ -24,10 +30,10 @@ async def register(req):
@app.post("/")
async def handle(req):
print(req)
logger.debug(req)
try:
update = req.json
print(update)
logger.debug(update)
# видимые сообщения
msg = update.get("message", update.get("edited_message"))
@@ -45,12 +51,12 @@ async def handle(req):
# заявки
elif "chat_join_request" in update:
print("chat join request")
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")

View File

@@ -1,7 +1,6 @@
import os
BOT_TOKEN = os.environ.get("BOT_TOKEN") or ""
ADMIN_ID = os.environ.get("ADMIN_ID") or ""
WEBHOOK = os.environ.get("VERCEL_URL") or "http://localhost:8000"
REDIS_URL = os.environ.get("REDIS_URL") or "redis://localhost:6379"
FEEDBACK_CHAT_ID = os.environ.get("FEEDBACK_CHAT_ID", "").replace("-", "-100")

View File

@@ -1,3 +1,4 @@
from bot.config import FEEDBACK_CHAT_ID
from storage import scan, Profile
from api import approve_chat_join_request, kick_member, send_message
from handlers.callback_vouch import update_button
@@ -7,8 +8,9 @@ import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# устанавливает соответствие данных
async def handle_startup(msg):
async def revalidate_storage():
# поддерживает консистентность данных
btn_ids, _btns = scan(match="btn-*", count=100)
for btnid in btn_ids:
# для каждой ранее созданной кнопки
@@ -28,11 +30,9 @@ async def handle_startup(msg):
logger.debug(r)
if r["ok"]:
_, identity, username = userdata_extract(newcomer["result"]["user"])
lang = msg["from_user"]["lang"]
body = (
"Участник %s%s был удалён"
if lang == "ru"
else "Member %s%s was deleted"
) % (identity, username)
r = await send_message(chat_id, body)
body = f"Участник {identity} {username} был удалён"
r = await send_message(FEEDBACK_CHAT_ID, body)
logger.debug(r)
async def handle_startup():
await revalidate_storage()

View File

@@ -31,8 +31,6 @@ async def handle_routing(msg, state):
logger.debug(msg)
if msg.get("reply_to_message"):
await handle_answer(msg)
elif msg["text"] == "/graph":
await handle_command_graph(msg)
elif msg["text"].startswith("/ask"):
await handle_command_ask(msg)

View File

@@ -6,7 +6,7 @@ from aiogram import Bot, Dispatcher, Router
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart, Command
from aiogram.types import Message, ChatJoinRequest, CallbackQuery
from config import BOT_TOKEN, ADMIN_ID
from config import BOT_TOKEN
from handlers.routing import handle_routing
from handlers.callback_unlink import handle_unlink
from handlers.callback_vouch import handle_button