Skip to content

AutoConv Handler

Moris Doratiotto edited this page Aug 7, 2021 · 5 revisions

Overview

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.

Basic

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.

Doc

AutoConvHandler(
    conversation: Conversation,
    telegram_state_name: Any
)
  • conversation: conversation to handle.
  • telegram_state_name: Telegram state name to handle callback, text and other things.

Manage Conversation

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.

Restart

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.

Force State

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.

Set Timed Function

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 waiting seconds, if specified.

Stop Timed Function

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 in set_timed_function.
  • function: function specified in set_timed_function.

If both are specified, name has the priority.

Send Autodestroy Message

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: after seconds the message will auto destroy itself.
  • kwargs: parameters you need from Telegram API sendMessage.
Clone this wiki locally