1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Added file custom_filters

Added file with custom filters. Updated the examples
This commit is contained in:
coder2020official
2021-09-12 20:22:26 +05:00
parent 1ceec3cb54
commit 7d5e9e5111
5 changed files with 72 additions and 60 deletions

View File

@ -185,7 +185,7 @@ class TeleBot:
self.poll_answer_handlers = []
self.my_chat_member_handlers = []
self.chat_member_handlers = []
self.custom_filters = {'text': util.TextFilter(), 'text_contains': util.TextContains(), 'chat_id': util.UserFilter(), 'text_startswith': util.TextStarts()}
self.custom_filters = {}
if apihelper.ENABLE_MIDDLEWARE:
self.typed_middleware_handlers = {

61
telebot/custom_filters.py Normal file
View File

@ -0,0 +1,61 @@
from telebot import util
class TextFilter(util.AdvancedCustomFilter):
"""
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
class TextContains(util.AdvancedCustomFilter):
"""
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
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):
"""
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)

View File

@ -486,62 +486,4 @@ class AdvancedCustomFilter(ABC):
"""
pass
class TextFilter(AdvancedCustomFilter):
"""
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
class TextContains(AdvancedCustomFilter):
"""
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
class UserFilter(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(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)