from fastapi import FastAPI, Request from pyrogram import Client, filters import os import requests from contextlib import asynccontextmanager import logging import aria2p import asyncio import tempfile import shutil # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) app = FastAPI() API_ID = os.environ.get("API_ID", 2589472) API_HASH = os.environ.get("API_HASH", "5243628014db9798d6775b330e542602") TOKEN = os.environ.get("TOKEN", "6520286363:AAEe0c5v_JQf8ztatUx8ZeO_ARXrJkYsluI") CHAT_ID = os.environ.get("CHAT_ID", "1487943213") # Add your chat ID here async def clientbot(): bot = Client( ':memory:', api_id=API_ID, api_hash=API_HASH, bot_token=TOKEN ) aria_helper = AriaHelper() @bot.on_message(filters.command("start")) async def start_command(client, message): await message.reply_text("Bot has started!") logger.info("Start command received and responded") @bot.on_message(filters.command("sendmessage")) async def send_message_command(client, message): if len(message.command) < 3: await message.reply_text("Usage: /sendmessage ") return chat_id = message.command[1] msg = " ".join(message.command[2:]) try: await client.send_message(chat_id, msg) await message.reply_text("Message sent successfully") logger.info(f"Message sent to {chat_id}: {msg}") except Exception as e: await message.reply_text(f"Failed to send message: {str(e)}") logger.error(f"Failed to send message: {str(e)}") @bot.on_message(filters.command("id")) async def id_command(client, message): user = None user_info = None if message.reply_to_message: if hasattr(message.reply_to_message, 'forward_origin') and message.reply_to_message.forward_origin: if hasattr(message.reply_to_message.forward_origin, 'sender_user') and message.reply_to_message.forward_origin.sender_user: user = message.reply_to_message.forward_origin.sender_user elif hasattr(message.reply_to_message.forward_origin, 'sender_chat') and message.reply_to_message.forward_origin.sender_chat: user_info = { "id": message.reply_to_message.forward_origin.sender_chat.id, "name": message.reply_to_message.forward_origin.sender_chat.title, "type": "channel", "link": f"https://t.me/{message.reply_to_message.forward_origin.sender_chat.username}/{message.reply_to_message.id}", "username": message.reply_to_message.forward_origin.sender_chat.username } else: user = message.reply_to_message.from_user elif hasattr(message, 'forward_origin') and message.forward_origin: if hasattr(message.forward_origin, 'sender_user') and message.forward_origin.sender_user: user = message.forward_origin.sender_user elif hasattr(message.forward_origin, 'sender_chat') and message.forward_origin.sender_chat: user_info = { "id": message.forward_origin.sender_chat.id, "name": message.forward_origin.sender_chat.title, "type": "channel", "link": f"https://t.me/{message.forward_origin.sender_chat.username}/{message.id}", "username": message.forward_origin.sender_chat.username } else: user = message.from_user if user: user_id = user.id user_name = user.first_name if user.last_name: user_name += f" {user.last_name}" profile_link = f"tg://user?id={user_id}" user_tag = f"@{user.username}" if user.username else "User has not set a user tag" response = ( f"➲ **Username:** [{user_name}]({profile_link})\n" f"➲ **Usertag:** {user_tag}\n" f"➲ **Telegram ID:** `{user_id}`" ) elif user_info: user_tag = f"@{user_info['username']}" if user_info['username'] else "Channel has not set a user tag" response = ( f"➲ **Channel:** [{user_info['name']}]({user_info['link']})\n" f"➲ **Usertag:** {user_tag}\n" f"➲ **Telegram ID:** `{user_info['id']}`" ) else: response = "No user information found." await message.reply_text(response, disable_web_page_preview=True) logger.info(f"User info requested: {response}") await bot.start() return bot