Creating a Session

New sessions may be created using a Diatheke model, which defines what is allowed in the session’s conversation. For example, if the model defines an intent that includes the phrase “check account balance”, a user may speak that phrase and expect Diatheke to respond. If, however, the user speaks the phrase “buy pizza”, and that intent is not in the model, Diatheke may ignore the phrase or reply with an error message, such as “I didn’t understand that”.

To begin a new dialog, create a session by doing the following:


// The Diatheke model ID is defined in the Diatheke server config file.
diathekeModelID := "1"
sessionID, err := client.NewSession(context.Background(), diathekeModelID)

// Be sure to close the session when we are done with it.
defer client.EndSession(context.Background(), sessionID)

// The Diatheke model ID is defined in the Diatheke server config file.
std::string diathekeModelID = "1";
std::string sessionID = client.newSession(diathekeModelID);

// Be sure to close the session when we are done with it.
client.endSession(sessionID);

# The Diatheke model ID is defined in the Diatheke server config file.
diatheke_model_id = "1"
session_id = client.new_session(diatheke_model_id)

# Be sure to close the session when we are done with it.
client.end_session(session_id)

// The Diatheke model ID is defined in the Diatheke server config file.
let modelID = "1"
var sessionID: String

client.newSession(model: modelID, success: { (sessionID) in
    self.sessionID = sessionID
}) { (error) in
    print(error.localizedDescription)
}

// Be sure to close the session when we are done with it.
client.endSession(sessionID: sessionID) { (error) in
    print(error.localizedDescription)
}

String modelID = "1"
DiathekeOuterClass.NewSessionRequest request = DiathekeOuterClass.NewSessionRequest.newBuilder()
        .setModel(modelId)
        .build();
mClient.newSession(request, new StreamObserver<DiathekeOuterClass.SessionID>() {
    @Override
    public void onNext(DiathekeOuterClass.SessionID value) {
        String sessionId = value.getSessionId();
    }

    @Override
    public void onError(Throwable t) {

    }

    @Override
    public void onCompleted() {

    }
});

Creating a new session will return a session ID, which is required for all the session functions in the SDK.

As shown in the example, sessions should be closed when they are no longer in use. This has the side-effect of freeing resources related to the session on the server.

It should also be noted that simply exiting the application or closing the client will not end a session on the server. This is by design - in some situations, it may be convenient to save the sessionID and resume session interactions after creating a new client connection. For example, it might not make sense to preserve the network connection for a long running commands. The client code could save the session ID, close the client connection, execute the command, then restart the client and resume the session when the command has finished.

The Session Object

For those who prefer a more object oriented approach to interacting with sessions, the SDK provides a convenience class that wraps the client and session ID. The session object includes all the session functions as class members.


// Wrap the session ID in the convenience class
session := diatheke.Session{
    ID: sessionID,
    Parent: client,
}

// Use session methods without the ID as a parameter
err := session.EndSession(context.Background());

// Wrap the session ID in the convenience class
Diatheke::Session session(sessionID, &client);

// Use session methods without the ID as a parameter
session.EndSession();

# Wrap the session ID in the convenience class
session = diatheke.Session(session_id, client)

# Use session methods without the ID as a parameter
session.end_session()

// Wrap the session ID in the convenience class
let session = Session(id: sessionID, client: client)

# Use session methods without the ID as a parameter
session.endSession { (error) in
    print(error.localizedDescription)
}

// create request
DiathekeOuterClass.SessionID session = DiathekeOuterClass.SessionID.newBuilder()
                .setSessionId(sessionId)
                .build();
mClient.endSession(session, new StreamObserver<DiathekeOuterClass.Empty>() {
    @Override
    public void onNext(DiathekeOuterClass.Empty value) {
        
    }

    @Override
    public void onError(Throwable t) {

    }

    @Override
    public void onCompleted() {

    }
});