From 5f5b7325a510147f563b6917442b5603901f8e64 Mon Sep 17 00:00:00 2001
From: Joa Roque
- - - - - - - - - - - - + + + + + + + + + + + +
## :arrow_down: Installation + To get a local copy installed and working, follow these steps: - - Clone this repository +- Clone this repository + + ```console + git clone https://github.com/joaroque/Discord-to-Telegram.git + ``` - ```console - git clone https://github.com/joaroque/Discord-to-Telegram.git - ``` - - - Enter the project folder +- Enter the project folder - ```sh - cd Discord-to-Telegram - ``` + ```sh + cd Discord-to-Telegram + ``` ### 📦 Install dependencies -Note: use `pip install -r requirements .txt` to install all dependencies. +> optional commands +1. Create a virtual env + + ```shell + python3 -m venv venv + ``` + +1. Active virtual env + +1. Use `pip install -r requirements.txt` to install all dependencies. ### 🚀 Setup the bot - 1. Get telegram client (credentials)[https://my.telegram.org/auth] - - 2. Get discord token on Chrome Devtools request monitoring +1. Get telegram client [credentials](https://my.telegram.org/auth) + +2. Get discord token on Chrome Devtools request monitoring + +3. Insert your token in the `.ini` file - 3. Insert your token in the `.ini` file + ```ini + [TELEGRAM] + API_ID = + API_HASH = + CLIENT_NAME = + DEST_CHANNEL = -100123456789 - ```ini - [TELEGRAM] - API_ID = - API_HASH = - CLIENT_NAME = - DEST_CHANNEL = -100123456789 - - [DISCORD] - SOURCE_CHANNEL = - AUTH_TOKEN = - HEARTBEAT_INTERVAL = 100 + [DISCORD] + SOURCE_CHANNEL = + AUTH_TOKEN = + HEARTBEAT_INTERVAL = 100 - ``` + ``` - 4. Start the bot +4. Start the bot - ```shell + ```shell python main.py - ``` - + ``` ## Meta diff --git a/config.ini.example b/config.ini.example new file mode 100644 index 0000000..dd7cd08 --- /dev/null +++ b/config.ini.example @@ -0,0 +1,10 @@ +[TELEGRAM] +API_ID = +API_HASH = +CLIENT_NAME = +DEST_CHANNEL = -100123456789 + +[DISCORD] +SOURCE_CHANNEL = 1131604317896433396 +AUTH_TOKEN = +HEARTBEAT_INTERVAL = 100 diff --git a/main.py b/main.py new file mode 100644 index 0000000..dc4cff0 --- /dev/null +++ b/main.py @@ -0,0 +1,101 @@ +import asyncio +import json +import ssl +import configparser +import websockets +from telethon import TelegramClient +import logging +import websockets.exceptions + +# Configuring the logging +logging.basicConfig(level=logging.INFO) + +config = configparser.ConfigParser() +config.read("config.ini") + +# Telegram settings +api_id = config.getint("TELEGRAM", "API_ID") +api_hash = config.get("TELEGRAM", "API_HASH") +client_name = config.get("TELEGRAM", "CLIENT_NAME") +destination_channel = config.getint("TELEGRAM", "DEST_CHANNEL") + +# Discord settings +channel_id_to_monitor = config.get("DISCORD", "SOURCE_CHANNEL") +token = config.get("DISCORD", "AUTH_TOKEN") + +discord_ws_url = "wss://gateway.discord.gg/?v=6&encoding=json" + +client = TelegramClient(client_name, api_id, api_hash) + +async def send_to_telegram(message): + await client.send_message(destination_channel, message) + logging.info(f"Message sent to Telegram: {message}") + +async def heartbeat(ws, interval, last_sequence): + while True: + await asyncio.sleep(interval) + payload = { + "op": 1, + "d": last_sequence + } + await ws.send(json.dumps(payload)) + logging.info("Heartbeat packet sent.") + +async def identify(ws): + identify_payload = { + "op": 2, + "d": { + "token": token, + "properties": { + "$os": "windows", + "$browser": "chrome", + "$device": "pc" + } + } + } + await ws.send(json.dumps(identify_payload)) + logging.info("Identification sent.") + +async def on_message(ws): + last_sequence = None + while True: + event = json.loads(await ws.recv()) + logging.info(f"Event received: {event}") + op_code = event.get('op', None) + + if op_code == 10: + interval = event['d']['heartbeat_interval'] / 1000 + asyncio.create_task(heartbeat(ws, interval, last_sequence)) + + elif op_code == 0: + last_sequence = event.get('s', None) + event_type = event.get('t') + if event_type == 'MESSAGE_CREATE': + channel_id = event['d']['channel_id'] + message = event['d']['content'] + if channel_id == channel_id_to_monitor and message != '': + logging.info(f"Message received from Discord: {message}") + + await send_to_telegram(f"{message}") + + elif op_code == 9: + logging.info("Invalid session. Starting a new session...") + await identify(ws) + +async def main(): + ssl_context = ssl.create_default_context() + ssl_context.check_hostname = False + ssl_context.verify_mode = ssl.CERT_NONE + + while True: + try: + async with websockets.connect(discord_ws_url, ssl=ssl_context) as ws: + await identify(ws) + await on_message(ws) + except websockets.exceptions.ConnectionClosed as e: + logging.error(f"WebSocket connection closed unexpectedly: {e}. Reconnecting...") + await asyncio.sleep(5) + continue + +with client: + client.loop.run_until_complete(main()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b7ac49e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,6 @@ +pyaes==1.6.1 +pyasn1==0.5.0 +rsa==4.9 +Telethon==1.28.5 +websocket-client==1.6.1 +websockets==11.0.3 diff --git a/screenshots/banner.png b/screenshots/banner.png new file mode 100644 index 0000000000000000000000000000000000000000..aff6a631409d5fcf142f5eac294f38fdb5ab4f04 GIT binary patch literal 211478 zcmXty
zLGeQY4fia}s<#43x&?oVv1rUJ9mK8(nzebS 6x!gDj<6L{xricdK19ZUcV&;PA<^RWmbUDVerrmm&PdBr7=1B0>sF|_iqoUJT4gi
z@ad+CH^E?MLgcqmJ{&~7Pgj`wm`jr`&5m($lIN{i6v)8uDW~N-K3!X(vQ4xV{(+PP
znyr{g`R-BRSwm&u>lOt&_0@D*dY(h-++cfOcj=hnJ=u=+V2HXk=?833F`j$-z%I!?
zu0mRMj<)ABC90|f7X1w@v-BhqM8GjXRF*gA949m?+Y1)cpZs`*&>3e__CEbb$_kAD
z6H>$0)YAClOL+YzaMqQ1*BvJ8vAT0l!a_GB&BC(j2p
zN`tf$`@GM9S5%|xzeP)*x@V&Hyn(;!%n^0=h&s2UY*@=14B`o=^N#oRBI(TZ+X(rTF;YjYQ2
zVY5q=GNM%1RgMQyl_|EHhyOo?XRkknt&`f&!ma$ArE$Yg#^X1g&8?AC&{q8TD
z6%jx_s;Gdtxlm+Hv@TSrT5pEuZ=Rb@6j{OT+uIy|5XTM4=JEVFlH{IeUwV~tEqcq8
z9DOw4*i5R1HtUt%^Kd9p#Cs_Et@SS&nH71f)_8h-okZ!Ou;jbjEB{wu1a|hWc$N{v
z0;`W-6~XD^yox3DtS!CEVL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