mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
States, and some minor improvements
This commit is contained in:
@@ -27,7 +27,7 @@ logger.addHandler(console_output_handler)
|
||||
logger.setLevel(logging.ERROR)
|
||||
|
||||
from telebot import apihelper, util, types
|
||||
from telebot.handler_backends import MemoryHandlerBackend, FileHandlerBackend
|
||||
from telebot.handler_backends import MemoryHandlerBackend, FileHandlerBackend, StateMachine, State
|
||||
|
||||
|
||||
REPLY_MARKUP_TYPES = Union[
|
||||
@@ -186,6 +186,9 @@ class TeleBot:
|
||||
self.my_chat_member_handlers = []
|
||||
self.chat_member_handlers = []
|
||||
self.custom_filters = {}
|
||||
self.state_handlers = []
|
||||
|
||||
self.current_states = StateMachine()
|
||||
|
||||
if apihelper.ENABLE_MIDDLEWARE:
|
||||
self.typed_middleware_handlers = {
|
||||
@@ -495,6 +498,7 @@ class TeleBot:
|
||||
|
||||
def process_new_messages(self, new_messages):
|
||||
self._notify_next_handlers(new_messages)
|
||||
self._notify_state_handlers(new_messages)
|
||||
self._notify_reply_handlers(new_messages)
|
||||
self.__notify_update(new_messages)
|
||||
self._notify_command_handlers(self.message_handlers, new_messages)
|
||||
@@ -2362,6 +2366,31 @@ class TeleBot:
|
||||
chat_id = message.chat.id
|
||||
self.register_next_step_handler_by_chat_id(chat_id, callback, *args, **kwargs)
|
||||
|
||||
def set_state(self, chat_id, state):
|
||||
"""
|
||||
Sets a new state of a user.
|
||||
:param chat_id:
|
||||
:param state: new state. can be string or integer.
|
||||
"""
|
||||
self.current_states.add_state(chat_id, state)
|
||||
|
||||
def delete_state(self, chat_id):
|
||||
"""
|
||||
Delete the current state of a user.
|
||||
:param chat_id:
|
||||
:return:
|
||||
"""
|
||||
self.current_states.delete_state(chat_id)
|
||||
|
||||
def get_state(self, chat_id):
|
||||
"""
|
||||
Get current state of a user.
|
||||
:param chat_id:
|
||||
:return: state of a user
|
||||
"""
|
||||
return self.current_states.current_state(chat_id)
|
||||
|
||||
|
||||
def register_next_step_handler_by_chat_id(
|
||||
self, chat_id: Union[int, str], callback: Callable, *args, **kwargs) -> None:
|
||||
"""
|
||||
@@ -2426,6 +2455,26 @@ class TeleBot:
|
||||
if need_pop:
|
||||
new_messages.pop(i) # removing message that was detected with next_step_handler
|
||||
|
||||
|
||||
def _notify_state_handlers(self, new_messages):
|
||||
"""
|
||||
Description: TBD
|
||||
:param new_messages:
|
||||
:return:
|
||||
"""
|
||||
for i, message in enumerate(new_messages):
|
||||
need_pop = False
|
||||
user_state = self.current_states.current_state(message.from_user.id)
|
||||
if user_state:
|
||||
for handler in self.state_handlers:
|
||||
if handler['filters']['state'] == user_state:
|
||||
need_pop = True
|
||||
state = State(self.current_states)
|
||||
self._exec_task(handler["function"], message, state)
|
||||
if need_pop:
|
||||
new_messages.pop(i) # removing message that was detected by states
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _build_handler_dict(handler, **filters):
|
||||
"""
|
||||
@@ -2661,6 +2710,44 @@ class TeleBot:
|
||||
**kwargs)
|
||||
self.add_edited_message_handler(handler_dict)
|
||||
|
||||
|
||||
def state_handler(self, state=None, content_types=None, func=None,**kwargs):
|
||||
|
||||
def decorator(handler):
|
||||
handler_dict = self._build_handler_dict(handler,
|
||||
state=state,
|
||||
content_types=content_types,
|
||||
func=func,
|
||||
**kwargs)
|
||||
self.add_state_handler(handler_dict)
|
||||
return handler
|
||||
|
||||
return decorator
|
||||
|
||||
def add_state_handler(self, handler_dict):
|
||||
"""
|
||||
Adds the edit message handler
|
||||
:param handler_dict:
|
||||
:return:
|
||||
"""
|
||||
self.state_handlers.append(handler_dict)
|
||||
|
||||
def register_state_handler(self, callback, state=None, content_types=None, func=None, **kwargs):
|
||||
"""
|
||||
Register a state handler.
|
||||
:param callback: function to be called
|
||||
:param state: state to be checked
|
||||
:param content_types:
|
||||
:param func:
|
||||
"""
|
||||
handler_dict = self._build_handler_dict(callback,
|
||||
state=state,
|
||||
content_types=content_types,
|
||||
func=func,
|
||||
**kwargs)
|
||||
self.add_state_handler(handler_dict)
|
||||
|
||||
|
||||
def channel_post_handler(self, commands=None, regexp=None, func=None, content_types=None, **kwargs):
|
||||
"""
|
||||
Channel post handler decorator
|
||||
|
||||
Reference in New Issue
Block a user