Skip to content

Commit

Permalink
Merge pull request #55 from hyp3rd/fix/API
Browse files Browse the repository at this point in the history
Added basic anti-spam filter feature
  • Loading branch information
hyp3rd committed Dec 18, 2023
2 parents 48ae447 + 9dcaa56 commit 87619e8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: local
hooks:
- id: bump-version
name: bump the version
language: system
entry: ./tools/bump-version.bash
types: [python]
# - repo: local
# hooks:
# - id: bump-version
# name: bump the version
# language: system
# entry: ./tools/bump-version.bash
# types: [python]

- repo: local
hooks:
Expand Down
1 change: 1 addition & 0 deletions api/routers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async def get_config(self) -> ConfigSchema:
debug=config.application.debug,
healthcheck_interval=config.application.healthcheck_interval,
recoverer_delay=config.application.recoverer_delay,
anti_spam_enabled=config.application.anti_spam_enabled,
)

api_config = APIConfig(
Expand Down
7 changes: 1 addition & 6 deletions bridge/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class ApplicationConfig(BaseModel): # pylint: disable=too-few-public-methods
healthcheck_interval: int = 60
recoverer_delay: float = 60.0
internet_connected: bool = False
anti_spam_enabled: bool = False

@validator("version")
def version_validator(cls, val):
Expand Down Expand Up @@ -386,12 +387,6 @@ class ConfigYAMLSchema(BaseModel): # pylint: disable=too-few-public-methods
openai: OpenAIConfig
telegram_forwarders: List[ForwarderConfig]

# def __getattr__(self, item):
# try:
# return self[item]
# except KeyError:
# raise AttributeError(f"{item} not found in ConfigYAMLSchema")

@model_validator(mode="before")
def forwarder_validator(cls, values):
"""Validate forwarder combinations to avoid duplicates."""
Expand Down
10 changes: 10 additions & 0 deletions bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ async def _handle_new_message(

tg_channel_id = message.peer_id.channel_id # type: ignore

if config.application.anti_spam_enabled:
# check for duplicate messages
if await self.history_manager.is_duplicate_message(
telegram_message=message,
channel_id=tg_channel_id,
tgc=self.telegram_client,
):
logger.debug("Duplicate message found, skipping...")
return

matching_forwarders: List[ForwarderConfig] = self.get_matching_forwarders(
tg_channel_id
)
Expand Down
21 changes: 21 additions & 0 deletions bridge/history/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import aiofiles
from telethon import TelegramClient
from telethon.tl.types import Message

from bridge.config import Config
from bridge.logger import Logger
Expand Down Expand Up @@ -176,3 +177,23 @@ async def fetch_messages_after(
logger.debug("Fetched message: %s", message.id)
messages.append(message)
return messages

async def is_duplicate_message(
self, telegram_message: Message, channel_id: int, tgc: TelegramClient
) -> bool:
"""Detect if a message with the same text was already sent based in the past 30 seconds."""
logger.info("Checking if message is duplicate")
async for message in tgc.iter_messages(channel_id, limit=10, reverse=False):
logger.debug("Checking message: %s", message.id)
logger.debug("Message text: %s", message.text)
logger.debug("Current message text: %s", telegram_message.text)
if (
message.text == telegram_message.text
and message.id != telegram_message.id
):
if message.date.timestamp() > (asyncio.get_event_loop().time() - 30):
logger.debug("Message is duplicate")
return True
break
logger.debug("Message is not duplicate")
return False
7 changes: 3 additions & 4 deletions forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ def __controller(self, start_forwarding: bool = True):
self.logger.info("Version: %s", config.application.version)
self.logger.info("Description: %s", config.application.description)
self.logger.info("Log level: %s", config.logger.level)
self.logger.info(
"Anti-Spam enabled: %s", config.application.anti_spam_enabled
)
self.logger.info("Debug enabled: %s", config.application.debug)
self.logger.info("API enabled: %s", config.api.enabled)
self.logger.info(
Expand Down Expand Up @@ -256,10 +259,6 @@ def __start(self):
ex,
exc_info=config.application.debug,
)
finally:
# Remove the PID file.
if not config.api.enabled:
self.remove_pid_file()

def __stop(self):
"""Stop the bridge."""
Expand Down
3 changes: 2 additions & 1 deletion tools/bump-version.bash
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ bump_version() {
sed -i '' 's/ghcr.io\/hyp3rd\/bridge:.*/ghcr.io\/hyp3rd\/bridge:'"$new_version"'/' README.md
}

bump_version && git add bridge/release.py README.md
# bump_version && git add bridge/release.py README.md
bump_version

0 comments on commit 87619e8

Please sign in to comment.