Files
tg_post_min/app/bot/handlers/chat_id_cmd.py
2025-08-20 21:53:40 +09:00

35 lines
1.4 KiB
Python

import asyncio
from telegram import Update
from telegram.constants import ChatType, ParseMode
from telegram.ext import ContextTypes
TTL_SEC = 20
async def _auto_delete(ctx: ContextTypes.DEFAULT_TYPE, chat_id: int, message_id: int, delay: int = TTL_SEC):
try:
await asyncio.sleep(delay)
await ctx.bot.delete_message(chat_id=chat_id, message_id=message_id)
except Exception:
pass
async def chat_id_cmd(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
if chat.type not in (ChatType.GROUP, ChatType.SUPERGROUP, ChatType.CHANNEL):
await update.effective_message.reply_text("Эта команда работает в группе/канале.")
return
# Только админы могут увидеть ID (снижаем риск утечки)
try:
member = await ctx.bot.get_chat_member(chat.id, update.effective_user.id)
if member.status not in ("administrator", "creator"):
return # молча игнорируем для не-админов
except Exception:
return
text = f"ID этого чата: `{chat.id}`\nСообщение удалится через {TTL_SEC} сек."
try:
msg = await update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
ctx.application.create_task(_auto_delete(ctx, chat.id, msg.message_id, delay=TTL_SEC))
except Exception:
pass