mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Merge pull request #1317 from coder2020official/master
Update handler_backends.py
This commit is contained in:
commit
946afcc3c1
@ -27,7 +27,7 @@ logger.addHandler(console_output_handler)
|
|||||||
logger.setLevel(logging.ERROR)
|
logger.setLevel(logging.ERROR)
|
||||||
|
|
||||||
from telebot import apihelper, util, types
|
from telebot import apihelper, util, types
|
||||||
from telebot.handler_backends import MemoryHandlerBackend, FileHandlerBackend, StateMachine, State
|
from telebot.handler_backends import MemoryHandlerBackend, FileHandlerBackend, State
|
||||||
|
|
||||||
|
|
||||||
REPLY_MARKUP_TYPES = Union[
|
REPLY_MARKUP_TYPES = Union[
|
||||||
@ -188,7 +188,7 @@ class TeleBot:
|
|||||||
self.custom_filters = {}
|
self.custom_filters = {}
|
||||||
self.state_handlers = []
|
self.state_handlers = []
|
||||||
|
|
||||||
self.current_states = StateMachine()
|
self.current_states = State()
|
||||||
|
|
||||||
if apihelper.ENABLE_MIDDLEWARE:
|
if apihelper.ENABLE_MIDDLEWARE:
|
||||||
self.typed_middleware_handlers = {
|
self.typed_middleware_handlers = {
|
||||||
@ -2475,7 +2475,7 @@ class TeleBot:
|
|||||||
if not self._test_filter(message_filter, filter_value, message):
|
if not self._test_filter(message_filter, filter_value, message):
|
||||||
return False
|
return False
|
||||||
need_pop = True
|
need_pop = True
|
||||||
state = State(self.current_states)
|
state = self.current_states
|
||||||
self._exec_task(handler["function"], message, state)
|
self._exec_task(handler["function"], message, state)
|
||||||
if need_pop:
|
if need_pop:
|
||||||
new_messages.pop(i) # removing message that was detected by states
|
new_messages.pop(i) # removing message that was detected by states
|
||||||
|
@ -143,7 +143,7 @@ class RedisHandlerBackend(HandlerBackend):
|
|||||||
return handlers
|
return handlers
|
||||||
|
|
||||||
|
|
||||||
class StateMachine:
|
class State:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._states = {}
|
self._states = {}
|
||||||
|
|
||||||
@ -155,7 +155,7 @@ class StateMachine:
|
|||||||
"""
|
"""
|
||||||
if chat_id in self._states:
|
if chat_id in self._states:
|
||||||
|
|
||||||
self._states[int(chat_id)]['state'] = state
|
self._states[chat_id]['state'] = state
|
||||||
else:
|
else:
|
||||||
self._states[chat_id] = {'state': state,'data': {}}
|
self._states[chat_id] = {'state': state,'data': {}}
|
||||||
|
|
||||||
@ -171,27 +171,20 @@ class StateMachine:
|
|||||||
def _get_data(self, chat_id):
|
def _get_data(self, chat_id):
|
||||||
return self._states[chat_id]['data']
|
return self._states[chat_id]['data']
|
||||||
|
|
||||||
class State:
|
|
||||||
"""
|
|
||||||
Base class for state managing.
|
|
||||||
"""
|
|
||||||
def __init__(self, obj: StateMachine) -> None:
|
|
||||||
self.obj = obj
|
|
||||||
|
|
||||||
def set(self, chat_id, new_state):
|
def set(self, chat_id, new_state):
|
||||||
"""
|
"""
|
||||||
Set a new state for a user.
|
Set a new state for a user.
|
||||||
:param chat_id:
|
:param chat_id:
|
||||||
:param new_state: new_state of a user
|
:param new_state: new_state of a user
|
||||||
"""
|
"""
|
||||||
self.obj._states[chat_id]['state'] = new_state
|
self.add_state(chat_id,new_state)
|
||||||
|
|
||||||
def finish(self, chat_id):
|
def finish(self, chat_id):
|
||||||
"""
|
"""
|
||||||
Finish(delete) state of a user.
|
Finish(delete) state of a user.
|
||||||
:param chat_id:
|
:param chat_id:
|
||||||
"""
|
"""
|
||||||
self.obj._states.pop(chat_id)
|
self.delete_state(chat_id)
|
||||||
|
|
||||||
def retrieve_data(self, chat_id):
|
def retrieve_data(self, chat_id):
|
||||||
"""
|
"""
|
||||||
@ -204,13 +197,13 @@ class State:
|
|||||||
Also, at the end of your 'Form' you can get the name:
|
Also, at the end of your 'Form' you can get the name:
|
||||||
data['name']
|
data['name']
|
||||||
"""
|
"""
|
||||||
return StateContext(self.obj, chat_id)
|
return StateContext(self, chat_id)
|
||||||
|
|
||||||
class StateContext:
|
class StateContext:
|
||||||
"""
|
"""
|
||||||
Class for data.
|
Class for data.
|
||||||
"""
|
"""
|
||||||
def __init__(self , obj: StateMachine, chat_id) -> None:
|
def __init__(self , obj: State, chat_id) -> None:
|
||||||
self.obj = obj
|
self.obj = obj
|
||||||
self.chat_id = chat_id
|
self.chat_id = chat_id
|
||||||
self.data = obj._get_data(chat_id)
|
self.data = obj._get_data(chat_id)
|
||||||
|
Loading…
Reference in New Issue
Block a user