Skip to content

Commit

Permalink
🎉 first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joaroque committed Aug 5, 2023
1 parent d2231e9 commit 5f5b732
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 43 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
95 changes: 52 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<a href="https://github.com/joaroque/Discord-to-Telegram">
<img alt="Slickr" src="screenshot/banner.png" width="630" />
<img alt="Discord-to-Telegram banner" src="screenshots/banner.png" width="630" />
</a>
</p>
<h2 align="center">Messages forwarder in real time by Websockets</h2>
Expand All @@ -10,68 +10,77 @@
<a href="https://github.com/joaroque/Discord-to-Telegram/issues">Request Feature</a>
</p>
<p align="center">
<a href="https://github.com/joaroque/Discord-to-Telegram/fork" target="blank">
<img src="https://img.shields.io/github/forks/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-To-Teleram forks"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/stargazers" target="blank">
<img src="https://img.shields.io/github/stars/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-to-Telegram stars"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/issues" target="blank">
<img src="https://img.shields.io/github/issues/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-to-Telegramissues"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/pulls" target="blank">
<img src="https://img.shields.io/github/issues-pr/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-to-Telegram pull-requests"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/fork" target="blank">
<img src="https://img.shields.io/github/forks/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-To-Teleram forks"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/stargazers" target="blank">
<img src="https://img.shields.io/github/stars/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-to-Telegram stars"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/issues" target="blank">
<img src="https://img.shields.io/github/issues/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-to-Telegramissues"/>
</a>
<a href="https://github.com/joaroque/Discord-to-Telegram/pulls" target="blank">
<img src="https://img.shields.io/github/issues-pr/joaroque/Discord-to-Telegram?style=flat-square" alt="Discord-to-Telegram pull-requests"/>
</a>
</p>

## :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

Expand Down
10 changes: 10 additions & 0 deletions config.ini.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[TELEGRAM]
API_ID =
API_HASH =
CLIENT_NAME =
DEST_CHANNEL = -100123456789

[DISCORD]
SOURCE_CHANNEL = 1131604317896433396
AUTH_TOKEN =
HEARTBEAT_INTERVAL = 100
101 changes: 101 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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())
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Binary file added screenshots/banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5f5b732

Please sign in to comment.