mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
removed TextFilterKey in example, instead TextMatchFilter was modified
This commit is contained in:
parent
3cd86d0e93
commit
5b1483f646
@ -9,16 +9,9 @@ import asyncio
|
||||
from telebot.async_telebot import AsyncTeleBot
|
||||
from telebot import types
|
||||
from telebot.custom_filters import TextFilter
|
||||
from telebot.asyncio_filters import AdvancedCustomFilter
|
||||
from telebot.asyncio_filters import TextMatchFilter
|
||||
|
||||
bot = AsyncTeleBot("1254795383:AAE7gbj1aas4lEDHB1eVuZZhSGPWcH1B5ds")
|
||||
|
||||
|
||||
class TextFilterKey(AdvancedCustomFilter):
|
||||
key = 'text'
|
||||
|
||||
async def check(self, message, config: TextFilter):
|
||||
return config.check(message)
|
||||
bot = AsyncTeleBot("")
|
||||
|
||||
|
||||
@bot.message_handler(text=TextFilter(equals='hello'))
|
||||
@ -99,5 +92,5 @@ async def poll_question_handler_ignore_case(poll: types.Poll):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
bot.add_custom_filter(TextFilterKey())
|
||||
bot.add_custom_filter(TextMatchFilter())
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -6,18 +6,11 @@ with (message_handler, callback_query_handler, poll_handler)
|
||||
"""
|
||||
|
||||
from telebot import TeleBot, types
|
||||
from telebot.custom_filters import TextFilter, AdvancedCustomFilter
|
||||
from telebot.custom_filters import TextFilter, TextMatchFilter
|
||||
|
||||
bot = TeleBot("")
|
||||
|
||||
|
||||
class TextFilterKey(AdvancedCustomFilter):
|
||||
key = 'text'
|
||||
|
||||
def check(self, message, config: TextFilter):
|
||||
return config.check(message)
|
||||
|
||||
|
||||
@bot.message_handler(text=TextFilter(equals='hello'))
|
||||
def hello_handler(message: types.Message):
|
||||
bot.send_message(message.chat.id, message.text)
|
||||
@ -96,5 +89,5 @@ def poll_question_handler_ignore_case(poll: types.Poll):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
bot.add_custom_filter(TextFilterKey())
|
||||
bot.add_custom_filter(TextMatchFilter())
|
||||
bot.infinity_polling()
|
||||
|
@ -1,5 +1,8 @@
|
||||
from abc import ABC
|
||||
|
||||
from telebot.custom_filters import TextFilter
|
||||
|
||||
|
||||
class SimpleCustomFilter(ABC):
|
||||
"""
|
||||
Simple Custom Filter base class.
|
||||
@ -42,8 +45,13 @@ class TextMatchFilter(AdvancedCustomFilter):
|
||||
key = 'text'
|
||||
|
||||
async def check(self, message, text):
|
||||
if type(text) is list:return message.text in text
|
||||
else: return text == message.text
|
||||
if isinstance(text, TextFilter):
|
||||
return text.check(message)
|
||||
elif type(text) is list:
|
||||
return message.text in text
|
||||
else:
|
||||
return text == message.text
|
||||
|
||||
|
||||
class TextContainsFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
@ -60,6 +68,7 @@ class TextContainsFilter(AdvancedCustomFilter):
|
||||
async def check(self, message, text):
|
||||
return text in message.text
|
||||
|
||||
|
||||
class TextStartsFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Filter to check whether message starts with some text.
|
||||
@ -70,8 +79,10 @@ class TextStartsFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
|
||||
key = 'text_startswith'
|
||||
|
||||
async def check(self, message, text):
|
||||
return message.text.startswith(text)
|
||||
return message.text.startswith(text)
|
||||
|
||||
|
||||
class ChatFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
@ -82,9 +93,11 @@ class ChatFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
|
||||
key = 'chat_id'
|
||||
|
||||
async def check(self, message, text):
|
||||
return message.chat.id in text
|
||||
|
||||
|
||||
class ForwardFilter(SimpleCustomFilter):
|
||||
"""
|
||||
Check whether message was forwarded from channel or group.
|
||||
@ -99,6 +112,7 @@ class ForwardFilter(SimpleCustomFilter):
|
||||
async def check(self, message):
|
||||
return message.forward_from_chat is not None
|
||||
|
||||
|
||||
class IsReplyFilter(SimpleCustomFilter):
|
||||
"""
|
||||
Check whether message is a reply.
|
||||
@ -114,7 +128,6 @@ class IsReplyFilter(SimpleCustomFilter):
|
||||
return message.reply_to_message is not None
|
||||
|
||||
|
||||
|
||||
class LanguageFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Check users language_code.
|
||||
@ -127,8 +140,11 @@ class LanguageFilter(AdvancedCustomFilter):
|
||||
key = 'language_code'
|
||||
|
||||
async 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
|
||||
if type(text) is list:
|
||||
return message.from_user.language_code in text
|
||||
else:
|
||||
return message.from_user.language_code == text
|
||||
|
||||
|
||||
class IsAdminFilter(SimpleCustomFilter):
|
||||
"""
|
||||
@ -147,6 +163,7 @@ class IsAdminFilter(SimpleCustomFilter):
|
||||
result = await self._bot.get_chat_member(message.chat.id, message.from_user.id)
|
||||
return result.status in ['creator', 'administrator']
|
||||
|
||||
|
||||
class StateFilter(AdvancedCustomFilter):
|
||||
"""
|
||||
Filter to check state.
|
||||
@ -154,8 +171,10 @@ class StateFilter(AdvancedCustomFilter):
|
||||
Example:
|
||||
@bot.message_handler(state=1)
|
||||
"""
|
||||
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
key = 'state'
|
||||
|
||||
async def check(self, message, text):
|
||||
@ -166,22 +185,23 @@ class StateFilter(AdvancedCustomFilter):
|
||||
text = new_text
|
||||
elif isinstance(text, object):
|
||||
text = text.name
|
||||
|
||||
|
||||
if message.chat.type == 'group':
|
||||
group_state = await self.bot.current_states.get_state(message.chat.id, message.from_user.id)
|
||||
if group_state == text:
|
||||
return True
|
||||
elif group_state in text and type(text) is list:
|
||||
return True
|
||||
|
||||
|
||||
|
||||
|
||||
else:
|
||||
user_state = await self.bot.current_states.get_state(message.chat.id,message.from_user.id)
|
||||
user_state = await self.bot.current_states.get_state(message.chat.id, message.from_user.id)
|
||||
if user_state == text:
|
||||
return True
|
||||
elif type(text) is list and user_state in text:
|
||||
return True
|
||||
|
||||
|
||||
class IsDigitFilter(SimpleCustomFilter):
|
||||
"""
|
||||
Filter to check whether the string is made up of only digits.
|
||||
|
@ -128,7 +128,9 @@ class TextMatchFilter(AdvancedCustomFilter):
|
||||
key = 'text'
|
||||
|
||||
def check(self, message, text):
|
||||
if type(text) is list:
|
||||
if isinstance(text, TextFilter):
|
||||
return text.check(message)
|
||||
elif type(text) is list:
|
||||
return message.text in text
|
||||
else:
|
||||
return text == message.text
|
||||
|
Loading…
Reference in New Issue
Block a user