Use the Session-Managed Bot Client

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.

1// Create a JWT Bearer authentication mechanism.
2AuthMechanism oAuth = JwtBearerOAuth.with()
3    .privateKeyFilePath(privateKeyFilePath)
4    .loginEndpoint(loginEndpoint)
5    .connectedAppId(connectedAppId)
6    .connectedAppSecret(secret)
7    .userId(userId)
8    .build();
9
10// Create a session-managed client.
11SessionManagedChatbotClient client = ChatbotClients
12    .sessionManaged()
13    .basicClient(ChatbotClients.basic()
14        .basePath(basePath)
15        .authMechanism(oAuth)
16        .build())
17    .cache(new RedisCache(YOUR_TTL_SECONDS,YOUR_REDIS_URL))
18    .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.

1RequestConfig config = RequestConfig.with()
2    .botId(botId)
3    .orgId(orgId)
4    .forceConfigEndpoint(forceConfigEndPoint)
5    .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.

1// Retrieve the external session ID from the external channel.
2ExternalSessionId externalSessionId =
3    new ExternalSessionId(UUID.randomUUID().toString());
4
5// Build the request to send a message to the bot.
6BotSendMessageRequest sendMessageRequest = BotRequest
7    .withMessage(buildTextMessage(message))
8    .build();
9
10// Send the request using the external session ID.
11BotResponse response = client
12   .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.

1// Build the request to end the bot session.
2BotEndSessionRequest botEndSessionRequest = BotRequest
3    .withEndSession(EndSessionReason.USERREQUEST).build();
4
5// Use the external session ID of the external channel.
6ExternalSessionId externalSessionId =
7    new ExternalSessionId(UUID.randomUUID().toString());
8
9// Send the request to the bot to end the session.
10BotResponse endSessionResponse = client
11    .endChatSession(config, externalSessionId, botEndSessionRequest);