telegram.ext.conversationhandler module¶
This module contains the ConversationHandler
-
class
telegram.ext.conversationhandler.ConversationHandler(entry_points, states, fallbacks, allow_reentry=False, run_async_timeout=None, timed_out_behavior=None)¶ Bases:
telegram.ext.handler.HandlerA handler to hold a conversation with a user by managing four collections of other handlers.
The first collection, a
listnamedentry_points, is used to initiate the conversation, for example with aCommandHandlerorRegexHandler.The second collection, a
dictnamedstates, contains the different conversation steps and one or more associated handlers that should be used if the user sends a message when the conversation with them is currently in that state. You will probably use mostlyMessageHandlerandRegexHandlerhere.The third collection, a
listnamedfallbacks, is used if the user is currently in a conversation but the state has either no associated handler or the handler that is associated to the state is inappropriate for the update, for example if the update contains a command, but a regular text message is expected. You could use this for a/cancelcommand or to let the user know their message was not recognized.The fourth, optional collection of handlers, a
listnamedtimed_out_behavioris used if the wait forrun_asynctakes longer than defined inrun_async_timeout. For example, you can let the user know that they should wait for a bit before they can continue.To change the state of conversation, the callback function of a handler must return the new state after responding to the user. If it does not return anything (returning
Noneby default), the state will not change. To end the conversation, the callback function must returnCallbackHandler.ENDor-1.Parameters: - entry_points (list) – A list of
Handlerobjects that can trigger the start of the conversation. The first handler whichcheck_updatemethod returnsTruewill be used. If all returnFalse, the update is not handled. - states (dict) – A
dict[object: list[Handler]]that defines the different states of conversation a user can be in and one or more associatedHandlerobjects that should be used in that state. The first handler whichcheck_updatemethod returnsTruewill be used. - fallbacks (list) – A list of handlers that might be used if the user is in a conversation,
but every handler for their current state returned
Falseoncheck_update. The first handler whichcheck_updatemethod returnsTruewill be used. If all returnFalse, the update is not handled. - allow_reentry (Optional[bool]) – If set to
True, a user that is currently in a conversation can restart the conversation by triggering one of the entry points. - run_async_timeout (Optional[float]) – If the previous handler for this user was running
asynchronously using the
run_asyncdecorator, it might not be finished when the next message arrives. This timeout defines how long the conversation handler should wait for the next state to be computed. The default isNonewhich means it will wait indefinitely. - timed_out_behavior (Optional[list]) – A list of handlers that might be used if
the wait for
run_asynctimed out. The first handler whichcheck_updatemethod returnsTruewill be used. If all returnFalse, the update is not handled.
-
END= -1¶
-
check_update(update)¶
-
conversations= None¶ type – dict[(int, int): str]
-
entry_points= None¶ type – list[telegram.ext.Handler]
-
fallbacks= None¶ type – list[telegram.ext.Handler]
-
handle_update(update, dispatcher)¶
-
states= None¶ type – dict[str: telegram.ext.Handler]
-
timed_out_behavior= None¶ type – list[telegram.ext.Handler]
-
update_state(new_state, key)¶
- entry_points (list) – A list of