from telethon import TelegramClient, events
import asyncio
import logging
# Configuración de logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
# ===================== CONFIGURACIÓN =====================
# Obtén estos valores de https://my.telegram.org
API_ID = 'TU_API_ID'
API_HASH = 'TU_API_HASH'
BOT_TOKEN = 'TU_BOT_TOKEN' # Obtén esto de @BotFather
# IDs de los canales (pueden ser username o ID numérico)
SOURCE_CHANNEL = '@canal_origen' # O -1001234567890
TARGET_CHANNEL = '@canal_destino' # O -1001234567890
# Extensiones de archivo a repostear
ALLOWED_EXTENSIONS = ('.zip', '.rar', '.7z', '.tar', '.gz', '.3mf', '.stl')
# =========================================================
# Crear el cliente del bot
bot = TelegramClient('bot_session', API_ID, API_HASH)
@bot.on(events.NewMessage(chats=SOURCE_CHANNEL))
async def repost_handler(event):
"""
Maneja los nuevos mensajes del canal origen y los repostea al destino
"""
try:
message = event.message
# Verificar si el mensaje tiene un archivo
if message.media and message.document:
# Obtener el nombre del archivo
file_name = None
for attribute in message.document.attributes:
if hasattr(attribute, 'file_name'):
file_name = attribute.file_name
break
# Verificar si es una extensión permitida
if file_name and any(file_name.lower().endswith(ext) for ext in ALLOWED_EXTENSIONS):
logger.info(f"📥 Reposteando archivo: {file_name}")
# Repostear el mensaje completo (archivo + caption si existe)
await bot.forward_messages(
entity=TARGET_CHANNEL,
messages=message.id,
from_peer=SOURCE_CHANNEL
)
logger.info(f"✅ Archivo reposteado exitosamente: {file_name}")
else:
logger.debug(f"⏭️ Archivo ignorado (extensión no permitida): {file_name}")
else:
logger.debug("⏭️ Mensaje sin archivo, ignorado")
except Exception as e:
logger.error(f"❌ Error al repostear mensaje: {e}")
async def main():
"""
Función principal para iniciar el bot
"""
logger.info("🤖 Iniciando bot...")
# Conectar con el token del bot
await bot.start(bot_token=BOT_TOKEN)
# Obtener información del bot
me = await bot.get_me()
logger.info(f"✅ Bot conectado: @{me.username}")
logger.info(f"📡 Escuchando mensajes en: {SOURCE_CHANNEL}")
logger.info(f"📤 Reposteando a: {TARGET_CHANNEL}")
logger.info(f"📁 Extensiones permitidas: {', '.join(ALLOWED_EXTENSIONS)}")
logger.info("🟢 Bot funcionando. Presiona Ctrl+C para detener.")
# Mantener el bot corriendo
await bot.run_until_disconnected()
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("🛑 Bot detenido por el usuario")