AsyncTeleBot¶
AsyncTeleBot methods¶
Asyncio filters¶
- class telebot.asyncio_filters.AdvancedCustomFilter¶
Bases:
ABC
Advanced 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
Child classes should have .key property.
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)
- async check(message, text)¶
Perform a check.
- key: str = None¶
- class telebot.asyncio_filters.ChatFilter¶
Bases:
AdvancedCustomFilter
Check whether chat_id corresponds to given chat_id.
@bot.message_handler(chat_id=[99999]) # your function
- key: str = 'chat_id'¶
- class telebot.asyncio_filters.ForwardFilter¶
Bases:
SimpleCustomFilter
Check whether message was forwarded from channel or group.
@bot.message_handler(is_forwarded=True) # your function
- key: str = 'is_forwarded'¶
- class telebot.asyncio_filters.IsAdminFilter(bot)¶
Bases:
SimpleCustomFilter
Check whether the user is administrator / owner of the chat.
@bot.message_handler(chat_types=['supergroup'], is_chat_admin=True) # your function
- key: str = 'is_chat_admin'¶
- class telebot.asyncio_filters.IsDigitFilter¶
Bases:
SimpleCustomFilter
Filter to check whether the string is made up of only digits.
@bot.message_handler(is_digit=True) # your function
- key: str = 'is_digit'¶
- class telebot.asyncio_filters.IsReplyFilter¶
Bases:
SimpleCustomFilter
Check whether message is a reply.
@bot.message_handler(is_reply=True) # your function
- key: str = 'is_reply'¶
- class telebot.asyncio_filters.LanguageFilter¶
Bases:
AdvancedCustomFilter
Check users language_code.
@bot.message_handler(language_code=['ru']) # your function
- key: str = 'language_code'¶
- class telebot.asyncio_filters.SimpleCustomFilter¶
Bases:
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.
Child classes should have .key property.
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
- async check(message) bool ¶
Perform a check.
- key: str = None¶
- class telebot.asyncio_filters.StateFilter(bot)¶
Bases:
AdvancedCustomFilter
Filter to check state.
@bot.message_handler(state=1) # your function
- key: str = 'state'¶
- class telebot.asyncio_filters.TextContainsFilter¶
Bases:
AdvancedCustomFilter
Filter to check Text message. key: text
# Will respond if any message.text contains word 'account' @bot.message_handler(text_contains=['account']) # your function
- key: str = 'text_contains'¶
- class telebot.asyncio_filters.TextFilter(equals: str | None = None, contains: list | tuple | None = None, starts_with: str | list | tuple | None = None, ends_with: str | list | tuple | None = None, ignore_case: bool = False)¶
Bases:
object
Advanced text filter to check (types.Message, types.CallbackQuery, types.InlineQuery, types.Poll)
example of usage is in examples/asynchronous_telebot/custom_filters/advanced_text_filter.py
- Parameters:
equals (
str
) – string, True if object’s text is equal to passed stringcontains (list[str] or tuple[str]) – list[str] or tuple[str], True if any string element of iterable is in text
starts_with (
str
) – string, True if object’s text starts with passed stringends_with (
str
) – string, True if object’s text starts with passed stringignore_case (
bool
) – bool (default False), case insensitive
- Raises:
ValueError – if incorrect value for a parameter was supplied
- Returns:
None
- class telebot.asyncio_filters.TextMatchFilter¶
Bases:
AdvancedCustomFilter
Filter to check Text message.
@bot.message_handler(text=['account']) # your function
- key: str = 'text'¶
- class telebot.asyncio_filters.TextStartsFilter¶
Bases:
AdvancedCustomFilter
Filter to check whether message starts with some text.
# Will work if message.text starts with 'sir'. @bot.message_handler(text_startswith='sir') # your function
- key: str = 'text_startswith'¶
Asyncio handler backends¶
File with all middleware classes, states.
- class telebot.asyncio_handler_backends.BaseMiddleware¶
Bases:
object
Base class for middleware. Your middlewares should be inherited from this class.
Set update_sensitive=True if you want to get different updates on different functions. For example, if you want to handle pre_process for message update, then you will have to create pre_process_message function, and so on. Same applies to post_process.
class MyMiddleware(BaseMiddleware): def __init__(self): self.update_sensitive = True self.update_types = ['message', 'edited_message'] async def pre_process_message(self, message, data): # only message update here pass async def post_process_message(self, message, data, exception): pass # only message update here for post_process async def pre_process_edited_message(self, message, data): # only edited_message update here pass async def post_process_edited_message(self, message, data, exception): pass # only edited_message update here for post_process
- async post_process(message, data, exception)¶
- async pre_process(message, data)¶
- update_sensitive: bool = False¶
- class telebot.asyncio_handler_backends.CancelUpdate¶
Bases:
object
Class for canceling updates. Just return instance of this class in middleware to skip update. Update will skip handler and execution of post_process in middlewares.
- class telebot.asyncio_handler_backends.ContinueHandling¶
Bases:
object
Class for continue updates in handlers. Just return instance of this class in handlers to continue process.
@bot.message_handler(commands=['start']) async def start(message): await bot.send_message(message.chat.id, 'Hello World!') return ContinueHandling() @bot.message_handler(commands=['start']) async def start2(message): await bot.send_message(message.chat.id, 'Hello World2!')
- class telebot.asyncio_handler_backends.SkipHandler¶
Bases:
object
Class for skipping handlers. Just return instance of this class in middleware to skip handler. Update will go to post_process, but will skip execution of handler.
- class telebot.asyncio_handler_backends.State¶
Bases:
object
Class representing a state.
class MyStates(StatesGroup): my_state = State() # returns my_state:State string.