Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To hold a conversation with users, the bot has to implement a state machine to keep track of the different paths in the conversation. This is a pretty common pattern and we don't have a good way to deal with it right now. The state_machine_bot.py example is far from perfect.
This PR introduces a new
Handler
subclass calledConversationHandler
to take care of this. Instead of re-inventing the wheel, this handler acts only as "management" for the existing handler classes.Copied from documentation:
A handler to hold a conversation with a user by managing four collections of other handlers.
The first collection, a
list
namedentry_points
, is used to initiate the conversation,for example with a
CommandHandler
orRegexHandler
.The second collection, a
dict
namedstates
, contains the different conversation stepsand 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 mostly
MessageHandler
andRegexHandler
here.The third collection, a
list
namedfallbacks
, is used if the user is currently in aconversation 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
/cancel
command or to let theuser know their message was not recognized.
The fourth, optional collection of handlers, a
list
namedtimed_out_behavior
is used ifthe wait for
run_async
takes 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
None
bydefault), the state will not change. To end the conversation, the callback function must
return
CallbackHandler.END
or-1
.Usage (copied from example):
TODO
@run_async
integration