Message Interfaces#
The Message Interfaces module contains several basic classes that define the message interfaces. These classes provide a way to define the structure of the messengers that are used to communicate with Chatsky.
- class MessengerInterface[source]#
Bases:
ABCClass that represents a message interface used for communication between pipeline and users. It is responsible for connection between user and pipeline, as well as for request-response transactions.
- abstract async connect(pipeline_runner)[source]#
Method invoked when message interface is instantiated and connection is established. May be used for sending an introduction message or displaying general bot information.
- Parameters:
pipeline_runner (
PipelineRunnerFunction) – A function that should process user request and return context; usually it’s a_run_pipeline()function.
- class MessengerInterfaceWithAttachments(attachments_directory=None)[source]#
Bases:
MessengerInterface,ABCMessengerInterface subclass that has methods for attachment handling.
- Parameters:
attachments_directory (
Optional[Path]) – Directory where attachments will be stored. If not specified, the temporary directory will be used.
-
supported_request_attachment_types:
set[Type[Attachment]] = {}# Types of attachment that this messenger interface can receive. Attachments not in this list will be neglected.
-
supported_response_attachment_types:
set[Type[Attachment]] = {}# Types of attachment that this messenger interface can send. Attachments not in this list will be neglected.
- abstract async get_attachment_bytes(source)[source]#
Get attachment bytes from file source.
E.g. if a file attachment consists of a URL of the file uploaded to the messenger servers, this method is the right place to call the messenger API for the file downloading.
- Parameters:
source (
str) – Identifying string for the file.- Return type:
bytes- Returns:
The attachment bytes.
- class PollingMessengerInterface[source]#
Bases:
MessengerInterfacePolling message interface runs in a loop, constantly asking users for a new input.
- abstract _request()[source]#
Method used for sending users request for their input.
- Return type:
List[Tuple[Message,Hashable]]- Returns:
A list of tuples: user inputs and context ids (any user ids) associated with the inputs.
- abstract _respond(responses)[source]#
Method used for sending users responses for their last input.
- Parameters:
responses (
List[Context]) – A list of contexts, representing dialogs with the users; last_response, id and some dialog info can be extracted from there.
- _on_exception(e)[source]#
Method that is called on polling cycle exceptions, in some cases it should show users the exception. By default, it logs all exit exceptions to info log and all non-exit exceptions to error.
- Parameters:
e (
BaseException) – The exception.
- async _polling_loop(pipeline_runner, timeout=0)[source]#
Method running the request - response cycle once.
- async connect(pipeline_runner, loop=<function PollingMessengerInterface.<lambda>>, timeout=0)[source]#
Method, running a request - response cycle in a loop. The looping behavior is determined by loop and timeout, for most cases the loop itself shouldn’t be overridden.
- Parameters:
pipeline_runner (
PipelineRunnerFunction) – A function that should process user request and return context; usually it’s a_run_pipeline()function.loop (
Callable[[],bool]) – a function that determines whether polling should be continued; called in each cycle, should return True to continue polling or False to stop.timeout (
float) – a time interval between polls (in seconds).
- class CallbackMessengerInterface[source]#
Bases:
MessengerInterfaceCallback message interface is waiting for user input and answers once it gets one.
- async connect(pipeline_runner)[source]#
Method invoked when message interface is instantiated and connection is established. May be used for sending an introduction message or displaying general bot information.
- Parameters:
pipeline_runner (
PipelineRunnerFunction) – A function that should process user request and return context; usually it’s a_run_pipeline()function.
- async on_request_async(request, ctx_id=None, update_ctx_misc=None)[source]#
Method that should be invoked on user input. This method has the same signature as
PipelineRunnerFunction.- Return type:
- on_request(request, ctx_id=None, update_ctx_misc=None)[source]#
Method that should be invoked on user input. This method has the same signature as
PipelineRunnerFunction.- Return type: