pyTelegramBotAPI/telebot/asyncio_filters.py

455 lines
13 KiB
Python
Raw Normal View History

2021-11-20 13:47:55 +03:00
from abc import ABC
2022-02-12 15:33:29 +03:00
from typing import Optional, Union
2022-02-26 20:43:03 +03:00
from telebot.asyncio_handler_backends import State
2021-11-20 13:47:55 +03:00
2022-02-12 15:33:29 +03:00
from telebot import types
2021-11-20 13:47:55 +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.
2022-05-01 14:11:00 +03:00
Child classes should have .key property.
.. code-block:: python3
:caption: Example on creating a simple custom filter.
class ForwardFilter(SimpleCustomFilter):
# Check whether message was forwarded from channel or group.
key = 'is_forwarded'
def check(self, message):
return message.forward_date is not None
2021-11-20 13:47:55 +03:00
"""
2022-05-01 14:11:00 +03:00
key: str = None
2021-11-20 13:47:55 +03:00
async def check(self, message):
"""
Perform a check.
"""
pass
class AdvancedCustomFilter(ABC):
"""
Advanced Custom Filter base class.
2021-11-20 13:47:55 +03:00
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
2022-05-01 14:11:00 +03:00
Child classes should have .key property.
.. code-block:: python3
:caption: Example on creating an advanced custom filter.
class TextStartsFilter(AdvancedCustomFilter):
# Filter to check whether message starts with some text.
key = 'text_startswith'
def check(self, message, text):
return message.text.startswith(text)
2021-11-20 13:47:55 +03:00
"""
2022-05-01 14:11:00 +03:00
key: str = None
2021-11-20 13:47:55 +03:00
async def check(self, message, text):
"""
Perform a check.
"""
pass
2022-02-12 15:33:29 +03:00
class TextFilter:
"""
Advanced text filter to check (types.Message, types.CallbackQuery, types.InlineQuery, types.Poll)
2022-02-12 15:33:29 +03:00
example of usage is in examples/asynchronous_telebot/custom_filters/advanced_text_filter.py
:param equals: string, True if object's text is equal to passed string
:type equals: :obj:`str`
:param contains: list[str] or tuple[str], True if any string element of iterable is in text
:type contains: list[str] or tuple[str]
:param starts_with: string, True if object's text starts with passed string
:type starts_with: :obj:`str`
:param ends_with: string, True if object's text starts with passed string
:type ends_with: :obj:`str`
:param ignore_case: bool (default False), case insensitive
:type ignore_case: :obj:`bool`
:raises ValueError: if incorrect value for a parameter was supplied
:return: None
2022-02-12 15:33:29 +03:00
"""
def __init__(self,
equals: Optional[str] = None,
contains: Optional[Union[list, tuple]] = None,
starts_with: Optional[Union[str, list, tuple]] = None,
ends_with: Optional[Union[str, list, tuple]] = None,
2022-02-12 15:33:29 +03:00
ignore_case: bool = False):
"""
:param equals: string, True if object's text is equal to passed string
:type equals: :obj:`str`
2022-02-12 18:36:10 +03:00
:param contains: list[str] or tuple[str], True if any string element of iterable is in text
:type contains: list[str] or tuple[str]
2022-02-12 15:33:29 +03:00
:param starts_with: string, True if object's text starts with passed string
:type starts_with: :obj:`str`
2022-02-12 15:33:29 +03:00
:param ends_with: string, True if object's text starts with passed string
:type ends_with: :obj:`str`
2022-02-12 15:33:29 +03:00
:param ignore_case: bool (default False), case insensitive
:type ignore_case: :obj:`bool`
:raises ValueError: if incorrect value for a parameter was supplied
:return: None
2022-02-12 15:33:29 +03:00
"""
to_check = sum((pattern is not None for pattern in (equals, contains, starts_with, ends_with)))
if to_check == 0:
raise ValueError('None of the check modes was specified')
self.equals = equals
2022-02-16 10:29:27 +03:00
self.contains = self._check_iterable(contains, filter_name='contains')
self.starts_with = self._check_iterable(starts_with, filter_name='starts_with')
self.ends_with = self._check_iterable(ends_with, filter_name='ends_with')
2022-02-12 15:33:29 +03:00
self.ignore_case = ignore_case
2022-02-16 10:29:27 +03:00
def _check_iterable(self, iterable, filter_name):
if not iterable:
pass
elif not isinstance(iterable, str) and not isinstance(iterable, list) and not isinstance(iterable, tuple):
2022-02-16 10:29:27 +03:00
raise ValueError(f"Incorrect value of {filter_name!r}")
elif isinstance(iterable, str):
iterable = [iterable]
elif isinstance(iterable, list) or isinstance(iterable, tuple):
iterable = [i for i in iterable if isinstance(i, str)]
return iterable
2022-02-12 15:33:29 +03:00
async def check(self, obj: Union[types.Message, types.CallbackQuery, types.InlineQuery, types.Poll]):
"""
:meta private:
"""
2022-02-12 15:33:29 +03:00
if isinstance(obj, types.Poll):
text = obj.question
elif isinstance(obj, types.Message):
text = obj.text or obj.caption
elif isinstance(obj, types.CallbackQuery):
text = obj.data
elif isinstance(obj, types.InlineQuery):
text = obj.query
else:
return False
if self.ignore_case:
text = text.lower()
2022-02-19 13:53:58 +03:00
prepare_func = lambda string: str(string).lower()
else:
prepare_func = str
2022-02-12 15:33:29 +03:00
if self.equals:
2022-02-19 13:53:58 +03:00
result = prepare_func(self.equals) == text
if result:
return True
elif not result and not any((self.contains, self.starts_with, self.ends_with)):
return False
2022-02-12 15:33:29 +03:00
if self.contains:
2022-02-19 13:53:58 +03:00
result = any([prepare_func(i) in text for i in self.contains])
if result:
return True
elif not result and not any((self.starts_with, self.ends_with)):
return False
2022-02-12 15:33:29 +03:00
if self.starts_with:
2022-02-19 13:53:58 +03:00
result = any([text.startswith(prepare_func(i)) for i in self.starts_with])
if result:
return True
elif not result and not self.ends_with:
return False
2022-02-12 15:33:29 +03:00
if self.ends_with:
2022-02-19 13:53:58 +03:00
return any([text.endswith(prepare_func(i)) for i in self.ends_with])
2022-02-12 15:33:29 +03:00
return False
2021-11-20 13:47:55 +03:00
class TextMatchFilter(AdvancedCustomFilter):
"""
Filter to check Text message.
.. code-block:: python3
:caption: Example on using this filter:
@bot.message_handler(text=['account'])
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'text'
async def check(self, message, text):
"""
:meta private:
"""
if isinstance(text, TextFilter):
2022-02-12 15:33:29 +03:00
return await text.check(message)
elif type(text) is list:
return message.text in text
else:
return text == message.text
2021-11-20 13:47:55 +03:00
class TextContainsFilter(AdvancedCustomFilter):
"""
Filter to check Text message.
key: text
.. code-block:: python3
:caption: Example on using this filter:
# Will respond if any message.text contains word 'account'
@bot.message_handler(text_contains=['account'])
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'text_contains'
async def check(self, message, text):
"""
:meta private:
"""
2022-02-12 18:36:10 +03:00
if not isinstance(text, str) and not isinstance(text, list) and not isinstance(text, tuple):
raise ValueError("Incorrect text_contains value")
elif isinstance(text, str):
text = [text]
elif isinstance(text, list) or isinstance(text, tuple):
text = [i for i in text if isinstance(i, str)]
return any([i in message.text for i in text])
2021-11-20 13:47:55 +03:00
2021-11-20 13:47:55 +03:00
class TextStartsFilter(AdvancedCustomFilter):
"""
Filter to check whether message starts with some text.
.. code-block:: python3
:caption: Example on using this filter:
# Will work if message.text starts with 'sir'.
@bot.message_handler(text_startswith='sir')
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'text_startswith'
2021-11-20 13:47:55 +03:00
async def check(self, message, text):
"""
:meta private:
"""
return message.text.startswith(text)
2021-11-20 13:47:55 +03:00
class ChatFilter(AdvancedCustomFilter):
"""
Check whether chat_id corresponds to given chat_id.
.. code-block:: python3
:caption: Example on using this filter:
@bot.message_handler(chat_id=[99999])
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'chat_id'
2021-11-20 13:47:55 +03:00
async def check(self, message, text):
"""
:meta private:
"""
if isinstance(message, types.CallbackQuery):
return message.message.chat.id in text
2021-11-20 13:47:55 +03:00
return message.chat.id in text
2021-11-20 13:47:55 +03:00
class ForwardFilter(SimpleCustomFilter):
"""
Check whether message was forwarded from channel or group.
.. code-block:: python3
:caption: Example on using this filter:
2021-11-20 13:47:55 +03:00
@bot.message_handler(is_forwarded=True)
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'is_forwarded'
async def check(self, message):
"""
:meta private:
"""
return message.forward_date is not None
2021-11-20 13:47:55 +03:00
2021-11-20 13:47:55 +03:00
class IsReplyFilter(SimpleCustomFilter):
"""
Check whether message is a reply.
.. code-block:: python3
:caption: Example on using this filter:
2021-11-20 13:47:55 +03:00
@bot.message_handler(is_reply=True)
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'is_reply'
async def check(self, message):
"""
:meta private:
"""
if isinstance(message, types.CallbackQuery):
return message.message.reply_to_message is not None
2021-11-20 13:47:55 +03:00
return message.reply_to_message is not None
class LanguageFilter(AdvancedCustomFilter):
"""
Check users language_code.
.. code-block:: python3
:caption: Example on using this filter:
2021-11-20 13:47:55 +03:00
@bot.message_handler(language_code=['ru'])
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'language_code'
async def check(self, message, text):
"""
:meta private:
"""
if type(text) is list:
return message.from_user.language_code in text
else:
return message.from_user.language_code == text
2021-11-20 13:47:55 +03:00
class IsAdminFilter(SimpleCustomFilter):
"""
Check whether the user is administrator / owner of the chat.
.. code-block:: python3
:caption: Example on using this filter:
@bot.message_handler(chat_types=['supergroup'], is_chat_admin=True)
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'is_chat_admin'
def __init__(self, bot):
self._bot = bot
async def check(self, message):
"""
:meta private:
"""
if isinstance(message, types.CallbackQuery):
result = await self._bot.get_chat_member(message.message.chat.id, message.from_user.id)
return result.status ('creator', 'administrator')
2021-11-27 21:41:39 +03:00
result = await self._bot.get_chat_member(message.chat.id, message.from_user.id)
return result.status in ['creator', 'administrator']
2021-11-20 13:47:55 +03:00
2021-11-20 13:47:55 +03:00
class StateFilter(AdvancedCustomFilter):
"""
Filter to check state.
.. code-block:: python3
:caption: Example on using this filter:
@bot.message_handler(state=1)
# your function
2021-11-20 13:47:55 +03:00
"""
2021-11-20 13:47:55 +03:00
def __init__(self, bot):
self.bot = bot
2021-11-20 13:47:55 +03:00
key = 'state'
async def check(self, message, text):
"""
:meta private:
"""
if text == '*': return True
2022-02-02 13:44:02 +03:00
2022-02-26 20:43:03 +03:00
# needs to work with callbackquery
if isinstance(message, types.Message):
chat_id = message.chat.id
user_id = message.from_user.id
if isinstance(message, types.CallbackQuery):
chat_id = message.message.chat.id
user_id = message.from_user.id
message = message.message
2022-02-02 13:44:02 +03:00
if isinstance(text, list):
2022-02-23 11:07:25 +03:00
new_text = []
for i in text:
2022-02-26 20:43:03 +03:00
if isinstance(i, State): i = i.name
2022-02-23 11:07:25 +03:00
new_text.append(i)
2022-02-02 13:44:02 +03:00
text = new_text
2022-02-26 20:43:03 +03:00
elif isinstance(text, State):
2022-02-02 13:44:02 +03:00
text = text.name
2022-06-30 15:51:58 +03:00
if message.chat.type in ['group', 'supergroup']:
2022-09-06 17:12:11 +03:00
group_state = await self.bot.current_states.get_state(chat_id, user_id)
if group_state == text:
return True
2022-04-15 22:13:14 +03:00
elif type(text) is list and group_state in text:
return True
else:
2022-09-06 17:12:11 +03:00
user_state = await self.bot.current_states.get_state(chat_id, user_id)
if user_state == text:
return True
elif type(text) is list and user_state in text:
return True
2021-11-20 13:47:55 +03:00
2021-11-20 13:47:55 +03:00
class IsDigitFilter(SimpleCustomFilter):
"""
Filter to check whether the string is made up of only digits.
.. code-block:: python3
:caption: Example on using this filter:
@bot.message_handler(is_digit=True)
# your function
2021-11-20 13:47:55 +03:00
"""
key = 'is_digit'
async def check(self, message):
"""
:meta private:
"""
2021-11-20 13:47:55 +03:00
return message.text.isdigit()