websocket_platform#

class besser.bot.platforms.websocket.websocket_platform.WebSocketPlatform(bot, use_ui=True)[source]#

Bases: Platform

The WebSocket Platform allows a bot to communicate with the users using the WebSocket bidirectional communications protocol.

This platform implements the WebSocket server, and it can establish connection with a client, allowing the bidirectional communication between server and client (i.e. sending and receiving messages).

Note

We provide a UI (streamlit_ui) implementing a WebSocket client to communicate with the bot, though you can use or create your own UI as long as it has a WebSocket client that connects to the bot’s WebSocket server.

Parameters:
  • bot (Bot) – the bot the platform belongs to

  • use_ui (bool) – whether to use the built-in UI or not

_bot#

The bot the platform belongs to

Type:

Bot

_host#

The WebSocket host address (e.g. localhost)

Type:

str

_port#

The WebSocket port (e.g. 8765)

Type:

int

_use_ui#

Whether to use the built-in UI or not

Type:

bool

_connections#

The list of active connections (i.e. users connected to the bot)

Type:

dict[str, ServerConnection]

_websocket_server#

The WebSocket server instance

Type:

WebSocketServer or None

_message_handler#

The function that handles the user connections (sessions) and incoming messages

Type:

Callable[[ServerConnection], None]

_abc_impl = <_abc._abc_data object>#
_send(session_id, payload)[source]#

Send a payload message to a specific user.

Parameters:
  • session_id (str) – the user to send the response to

  • payload (Payload) – the payload message to send to the user

initialize()[source]#

Initialize the platform. This function is called right before starting the platform.

reply(session, message)[source]#

Send a bot reply, i.e. a text message, to a specific user.

Parameters:
  • session (Session) – the user session

  • message (str) – the message to send to the user

reply_dataframe(session, df)[source]#

Send a DataFrame bot reply, i.e. a table, to a specific user.

Parameters:
reply_file(session, file)[source]#

Send a file reply to a specific user

Parameters:
  • session (Session) – the user session

  • file (File) – the file to send

reply_location(session, latitude, longitude)[source]#

Send a location reply to a specific user.

Parameters:
  • session (Session) – the user session

  • latitude (str) – the latitude of the location

  • longitude (str) – the longitude of the location

reply_options(session, options)[source]#

Send a list of options as a reply. They can be used to let the user choose one of them

Parameters:
  • session (Session) – the user session

  • options (list[str]) – the list of options to send to the user

reply_plotly(session, plot)[source]#

Send a Plotly figure as a bot reply, to a specific user.

Parameters:
  • session (Session) – the user session

  • plot (plotly.graph_objs.Figure) – the message to send to the user

start()[source]#

Start the platform.

stop()[source]#

Terminate the platform execution.