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 the DFF.
- class MessengerInterface[source]#
Bases:
ABC
Class 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 PollingMessengerInterface[source]#
Bases:
MessengerInterface
Polling 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:
MessengerInterface
Callback 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:
- class CLIMessengerInterface(intro=None, prompt_request='request: ', prompt_response='response: ', out_descriptor=None)[source]#
Bases:
PollingMessengerInterface
Command line message interface is the default message interface, communicating with user via STDIN/STDOUT. This message interface can maintain dialog with one user at a time only.
- _request()[source]#
Method used for sending users request for their input.
- Return type:
List
[Tuple
[Message
,Any
]]- Returns:
A list of tuples: user inputs and context ids (any user ids) associated with the inputs.
- _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.
- async connect(pipeline_runner, **kwargs)[source]#
The CLIProvider generates new dialog id used to user identification on each connect call.
- Parameters:
pipeline_runner (
PipelineRunnerFunction
) – A function that should process user request and return context; usually it’s a_run_pipeline()
function.**kwargs – argument, added for compatibility with super class, it shouldn’t be used normally.