-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add word filters and blacklist (#98)
This update introduce two config on chat_list.json filters (Optional) - An array of strings to filter words. If the message containes any of the strings in the array, it WILL BE forwarded. blacklist (Optional) - An array of strings to blacklist words. If the message containes any of the string in the array, it will NOT BE forwarded.
- Loading branch information
Showing
7 changed files
with
146 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .chat import * | ||
from .message import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,90 @@ | ||
from typing import List, List, Union, TypedDict, Optional | ||
from typing import List, List, Union, Optional | ||
|
||
from forwarder import CONFIG | ||
|
||
|
||
class ChatConfig(TypedDict): | ||
chat_id: int | ||
thread_id: Optional[int] | ||
PARSED_CONFIG = [] | ||
|
||
|
||
def parse_topic(chat_id: Union[str, int]) -> ChatConfig: | ||
if isinstance(chat_id, str): | ||
raw = chat_id.split("#") | ||
if len(raw) == 2: | ||
return {"chat_id": int(raw[0]), "thread_id": int(raw[1])} | ||
return {"chat_id": int(raw[0]), "thread_id": None} | ||
class ChatConfig: | ||
__chat: Union[str, int] | ||
|
||
return {"chat_id": chat_id, "thread_id": None} | ||
def __init__(self, chat_id: Union[str, int]): | ||
self.__chat = chat_id | ||
|
||
def __repr__(self) -> str: | ||
if self.is_topic: | ||
return f"{self.get_id()}#{self.get_topic()}" | ||
return str(self.get_id()) | ||
|
||
def get_source() -> List[ChatConfig]: | ||
return [parse_topic(chat["source"]) for chat in CONFIG] | ||
@property | ||
def is_topic(self) -> bool: | ||
if isinstance(self.__chat, str) and len(self.__chat.split("#")) == 2: | ||
return True | ||
return False | ||
|
||
def get_topic(self) -> Optional[int]: | ||
if not self.is_topic: | ||
return None | ||
|
||
def get_destenation(chat_id: int, topic_id: Optional[int] = None) -> List[ChatConfig]: | ||
if isinstance(self.__chat, int): | ||
return None | ||
|
||
return int(self.__chat.split("#")[1]) | ||
|
||
def get_id(self) -> int: | ||
if isinstance(self.__chat, int): | ||
return self.__chat | ||
return int(self.__chat.split("#")[0]) | ||
|
||
|
||
class ForwardConfig: | ||
source: ChatConfig | ||
destination: List[ChatConfig] | ||
filters: Optional[List[str]] | ||
blacklist: Optional[List[str]] | ||
|
||
def __init__( | ||
self, | ||
source: Union[str, int], | ||
destination: List[Union[str, int]], | ||
filters: Optional[List[str]] = None, | ||
blacklist: Optional[List[str]] = None, | ||
): | ||
self.source = ChatConfig(source) | ||
self.destination = [ChatConfig(item) for item in destination] | ||
self.filters = filters | ||
self.blacklist = blacklist | ||
|
||
|
||
def get_config() -> List[ForwardConfig]: | ||
global PARSED_CONFIG | ||
if PARSED_CONFIG: | ||
return PARSED_CONFIG | ||
|
||
PARSED_CONFIG = [ | ||
ForwardConfig( | ||
source=chat["source"], | ||
destination=chat["destination"], | ||
filters=chat.get("filters"), | ||
blacklist=chat.get("blacklist"), | ||
) | ||
for chat in CONFIG | ||
] | ||
return PARSED_CONFIG | ||
|
||
|
||
def get_destination(chat_id: int, topic_id: Optional[int] = None) -> List[ForwardConfig]: | ||
"""Get destination from a specific source chat | ||
Args: | ||
chat_id (`int`): source chat id | ||
topic_id (`Optional[int]`): source topic id. Defaults to None. | ||
""" | ||
|
||
dest: List[ChatConfig] = [] | ||
|
||
for chat in CONFIG: | ||
parsed = parse_topic(chat["source"]) | ||
|
||
if parsed["chat_id"] == chat_id and parsed["thread_id"] == topic_id: | ||
dest.extend([parse_topic(item) for item in chat["destination"]]) | ||
dest: List[ForwardConfig] = [] | ||
|
||
for chat in get_config(): | ||
if chat.source.get_id() == chat_id and chat.source.get_topic() == topic_id: | ||
dest.append(chat) | ||
return dest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import re | ||
|
||
from typing import List | ||
|
||
|
||
def predicate_text(filters: List[str], text: str) -> bool: | ||
"""Check if the text contains any of the filters""" | ||
for i in filters: | ||
pattern = r"( |^|[^\w])" + re.escape(i) + r"( |$|[^\w])" | ||
if re.search(pattern, text, flags=re.IGNORECASE): | ||
return True | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters