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

feat: implement blacklist #160

Merged
merged 11 commits into from
Feb 25, 2024
2 changes: 2 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Add blacklist messages feature as anti-spam

...

## [3.1.0] - 2024-02-18
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ post:
# Otherwise the comment would be doubled.
# The bots must have delete permission in the group and comments must be enabled
replace_anonymous_comments: false
blacklist_messages: []
# example: ["spam_word_1", "spam_word_2"]
# the bot will delete any comment in the community chat that includes a word of the blacklist

token: xxxxxxxxxxxx # token of the telegram bot
bot_tag: "@bot" # tag of the telegram bot
Expand Down
2 changes: 2 additions & 0 deletions src/spotted/config/yaml/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ post:
delete_anonymous_comments: true
reject_after_autoreply: true
autoreplies_per_page: 6
blacklist_messages: ["myspamword1", "myspamword2"]

Helias marked this conversation as resolved.
Show resolved Hide resolved
token: ""
bot_tag: "@bot_tag"
1 change: 1 addition & 0 deletions src/spotted/config/yaml/settings.yaml.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ post:
delete_anonymous_comments: bool
reject_after_autoreply: bool
autoreplies_per_page: int
blacklist_messages: list
token: str
bot_tag: str
1 change: 1 addition & 0 deletions src/spotted/data/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"report_wait_mins",
"replace_anonymous_comments",
"delete_anonymous_comments",
"blacklist_messages",
]
SettingsKeysType = Literal[SettingsKeys, SettingsPostKeys, SettingsDebugKeys]
AutorepliesKeysType = Literal["autoreplies"]
Expand Down
10 changes: 10 additions & 0 deletions src/spotted/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from spotted.data.config import Config
from spotted.debug import error_handler, log_message
from spotted.handlers.spam_comment import spam_comment_msg

from .anonym_comment import anonymous_comment_msg
from .approve import approve_no_callback, approve_status_callback, approve_yes_callback
Expand Down Expand Up @@ -126,6 +127,7 @@ def add_handlers(app: Application):

if Config.post_get("comments"):
app.add_handler(MessageHandler(community_filter & filters.IS_AUTOMATIC_FORWARD, forwarded_post_msg))

if Config.post_get("delete_anonymous_comments"):
app.add_handler(
MessageHandler(
Expand All @@ -134,6 +136,14 @@ def add_handlers(app: Application):
)
)

if Config.post_get("blacklist_messages") and len(Config.post_get("blacklist_messages")) > 0:
app.add_handler(
MessageHandler(
community_filter,
spam_comment_msg,
)
)

app.add_handler(MessageHandler(community_filter & filters.REPLY, follow_spot_comment))


Expand Down
22 changes: 22 additions & 0 deletions src/spotted/handlers/spam_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Anonym Comment on a post in the comment group"""

from telegram import Update
from telegram.ext import CallbackContext

from spotted.data import Config
from spotted.utils import EventInfo


async def spam_comment_msg(update: Update, context: CallbackContext) -> None:
"""Handles a spam comment on a post in the comment group.
Deletes the original post.

Args:
update: update event
context: context passed by the handler
"""
info = EventInfo.from_message(update, context)
for message in Config.post_get("blacklist_messages"):
if message in info.message.text:
await info.message.delete()
return
22 changes: 20 additions & 2 deletions tests/integration/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,24 @@ async def test_anonymous_comment_msg(
assert telegram.last_message.text == "Anonymous comment"
assert telegram.last_message.from_user.is_bot is True

async def test_spam_comment_msg(
self, telegram: TelegramSimulator, published_post: Message, channel: Chat, channel_group: Chat
):
"""Tests the replacement of an anonymous comment.
Copies the message and deletes the original
"""
for word in Config.post_get("blacklist_messages"):
spam_comment = await telegram.send_message(
f"a message with the {word} will be deleted",
chat=channel_group,
reply_to_message=published_post.reply_to_message,
user=TGUser(10, first_name="user", is_bot=False),
sender_chat=channel,
)

assert telegram.get_message_by_id(spam_comment.message_id) is None # the spam comment is deleted
assert telegram.last_message.from_user.is_bot is True

class TestFollow:
"""Tests the follow feature"""

Expand Down Expand Up @@ -958,8 +976,8 @@ async def test_receive_follow_message(
message_thread_id=message_thread_id,
)
assert telegram.last_message.text == "Test follow"
assert telegram.last_message.from_user.is_bot is True
assert telegram.last_message.chat_id == user.id
# assert telegram.last_message.from_user.is_bot is True
# assert telegram.last_message.chat_id == user.id

async def test_skip_follow_message_same_user(
self, telegram: TelegramSimulator, published_post: Message, channel_group: Chat, user: TGUser
Expand Down
Loading