diff --git a/.gitignore b/.gitignore index 6e1579a..14a8be1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ __pycache__ log.txt config.toml -*.session \ No newline at end of file +*.session +modules/ +scratch.sh \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 60bd49f..b26a452 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.10.11 +FROM --platform=linux/amd64 python:3.10.11 WORKDIR /usr/src/app diff --git a/bot/__init__.py b/bot/__init__.py index d11076c..be56bbd 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -46,6 +46,7 @@ logging.info(f"Monitored chats: {', '.join(str(x) for x in monitored_chats)}") logging.info(f"Chats map: {chats_map}") +logging.info(f"Sudo users: {sudo_users}") if config["pyrogram"].get("bot_token"): app = Client( diff --git a/bot/__main__.py b/bot/__main__.py index 8f40951..10b1289 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,6 +1,7 @@ import random import logging from time import sleep +import traceback from pyrogram import filters @@ -12,10 +13,10 @@ logging.info("Bot Started") -@app.on_message(filters.chat(monitored_chats) & filters.incoming) +@app.on_message(filters.chat(monitored_chats) & filters.all) def work(_:Client, message:Message): caption = None - msg = None + msg = message.text chat = chats_map.get(message.chat.id) if chat.get("replace"): for old, new in chat["replace"].items(): @@ -25,19 +26,17 @@ def work(_:Client, message:Message): msg = message.text.markdown.replace(old, new) try: for chat in chat["to"]: - if caption: + if caption and should_send_message(caption): message.copy(chat, caption=caption, parse_mode=ParseMode.MARKDOWN) - elif msg: + elif msg and should_send_message(msg): app.send_message(chat, msg, parse_mode=ParseMode.MARKDOWN) - else: - message.copy(chat) except Exception as e: logging.error(f"Error while sending message from {message.chat.id} to {chat}: {e}") @app.on_message(filters.user(sudo_users) & filters.command(["fwd", "forward"]), group=1) -def forward(app, message): - if len(message.command) > 1: +def forward(client:Client, message:Message): + if len(message.command) > 1 and message.command[1].isdigit(): chat_id = int(message.command[1]) if chat_id: try: @@ -47,23 +46,25 @@ def forward(app, message): limit = int(message.command[2]) if len(message.command) > 3: offset_id = int(message.command[3]) - for msg in app.iter_history(chat_id, limit=limit, offset_id=offset_id): + for msg in client.get_chat_history(chat_id, limit=limit, offset_id=offset_id): msg.copy(message.chat.id) - sleep(random.randint(1, 10)) + sleep(random.randint(1, 5)) except Exception as e: - message.reply_text(f"```{e}```") + message.reply_text(f"Error:\n```{traceback.format_exc()}```") else: - reply = message.reply_text( - "```Invalid Chat Identifier. Give me a chat id, username or message link.```" + message.reply_text( + "Invalid Chat Identifier. Give me a chat id." ) - sleep(5) - reply.delete() else: - reply = message.reply_text( - "```Invalid Command ! Use /fwd {ChatID} {limit} {FirstMessageID}```" + message.reply_text( + "Invalid Command\nUse /fwd {chat_id} {limit} {first_message_id}" ) - sleep(20) - reply.delete() -app.run() +def should_send_message(message): + keywords = chats_map["filter"] + if not keywords or len(keywords) == 0: + return True + return any(keyword in message.lower() for keyword in keywords) + +app.run() \ No newline at end of file diff --git a/bot/helper/utils.py b/bot/helper/utils.py index 57929d2..9af306d 100644 --- a/bot/helper/utils.py +++ b/bot/helper/utils.py @@ -21,18 +21,27 @@ def parse_chats(chats): # Set all from values to a list and, from => [to, replace] in dict monitored_chats = set() chats_map = {} + filter = [] + chats_map["filter"] = [] for chat in chats: from_chats = chat["from"] to_chats = chat["to"] + filter = chat.get("filter") replace = chat.get("replace") - + if not isinstance(from_chats, list): from_chats = [from_chats] if not isinstance(to_chats, list): to_chats = [to_chats] + if not isinstance(filter, list) and filter is not None: + filter = [filter] + + if filter: + chats_map["filter"].extend(filter) + for from_chat in from_chats: if from_chat not in chats_map: chats_map[from_chat] = {"to": set(), "replace": replace} diff --git a/requirements.txt b/requirements.txt index 46190cb..42a0aa4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pyrogram==2.0.106 TgCrypto -toml \ No newline at end of file +toml +jsonschema \ No newline at end of file