To start a session with the bot, the request body from the external channel BotSendMessageRequest must have the type TextMessage. Here the customer sends a “Hello” message to the bot. The requestId is an optional UUID that you can provide to help track the requests.
// Create the message.
AnyRequestMessage message = new TextMessage()
.text("Hello")
.type(TextMessage.TypeEnum.TEXT)
.sequenceId(System.currentTimeMillis());
// Create the request body with the message.
BotSendMessageRequest botSendInitMessageRequest = BotRequest
.withMessage(message)
.requestId(UUID.randomUUID().toString())
.build();
Then retrieve the ExternalSessionId from the external channel, for example, a Slack or Twitter thread ID. We’re using a random UUID for the external session ID in the sample code.
ExternalSessionId externalSessionKey =
new ExternalSessionId(UUID.randomUUID().toString());
Start the session using the startChatSession() method.
BotResponse response = client
.startChatSession(config, externalSessionKey, botSendInitMessageRequest);
Retrieve the bot’s session ID from the BotResponse object. Use this session ID to continue and end the session.
String sessionId = response.getResponseEnvelope().getSessionId();