88 lines
3.4 KiB
Python
88 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Список участников TG-чата(ов) харнеса (t-261010, просьба Alex).
|
|
|
|
Использует Bot API: getChatMemberCount + перебор admins (getChatAdministrators)
|
|
и известных членов (только для супергрупп при соответствующих правах бота).
|
|
Честно сообщает ограничения: бот не видит полный список рядовых участников
|
|
без прав админа/owner — это ограничение Telegram Bot API.
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
from harness import tg_inbox as tgi # noqa: E402
|
|
|
|
|
|
def chat_members(chat_id):
|
|
tgi._load_env()
|
|
if not tgi.TG_TOKEN:
|
|
print("error: нет SIMBIOT_TG_TOKEN")
|
|
return 1
|
|
count = tgi._api("getChatMemberCount", {"chat_id": chat_id})["result"]
|
|
print(f"Чат {chat_id}: участников — {count}")
|
|
try:
|
|
chat = tgi._api("getChat", {"chat_id": chat_id})["result"]
|
|
title = chat.get("title") or chat.get("username") or "личный чат"
|
|
print(f" Название: {title}")
|
|
except Exception:
|
|
pass
|
|
try:
|
|
admins = tgi._api("getChatAdministrators", {"chat_id": chat_id})["result"]
|
|
for a in admins:
|
|
u = a.get("user") or {}
|
|
name = " ".join(x for x in [u.get("first_name"), u.get("last_name")] if x) or u.get("username") or u.get("id")
|
|
print(f" [{'@' + u['username'] if u.get('username') else u.get('id')}] {name} — {a.get('status')}")
|
|
except Exception as e:
|
|
print(f" (админов не удалось получить: {e})")
|
|
print(" Полный список рядовых участников Bot API не отдаёт — только количество и админов.")
|
|
return 0
|
|
|
|
|
|
def main():
|
|
# чаты: из inbox (все chat_id входящих) + ALLOWED
|
|
ids = []
|
|
try:
|
|
import sqlite3
|
|
from harness import paths
|
|
for db in [paths.db_path(), os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tg_inbox.db"), os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "hg_tg_inbox.db")]:
|
|
if not os.path.exists(db):
|
|
continue
|
|
try:
|
|
con = sqlite3.connect(db)
|
|
for row in con.execute("SELECT DISTINCT payload FROM events").fetchall():
|
|
try:
|
|
p = json.loads(row[0])
|
|
cid = p.get("chat_id") or (p.get("message") or {}).get("chat", {}).get("id")
|
|
if cid:
|
|
ids.append(cid)
|
|
except Exception:
|
|
pass
|
|
con.close()
|
|
except Exception:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
tgi._load_env()
|
|
ids += [n for n in (_num(x) for x in tgi.ALLOWED) if n]
|
|
seen, rc = set(), 0
|
|
for cid in ids:
|
|
if cid in seen:
|
|
continue
|
|
try:
|
|
seen.add(cid)
|
|
rc |= chat_members(cid)
|
|
except Exception as e:
|
|
print(f"Чат {cid}: ошибка {e}")
|
|
return rc
|
|
|
|
|
|
def _num(x):
|
|
try:
|
|
return int(x)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main() or 0)
|