-
Notifications
You must be signed in to change notification settings - Fork 2
AutoConv Handler
Moris Doratiotto edited this page Aug 7, 2021
·
5 revisions
This is a simple interface between our FSM (Conversation) and the python-telegram-bot API.
All the magic behind this package is here. In fact, there isn't much to say.
The simple manage_conversation
will handle the Conversation, you can choose to delete the user trigger message.
In addition, there is a restart
function (go back to a fresh start) and a force_state
function to jump around between State.
AutoConvHandler(
conversation: Conversation,
telegram_state_name: Any
)
-
conversation
: conversation to handle. -
telegram_state_name
: Telegram state name to handle callback, text and other things.
Usage example: autoconv_handler.py
# manage conversation with update and context
AutoConvHandler.manage_conversation(
update: Telegram.Update,
context: Telegram.Context,
delete_first: bool = True
)
-
update
,context
: from telegram bot function. -
delete_first
: if you want to delete the user message that trigger this handler.
Usage example: autoconv_handler.py
# restart the conversation to initial configuration
AutoConvHandler.restart(
update: Telegram.Update,
context: Telegram.Context
)
-
update
,context
: from telegram bot function.
Usage example: autoconv_handler.py
# force a state in the conversation
AutoConvHandler.force_state(
state: Union[State, str],
update: Telegram.Update
)
-
state
: force the conversation to a specific state. -
update
: from telegram bot function.
You can use
force_state
in action and operation buttons too, check out this example actions.py.
Usage example: async.py
# set an async function
AutoConvHandler.set_timed_function(
seconds: int,
state: Union[State, str] = None,
function: Callable = None
)
-
seconds
: seconds to wait before the function is called. -
state
: forced state when the function ends, if specified. -
function
: function must take one parameter (TelegramData object). It will called after waitingseconds
, if specified.
Usage example: async.py
# stop an async function
AutoConvHandler.stop_timed_function(
state: Union[State, str] = None,
function: Callable = None
)
-
state
: name or State specified inset_timed_function
. -
function
: function specified inset_timed_function
.
If both are specified, name has the priority.
Usage example: async.py
# send a message that will auto destroy itself after some seconds
AutoConvHandler.send_autodestroy_message(
msg: str,
seconds: int,
**kwargs
)
-
msg
: text to send in the message. -
seconds
: afterseconds
the message will auto destroy itself. -
kwargs
: parameters you need from Telegram APIsendMessage
.