Skip to content
Merged
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
6 changes: 6 additions & 0 deletions telebot/custom_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ def check(self, message, text):
elif isinstance(text, list) or isinstance(text, tuple):
text = [i for i in text if isinstance(i, str)]

if message.text is None:
return False
Comment on lines +250 to +251

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beduldul - please, fix also.

return any([i in message.text for i in text])


Expand All @@ -268,6 +270,8 @@ def check(self, message, text):
"""
:meta private:
"""
if message.text is None:
return False
return message.text.startswith(text)


Expand Down Expand Up @@ -452,4 +456,6 @@ def check(self, message):
"""
:meta private:
"""
if message.text is None:
return False
return message.text.isdigit()
52 changes: 52 additions & 0 deletions tests/test_custom_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import sys

sys.path.append('../')

import json

import pytest

from telebot import types
from telebot.custom_filters import IsDigitFilter, TextContainsFilter, TextStartsFilter


def make_message(**fields):
payload = {
"message_id": 1,
"date": 0,
"chat": {"id": 1, "type": "private"},
"from": {"id": 1, "is_bot": False, "first_name": "Test"},
}
payload.update(fields)
return types.Message.de_json(json.dumps(payload))


@pytest.fixture
def photo_message():
return make_message(
photo=[{"file_id": "f", "file_unique_id": "u", "width": 1, "height": 1}]
)


@pytest.fixture
def text_message():
return make_message(text="12345")


def test_text_contains_filter_skips_message_without_text(photo_message):
assert photo_message.text is None
assert TextContainsFilter().check(photo_message, "23") is False


def test_text_startswith_filter_skips_message_without_text(photo_message):
assert TextStartsFilter().check(photo_message, "12") is False


def test_is_digit_filter_skips_message_without_text(photo_message):
assert IsDigitFilter().check(photo_message) is False


def test_text_filters_still_match_text_messages(text_message):
assert TextContainsFilter().check(text_message, "23") is True
assert TextStartsFilter().check(text_message, "12") is True
assert IsDigitFilter().check(text_message) is True
Loading