Sessions

ChatterBot supports the ability to have multiple concurrent chat sessions. A chat session is where the chat bot interacts with a person, and supporting multiple chat sessions means that your chat bot can have multiple different conversations with different people at the same time.

class chatterbot.conversation.session.Session[source]

A session is an ordered collection of statements that are related to each other.

class chatterbot.conversation.session.ConversationSessionManager[source]

Object to hold and manage multiple chat sessions.

get(session_id, default=None)[source]

Return a session given a unique identifier.

new()[source]

Create a new conversation.

update(session_id, conversance)[source]

Add a conversance to a given session if the session exists.

Each session object holds a queue of the most recent communications that have occurred during that session. The queue holds tuples with two values each, the first value is the input that the bot received and the second value is the response that the bot returned.

class chatterbot.queues.ResponseQueue(maxsize=10)[source]

An extension of the FixedSizeQueue class with utility methods to help manage the conversation.

get_last_input_statement()[source]

Return the last response that was given.

get_last_response_statement()[source]

Return the last statement that was received.

Session scope

If two ChatBot instances are created, each will have sessions separate from each other.

An adapter can access any session as long as the unique identifier for the session is provided.

Session example

The following example is taken from the Django ChatterBotView built into ChatterBot. In this method, the unique identifiers for each chat session are being stored in Django’s session objects. This allows different users who interact with the bot through different web browsers to have separate conversations with the chat bot.

def post(self, request, *args, **kwargs):
    """
    Return a response to the statement in the posted data.
    """
    input_data = json.loads(request.read().decode('utf-8'))

    self.validate(input_data)

    chat_session = self.get_chat_session(request)

    response = self.chatterbot.get_response(input_data, chat_session.id_string)
    response_data = response.serialize()

    return JsonResponse(response_data, status=200)