diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2c239b1 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 68bc17f..d12af23 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,6 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +# Ini config file +*.ini diff --git a/README.md b/README.md index 225fb66..b8d17d8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- Slickr + Discord-to-Telegram banner

Messages forwarder in real time by Websockets

@@ -10,68 +10,77 @@ Request Feature

- -Discord-To-Teleram forks - - -Discord-to-Telegram stars - - -Discord-to-Telegramissues - - -Discord-to-Telegram pull-requests - + + Discord-To-Teleram forks + + + Discord-to-Telegram stars + + + Discord-to-Telegramissues + + + Discord-to-Telegram pull-requests +

## :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 0000000..aff6a63 Binary files /dev/null and b/screenshots/banner.png differ