Understand the Structure of the Bot Client Interfaces
There are two interfaces of the bot client: BasicChatbotClient and SessionManagedChatbotClient.
With the BasicChatbotClient interface, there’s a separate method to start and continue the session. You track the session using the bot’s runtime session ID. To start a session, you provide an ExternalSessionId of an external channel, such as a Slack or Twitter thread, to startChatSession(). Then the bot generates a unique internal session ID, which you use as the RuntimeSessionId, to send subsequent messages or end the active session.
1// The basic bot client
2public interface BasicChatbotClient {
3
4 BotResponse startChatSession(RequestConfig config,
5 ExternalSessionId sessionId,
6 BotSendMessageRequest requestEnvelope);
7
8 BotResponse sendMessage(RequestConfig config,
9 RuntimeSessionId sessionId,
10 BotSendMessageRequest requestEnvelope);
11
12 BotResponse endChatSession(RequestConfig config,
13 RuntimeSessionId sessionId,
14 BotEndSessionRequest requestEnvelope);
15
16 Status getHealthStatus()
17}The SessionManagedChatbotClient interface adds session management capabilities to the basic bot client. With the session-managed client, there’s no startChatSession() method and RuntimeSessionId. You start the session and send subsequent messages using the same method sendMessage(). You only need the ExternalSessionId since the client maps the external session ID to the internal one in the cache. For example, you could use a Slack thread ID to track all bot messages in a session without knowing the bot’s internal session ID.
1// The session-managed bot client
2public interface SessionManagedChatbotClient {
3
4 BotResponse sendMessage(RequestConfig config,
5 ExternalSessionId sessionId,
6 BotSendMessageRequest requestEnvelope);
7
8 BotResponse endChatSession(RequestConfig config,
9 ExternalSessionId sessionId,
10 BotEndSessionRequest requestEnvelope);
11
12 Status getHealthStatus()
13}Here are the features that both basic bot and session-managed clients share.
- The
RequestConfigobject contains the bot configuration variables. - The
BotSendMessageRequestobject contains the message and message options to start and continue a session. - The
BotEndSessionRequestobject contains the message to end the session. - The return type is
BotResponsefor all session requests. - The
getHealthStatus()method returns the health status of the bot. - To create an instance of either client, you require a runtime URL and authentication mechanism.
1// Create a basic bot client.
2BasicChatbotClient client = ChatbotClients.basic()
3 .basePath(YOUR_RUNTIME_URL)
4 .authMechanism(YOUR_OAUTH_MECHANISM)
5 .build();
6
7// Create a session-managed client.
8SessionManagedChatbotClient client = ChatbotClients
9 .sessionManaged()
10 .basicClient(ChatbotClients.basic()
11 .basePath(YOUR_RUNTIME_URL)
12 .authMechanism(YOUR_OAUTH_MECHANISM)
13 .build())
14 .cache(new RedisCache(YOUR_TTL_SECONDS,YOUR_REDIS_URL))
15 .build();