mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Update custom filters readme and examples
This commit is contained in:
@ -12,7 +12,7 @@ from typing import Any, Callable, List, Optional, Union
|
||||
# this imports are used to avoid circular import error
|
||||
import telebot.util
|
||||
import telebot.types
|
||||
|
||||
from custom_filters import SimpleCustomFilter, AdvancedCustomFilter
|
||||
|
||||
|
||||
logger = logging.getLogger('TeleBot')
|
||||
@ -3140,9 +3140,9 @@ class TeleBot:
|
||||
filter_check = self.custom_filters.get(message_filter)
|
||||
if not filter_check:
|
||||
return False
|
||||
elif isinstance(filter_check, util.SimpleCustomFilter):
|
||||
elif isinstance(filter_check, SimpleCustomFilter):
|
||||
return filter_value == filter_check.check(message)
|
||||
elif isinstance(filter_check, util.AdvancedCustomFilter):
|
||||
elif isinstance(filter_check, AdvancedCustomFilter):
|
||||
return filter_check.check(message, filter_value)
|
||||
else:
|
||||
logger.error("Custom filter: wrong type. Should be SimpleCustomFilter or AdvancedCustomFilter.")
|
||||
|
@ -1,8 +1,36 @@
|
||||
from telebot import util
|
||||
from abc import ABC
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
def check(self, message):
|
||||
"""
|
||||
Perform a check.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
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
|
||||
"""
|
||||
|
||||
class TextFilter(util.AdvancedCustomFilter):
|
||||
def check(self, message, text):
|
||||
"""
|
||||
Perform a check.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class TextMatchFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Filter to check Text message.
|
||||
key: text
|
||||
@ -17,7 +45,7 @@ class TextFilter(util.AdvancedCustomFilter):
|
||||
if type(text) is list:return message.text in text
|
||||
else: return text == message.text
|
||||
|
||||
class TextContains(util.AdvancedCustomFilter):
|
||||
class TextContainsFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Filter to check Text message.
|
||||
key: text
|
||||
@ -32,30 +60,27 @@ class TextContains(util.AdvancedCustomFilter):
|
||||
def check(self, message, text):
|
||||
return text in message.text
|
||||
|
||||
class UserFilter(util.AdvancedCustomFilter):
|
||||
"""
|
||||
Check whether chat_id corresponds to given chat_id.
|
||||
|
||||
Example:
|
||||
@bot.message_handler(chat_id=[99999])
|
||||
|
||||
"""
|
||||
|
||||
key = 'chat_id'
|
||||
def check(self, message, text):
|
||||
return message.chat.id in text
|
||||
|
||||
|
||||
class TextStarts(util.AdvancedCustomFilter):
|
||||
class TextStartsFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Filter to check whether message starts with some text.
|
||||
|
||||
Example:
|
||||
# Will work if message.text starts with 'Sir'.
|
||||
@bot.message_handler(text_startswith='Sir')
|
||||
|
||||
"""
|
||||
|
||||
key = 'text_startswith'
|
||||
def check(self, message, text):
|
||||
return message.text.startswith(text)
|
||||
|
||||
class ChatFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Check whether chat_id corresponds to given chat_id.
|
||||
|
||||
Example:
|
||||
@bot.message_handler(chat_id=[99999])
|
||||
"""
|
||||
|
||||
key = 'chat_id'
|
||||
def check(self, message, text):
|
||||
return message.chat.id in text
|
||||
|
@ -5,7 +5,6 @@ import string
|
||||
import threading
|
||||
import traceback
|
||||
from typing import Any, Callable, List, Dict, Optional, Union
|
||||
from abc import ABC
|
||||
|
||||
# noinspection PyPep8Naming
|
||||
import queue as Queue
|
||||
@ -456,34 +455,3 @@ def webhook_google_functions(bot, request):
|
||||
return 'Bot FAIL', 400
|
||||
else:
|
||||
return 'Bot ON'
|
||||
|
||||
|
||||
class SimpleCustomFilter(ABC):
|
||||
"""
|
||||
Simple Custom Filter base class.
|
||||
Create child class with check() method.
|
||||
Accepts only bool.
|
||||
"""
|
||||
|
||||
def check(self, message):
|
||||
"""
|
||||
Perform a check.
|
||||
"""
|
||||
pass
|
||||
|
||||
class AdvancedCustomFilter(ABC):
|
||||
"""
|
||||
Simple Custom Filter base class.
|
||||
Create child class with check() method.
|
||||
Can accept to parameters.
|
||||
message: Message class
|
||||
text: Filter value given in handler
|
||||
"""
|
||||
|
||||
def check(self, message, text):
|
||||
"""
|
||||
Perform a check.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
Reference in New Issue
Block a user