2021-09-12 19:36:23 +03:00
|
|
|
from abc import ABC
|
2021-09-12 18:22:26 +03:00
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
class SimpleCustomFilter(ABC):
|
|
|
|
"""
|
|
|
|
Simple Custom Filter base class.
|
|
|
|
Create child class with check() method.
|
|
|
|
Accepts only message, returns bool value, that is compared with given in handler.
|
|
|
|
"""
|
2021-09-12 18:22:26 +03:00
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
def check(self, message):
|
|
|
|
"""
|
|
|
|
Perform a check.
|
|
|
|
"""
|
|
|
|
pass
|
2021-09-12 18:22:26 +03:00
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
|
|
|
|
class AdvancedCustomFilter(ABC):
|
|
|
|
"""
|
|
|
|
Simple Custom Filter base class.
|
|
|
|
Create child class with check() method.
|
|
|
|
Accepts two parameters, returns bool: True - filter passed, False - filter failed.
|
|
|
|
message: Message class
|
|
|
|
text: Filter value given in handler
|
|
|
|
"""
|
|
|
|
|
|
|
|
def check(self, message, text):
|
|
|
|
"""
|
|
|
|
Perform a check.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TextMatchFilter(AdvancedCustomFilter):
|
2021-09-12 18:22:26 +03:00
|
|
|
"""
|
|
|
|
Filter to check Text message.
|
|
|
|
key: text
|
|
|
|
|
|
|
|
Example:
|
|
|
|
@bot.message_handler(text=['account'])
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = 'text'
|
|
|
|
|
|
|
|
def check(self, message, text):
|
|
|
|
if type(text) is list:return message.text in text
|
|
|
|
else: return text == message.text
|
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
class TextContainsFilter(AdvancedCustomFilter):
|
2021-09-12 18:22:26 +03:00
|
|
|
"""
|
|
|
|
Filter to check Text message.
|
|
|
|
key: text
|
|
|
|
|
|
|
|
Example:
|
|
|
|
# Will respond if any message.text contains word 'account'
|
|
|
|
@bot.message_handler(text_contains=['account'])
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = 'text_contains'
|
|
|
|
|
|
|
|
def check(self, message, text):
|
|
|
|
return text in message.text
|
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
class TextStartsFilter(AdvancedCustomFilter):
|
2021-09-12 18:22:26 +03:00
|
|
|
"""
|
2021-09-12 19:36:23 +03:00
|
|
|
Filter to check whether message starts with some text.
|
2021-09-12 18:22:26 +03:00
|
|
|
|
|
|
|
Example:
|
2021-09-12 19:36:23 +03:00
|
|
|
# Will work if message.text starts with 'Sir'.
|
|
|
|
@bot.message_handler(text_startswith='Sir')
|
2021-09-12 18:22:26 +03:00
|
|
|
"""
|
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
key = 'text_startswith'
|
2021-09-12 18:22:26 +03:00
|
|
|
def check(self, message, text):
|
2021-09-12 19:36:23 +03:00
|
|
|
return message.text.startswith(text)
|
2021-09-12 18:22:26 +03:00
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
class ChatFilter(AdvancedCustomFilter):
|
2021-09-12 18:22:26 +03:00
|
|
|
"""
|
2021-09-12 19:36:23 +03:00
|
|
|
Check whether chat_id corresponds to given chat_id.
|
2021-09-12 18:22:26 +03:00
|
|
|
|
|
|
|
Example:
|
2021-09-12 19:36:23 +03:00
|
|
|
@bot.message_handler(chat_id=[99999])
|
2021-09-12 18:22:26 +03:00
|
|
|
"""
|
|
|
|
|
2021-09-12 19:36:23 +03:00
|
|
|
key = 'chat_id'
|
2021-09-12 18:22:26 +03:00
|
|
|
def check(self, message, text):
|
2021-09-12 19:36:23 +03:00
|
|
|
return message.chat.id in text
|
2021-09-13 21:09:06 +03:00
|
|
|
|
|
|
|
class ForwardFilter(SimpleCustomFilter):
|
|
|
|
"""
|
2021-09-14 13:02:54 +03:00
|
|
|
Check whether message was forwarded from channel or group.
|
2021-09-13 21:09:06 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
@bot.message_handler(is_forwarded=True)
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = 'is_forwarded'
|
|
|
|
|
|
|
|
def check(self, message):
|
2021-09-14 13:02:54 +03:00
|
|
|
return message.forward_from_chat is not None
|
2021-09-13 21:09:06 +03:00
|
|
|
|
|
|
|
class IsReplyFilter(SimpleCustomFilter):
|
|
|
|
"""
|
|
|
|
Check whether message is a reply.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
@bot.message_handler(is_reply=True)
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = 'is_reply'
|
|
|
|
|
|
|
|
def check(self, message):
|
|
|
|
return message.reply_to_message is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LanguageFilter(AdvancedCustomFilter):
|
|
|
|
"""
|
|
|
|
Check users language_code.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
@bot.message_handler(language_code=['ru'])
|
|
|
|
"""
|
|
|
|
|
|
|
|
key = 'language_code'
|
|
|
|
|
|
|
|
def check(self, message, text):
|
|
|
|
if type(text) is list:return message.from_user.language_code in text
|
|
|
|
else: return message.from_user.language_code == text
|
|
|
|
|