I hope someone here can assist me with an issue I'm encountering in my Discord bot project. I'm working on a bot that should forward messages sent in a Telegram channel, where the bot has been added, to a Discord server via webhook.
I've properly configured the bot on Telegram and successfully obtained the webhook on Discord. However, I'm unable to get messages sent in the Telegram channel to be forwarded correctly to the Discord server via the webhook.
I've carefully reviewed the documentation for both platforms and followed all necessary steps, but I haven't been able to resolve the issue.
I would greatly appreciate it if someone with experience in this area could lend me a hand or suggest where I might have made errors. Any help or advice would be extremely valuable.
Thank you in advance for your time and assistance.
import osimport loggingimport requestsfrom telegram import Update, Botfrom telegram.ext import Updater, CallbackContext, CommandHandler, MessageHandler, Filters# Set up logginglogging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)logger = logging.getLogger(__name__)# Set up Telegram bot and Discord webhookTELEGRAM_BOT_TOKEN = ''DISCORD_WEBHOOK_URL = ''# Create Telegram bot updater and dispatcherupdater = Updater(token=TELEGRAM_BOT_TOKEN, use_context=True)dispatcher = updater.dispatcher# Define function to send messages to Discord webhookdef send_to_discord(message): # Prepare the data to send data = {"content": message } # Send the POST request response = requests.post(DISCORD_WEBHOOK_URL, json=data) # Check if the request was successful if response.status_code == 204: logger.info("Message sent to Discord successfully") else: logger.error(f"Failed to send message to Discord: {response.status_code} {response.reason}")# Define function to handle messages and forward them to Discorddef handle_message(update: Update, context: CallbackContext): message = update.message if message is not None: message_text = message.text logger.info(f"Received message from Telegram: {message_text}") # Forward the message to Discord send_to_discord(message_text) else: logger.warning("Received an update without a message")# Add a message handler to the dispatchermessage_handler = MessageHandler(Filters.text & (~Filters.command), handle_message)dispatcher.add_handler(message_handler)# Define function to forward existing messages in the channeldef forward_existing_messages(): # Create a Telegram bot instance bot = Bot(token=TELEGRAM_BOT_TOKEN) # Retrieve the last update ID updates = bot.get_updates() last_update_id = updates[-1].update_id if updates else 0 # Retrieve the messages in the channel with an offset updates = bot.get_updates(offset=last_update_id + 1) # Loop through the messages and forward them to Discord for update in updates: message = update.message message_text = message.text logger.info(f"Forwarding existing message from Telegram: {message_text}") # Forward the message to Discord send_to_discord(message_text)# Forward existing messages in the channelforward_existing_messages()# Start the botupdater.start_polling()updater.idle()