diff --git a/.DS_Store b/.DS_Store index 2c239b1..6aa831f 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/main.py b/main.py index dc4cff0..a31326c 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,14 @@ import asyncio +import configparser import json +import logging import ssl -import configparser + import websockets -from telethon import TelegramClient -import logging import websockets.exceptions +from telethon import TelegramClient + +from utils.middleware import should_send # Configuring the logging logging.basicConfig(level=logging.INFO) @@ -13,7 +16,7 @@ config = configparser.ConfigParser() config.read("config.ini") -# Telegram settings +# Telegram settings api_id = config.getint("TELEGRAM", "API_ID") api_hash = config.get("TELEGRAM", "API_HASH") client_name = config.get("TELEGRAM", "CLIENT_NAME") @@ -27,10 +30,12 @@ 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) @@ -41,6 +46,7 @@ async def heartbeat(ws, interval, last_sequence): await ws.send(json.dumps(payload)) logging.info("Heartbeat packet sent.") + async def identify(ws): identify_payload = { "op": 2, @@ -56,17 +62,18 @@ async def identify(ws): 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') @@ -76,12 +83,15 @@ async def on_message(ws): if channel_id == channel_id_to_monitor and message != '': logging.info(f"Message received from Discord: {message}") - await send_to_telegram(f"{message}") - + if should_send(message): + # comment this line if you dont want to use middlware + 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 @@ -93,7 +103,8 @@ async def main(): await identify(ws) await on_message(ws) except websockets.exceptions.ConnectionClosed as e: - logging.error(f"WebSocket connection closed unexpectedly: {e}. Reconnecting...") + logging.error( + f"WebSocket connection closed unexpectedly: {e}. Reconnecting...") await asyncio.sleep(5) continue diff --git a/screenshots/discord_auth_token.png b/screenshots/discord_auth_token.png new file mode 100644 index 0000000..a71d9cb Binary files /dev/null and b/screenshots/discord_auth_token.png differ diff --git a/screenshots/telegram_apps.png b/screenshots/telegram_apps.png new file mode 100644 index 0000000..3cad34a Binary files /dev/null and b/screenshots/telegram_apps.png differ diff --git a/utils/middleware.py b/utils/middleware.py new file mode 100644 index 0000000..dfd6b22 --- /dev/null +++ b/utils/middleware.py @@ -0,0 +1,47 @@ +import re + +""" + Feel free to improve this code. + I do something basic that suits my needs. +""" + + +important_keywords = [ + "EURGBP", + "AUDJPY", + "EURAUD", + "EURCHF", + "AUDNZD", + "NZDJPY", + "GBPAUD", + "GBPCAD", + "EURNZD", + "AUDCAD", + "GBPCHF", + "AUDCHF", + "EURCAD", + "CADJPY", + "GBPNZD", + "CADCHF", + "CHFJPY", + "NZDCAD", + "NZDCHF", + "XAUUSD", + "EURUSD", + "USDJPY", + "GBPUSD", + "AUDUSD", + "USDCAD", + "USDCHF", + "NZDUSD", + "EURJPY", + "GBPJPY" +] + + +def should_send(message): + cleaned_message = re.sub(r'\s+AT\s+\d+\.\d+\s+\(.*\)', '', message) + + if any(keyword in cleaned_message.upper() for keyword in important_keywords): + return cleaned_message.strip() + \ No newline at end of file