from telethon import TelegramClient, events import requests import telethon import aiohttp import nextcord import textwrap import os import requests import json import random from dotenv import load_dotenv '''This will load your .env file''' load_dotenv() url = os.environ.get("WEBHOOK") appid = os.environ.get("APPID") apihash = os.environ.get("APIHASH") apiname = os.environ.get("APINAME") webhookprofile = os.environ.get("WEBHOOK_NAME") mention = os.environ.get("MENTION_ROLE_ID") input_channels_entities = os.environ.get("INPUT_CHANNELS") blacklist = os.environ.get("BLACKLIST") if blacklist == 'True': blacklist = True if input_channels_entities is not None: input_channels_entities = list(map(int, input_channels_entities.split(','))) '''Multiple channel handler''' '''This method handles Telegram events and login system''' def start(): client = TelegramClient(apiname, appid, apihash) client.start() print("[TelegramForwarder]") print(f"Input channels: {input_channels_entities}") @client.on(events.NewMessage(chats=input_channels_entities, blacklist_chats=blacklist)) async def handler(event): # Log sent messages msg = event.message.message print(f"[Visible] {msg}") standard_message_sender("<@&" + mention + ">") webhook_sender(msg) # If a message is sent by a bot it won't be forwarded by Discord. if type(event.chat) == telethon.tl.types.User: return client.run_until_disconnected() '''HTTP request-based webhook embed message sending''' def webhook_sender(message): data = {"username": webhookprofile} data["embeds"] = [ { "title": "", "description": message, "color": 16777215 }] result = requests.post(url, json=data) try: result.raise_for_status() except requests.exceptions.HTTPError as err: print(err) '''HTTP request-based webhook message sending''' def standard_message_sender(message): data = { "content" : message, "username" : webhookprofile } result = requests.post(url, json = data) try: result.raise_for_status() except requests.exceptions.HTTPError as err: print(err) '''Main method''' if __name__ == "__main__": start()
## โ ๏ธ Troubleshooting ### My bot never started Make sure you have set the following environment variables correctly: - APPID - APIHASH - APINAME - WEBHOOK - WEBHOOK_NAME - INPUT_CHANNELS - MENTION_ROLE_ID - BLACKLIST ### The bot is not sending any message - Make sure the bot is connected to your server - Make sure you have set the bot's role above all other roles - Make sure you have entered the channel ID correctly - Make sure you have entered the role ID correctly ## ๐ Deploy on Heroku [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) ## ๐ License - **[MIT license](http://opensource.org/licenses/mit-license.php)**