Skip to content

Instantly share code, notes, and snippets.

@itspyguru
Created May 11, 2025 18:48
Show Gist options
  • Save itspyguru/4e8939f43db0e71289a9b1537c917710 to your computer and use it in GitHub Desktop.
Save itspyguru/4e8939f43db0e71289a9b1537c917710 to your computer and use it in GitHub Desktop.
Telegram Channel bulk content forwarder
# pip install python-telegram-bot
# you can get channel id by forwarding any message to idBot on telegram
from telegram import Bot
from telegram.error import TelegramError, RetryAfter
from loguru import logger
import asyncio
# Replace these with your values
BOT_TOKEN = '7635842250:AAGFijfxxxxxxxxxxxxxxxxxxxxxxxxx'
SOURCE_CHANNEL_ID = '-10019xxxxxxx'
DESTINATION_CHANNEL_ID = '-10025xxxxxxxx'
async def forward_messages():
"""Forward all messages from source channel to destination channel."""
bot = Bot(token=BOT_TOKEN)
try:
# Get bot information
bot_info = await bot.get_me()
logger.info(f"Bot started: {bot_info.first_name}")
latest_message_id = 1000
forwarded_count = 0
for message_id in range(1, latest_message_id+1):
try:
await bot.copy_message(
chat_id=DESTINATION_CHANNEL_ID,
from_chat_id=SOURCE_CHANNEL_ID,
message_id=message_id
)
forwarded_count += 1
logger.info(f"Forwarded message {message_id}. Total: {forwarded_count}")
await asyncio.sleep(2)
except RetryAfter as e:
retry_after = int(str(e).split()[-2])
logger.info(f"Hit rate limit. Waiting for {retry_after} seconds...")
await asyncio.sleep(retry_after)
# Retry the same message after waiting
try:
await bot.copy_message(
chat_id=DESTINATION_CHANNEL_ID,
from_chat_id=SOURCE_CHANNEL_ID,
message_id=message_id
)
forwarded_count += 1
logger.info(f"Successfully forwarded message {message_id} after waiting")
except TelegramError as e:
logger.error(f"Error forwarding message {message_id} after retry: {e}")
continue
except TelegramError as e:
logger.error(f"Error forwarding message {message_id}: {e}")
continue
logger.info(f"Forwarding completed. Total messages forwarded: {forwarded_count}")
except TelegramError as e:
logger.error(f"Bot error: {e}")
finally:
await bot.close()
if __name__ == '__main__':
logger.info("Starting message forwarding...")
asyncio.run(forward_messages())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment