This section provides code samples of running a bot session with the SessionManagedChatbotClient interface.
Set Up the Client
The SessionManagedChatbotClient adds session management capabilities on top of the BasicChatbotClient. Therefore, to create an instance of the session-managed client, pass BasicChatbotClient as a parameter to the SessionManagedChatbotClient. See Use the Basic Bot Client.
// Create a JWT Bearer authentication mechanism.
AuthMechanism oAuth = JwtBearerOAuth.with()
.privateKeyFilePath(privateKeyFilePath)
.loginEndpoint(loginEndpoint)
.connectedAppId(connectedAppId)
.connectedAppSecret(secret)
.userId(userId)
.build();
// Create a session-managed client.
SessionManagedChatbotClient client = ChatbotClients
.sessionManaged()
.basicClient(ChatbotClients.basic()
.basePath(basePath)
.authMechanism(oAuth)
.build())
.cache(new RedisCache(YOUR_TTL_SECONDS,YOUR_REDIS_URL))
.build();
If you are running the client locally, you can use InMemoryCache(YOUR_TTL_SECONDS) instead of RedisCache(YOUR_TTL_SECONDS,YOUR_REDIS_URL) for the .cache value. The InMemoryCache is the better caching method when running the client locally but for production use RedisCache or your own distributed cache.
Then create a RequestConfig object and add the bot ID, org ID, ForceConfig Endpoint that you already set up in the Set Up the Configuration Variables section.
RequestConfig config = RequestConfig.with()
.botId(botId)
.orgId(orgId)
.forceConfigEndpoint(forceConfigEndPoint)
.build();
Send Messages
The session-managed client starts a new session and sends subsequent messages using the same method instead of two separate methods. A session is tracked by a combination of the external session ID, bot ID, and org ID. If the client sends a message request with a new combination of those IDs, the bot starts a new session. If the combination exists, the message is part of an active session.
// Retrieve the external session ID from the external channel.
ExternalSessionId externalSessionId =
new ExternalSessionId(UUID.randomUUID().toString());
// Build the request to send a message to the bot.
BotSendMessageRequest sendMessageRequest = BotRequest
.withMessage(buildTextMessage(message))
.build();
// Send the request using the external session ID.
BotResponse response = client
.sendMessage(config, externalSessionId, sendMessageRequest);
Retrieve the Bot Response
The BotResponse object is the same as the one of the BasicChatbotClient so you can Retrieve the Bot’s Response the same way.
End the Bot Session
The code is the same as End a Bot Session for the BasicChatbotClient except that you pass externalSessionId instead of the bot’s internal session ID.
// Build the request to end the bot session.
BotEndSessionRequest botEndSessionRequest = BotRequest
.withEndSession(EndSessionReason.USERREQUEST).build();
// Use the external session ID of the external channel.
ExternalSessionId externalSessionId =
new ExternalSessionId(UUID.randomUUID().toString());
// Send the request to the bot to end the session.
BotResponse endSessionResponse = client
.endChatSession(config, externalSessionId, botEndSessionRequest);