feat: show telegram group names in manager
This commit is contained in:
+25
-4
@@ -167,10 +167,31 @@ async def api_actions(limit: int = Query(default=100, ge=1, le=500)):
|
||||
|
||||
@app.get("/api/groups")
|
||||
async def api_groups():
|
||||
rows = await Message.all().distinct().values("chat_id")
|
||||
extra = await LotteryEvent.all().distinct().values("chat_id")
|
||||
ids = sorted({int(r["chat_id"]) for r in rows + extra if r.get("chat_id")})
|
||||
return JSONResponse([{"chat_id": x, "name": str(x)} for x in ids])
|
||||
import config
|
||||
from telegram import Bot
|
||||
from telegram.request import HTTPXRequest
|
||||
|
||||
sources = []
|
||||
for model in (Message, Ban, Action, GameScore, LotteryEvent, ShopItem):
|
||||
try:
|
||||
sources.extend(await model.all().distinct().values("chat_id"))
|
||||
except Exception:
|
||||
pass
|
||||
ids = sorted({int(r["chat_id"]) for r in sources if r.get("chat_id")})
|
||||
if config.TG_CHAT_ID and config.TG_CHAT_ID not in ids:
|
||||
ids.insert(0, config.TG_CHAT_ID)
|
||||
|
||||
bot = Bot(token=config.TG_BOT_TOKEN, request=HTTPXRequest(proxy=getattr(config, "TG_PROXY_URL", config.PROXY_URL), connect_timeout=10.0, read_timeout=10.0))
|
||||
result = []
|
||||
for chat_id in ids:
|
||||
name = str(chat_id)
|
||||
try:
|
||||
chat = await bot.get_chat(chat_id=chat_id)
|
||||
name = chat.title or chat.full_name or chat.username or str(chat_id)
|
||||
except Exception:
|
||||
pass
|
||||
result.append({"chat_id": chat_id, "name": name, "label": f"{name} ({chat_id})"})
|
||||
return JSONResponse(result)
|
||||
|
||||
|
||||
@app.get("/api/shop-items")
|
||||
|
||||
Reference in New Issue
Block a user