chat.bots#

Contains a basic class that implements a chatbot.

Module Contents#

Classes#

ConversationInfo

Conversation interface available to bots.

ChatMessage

Represents a message.

Bot

Represents a chatbot.

UpperCaseEchoBot

Bot example - echoes text messages in upper-case letters.

CalculatorBot

Bot example - evaluates simple mathematical expressions.

TransparentManagerBot

Transparent bot manager.

CalcOrEchoManagerBot

Bot manager that selects between calculator and upper-case echo bots.

PreSelectManagerBot

Bot manager that lets the user choose a bot as soon as a conversation starts.

WebSocketExternalBot

External bot that interacts with the controller via WebSocket.

Functions#

_generate_local_id()

manager_redirection(*bot_names_or_participants_ids[, ...])

Return a string that indicates the bots that will receive a given message.

manager_approval()

Return a string that indicates that a message has been approved.

all_bots()

Return all installed bots.

class ConversationInfo#

Bases: Protocol

Conversation interface available to bots.

conversation_id: str#
bot_participant_id: str#
send_function: collections.abc.Callable[[dict[str, Any]], Message]#
class ChatMessage#

Represents a message.

Instances do not have any connection with the database model.

id: str#
time: datetime.datetime#
type: MessageType#
sent_by_human: bool#
options: list[str] | None#
local_id: str | None#
text: str | None#
sender_id: str | None#
additional_metadata: dict[str, Any] | None#
event: str | None#
quoted_message_id: str | None#
__post_init__()#
classmethod from_dict(d)#

Create an instance from a dict.

Parameters:

d (dict[str, Any]) – the serialized data

Returns:

a ChatMessage instance with the data in d

Return type:

ChatMessage

class Bot(conversation_info)#

Represents a chatbot.

.

Parameters:

conversation_info (ConversationInfo) – conversation data

receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

update_status(status)#

Receive a status update.

Example: the information that the list of participants has changed.

Parameters:

status (dict[str, Any]) – the status update

Return type:

None

_generate_local_id()#
Return type:

str

class UpperCaseEchoBot(conversation_info)#

Bases: Bot

Bot example - echoes text messages in upper-case letters.

.

Parameters:

conversation_info (ConversationInfo) – conversation data

receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

class CalculatorBot(conversation_info)#

Bases: Bot

Bot example - evaluates simple mathematical expressions.

.

Parameters:

conversation_info (ConversationInfo) – conversation data

receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

evaluate(expression)#

Compute the result of a simple mathematical expression.

Parameters:

expression (str) – the expression to evaluate

Returns:

the calculated result, or “?” if there are errors

Return type:

str

manager_redirection(*bot_names_or_participants_ids, field_overrides=None)#

Return a string that indicates the bots that will receive a given message.

Parameters:
  • bot_names_or_participants_ids (str) – the names or ids of the bots

  • field_overrides (dict[str | Any] | None) – dict from field names to the values that should replace the actual values

Returns:

a JSON-serialised redirection command

Return type:

str

manager_approval()#

Return a string that indicates that a message has been approved.

Returns:

a JSON-serialised approval command

Return type:

str

class TransparentManagerBot(conversation_info)#

Bases: Bot

Transparent bot manager.

It sends the user’s messages to all bots

and accepts all bots’ answers.

.

Parameters:

conversation_info (ConversationInfo) – conversation data

receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

class CalcOrEchoManagerBot(conversation_info, calculator_bot_name, echo_bot_name)#

Bases: Bot

Bot manager that selects between calculator and upper-case echo bots.

.

Parameters:
  • conversation_info (ConversationInfo) – conversation data

  • calculator_bot_name (str) –

  • echo_bot_name (str) –

receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

class PreSelectManagerBot(conversation_info, *, bots, greeting, after_choice, exit_word)#

Bases: Bot

Bot manager that lets the user choose a bot as soon as a conversation starts.

.

Parameters:
  • conversation_info (ConversationInfo) – conversation data

  • bots (list[str]) –

  • greeting (str) –

  • after_choice (str) –

  • exit_word (str) –

bot_in_conversation#
receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

class WebSocketExternalBot(conversation_info, trigger_url)#

Bases: Bot

External bot that interacts with the controller via WebSocket.

.

Parameters:
  • conversation_info (ConversationInfo) – conversation data

  • trigger_url (str) – HTTP URL to be requested for every new conversation

receive_message(message)#

Receive a message.

In order to avoid bots that answer to each other, implementations should ignore messages sent by themselves or other bots.

Parameters:

message (ChatMessage) – the received message

Return type:

None

_start_bot()#
Return type:

None

all_bots()#

Return all installed bots.

Returns:

list of installed bots

Return type:

dict[str, tuple[str, str, list[Any], dict[Any, Any]]]