Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filtering logic #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__pycache__
log.txt
config.toml
*.session
*.session
modules/
scratch.sh
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10.11
FROM --platform=linux/amd64 python:3.10.11

WORKDIR /usr/src/app

Expand Down
1 change: 1 addition & 0 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 21 additions & 20 deletions bot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
import logging
from time import sleep
import traceback

from pyrogram import filters

Expand All @@ -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():
Expand All @@ -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:
Expand All @@ -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()
11 changes: 10 additions & 1 deletion bot/helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pyrogram==2.0.106
TgCrypto
toml
toml
jsonschema