Let’s stick with the example where the bot greets the customer with a welcome message and a choices menu. See Retrieve the Bot’s Response for the JSON response body.
Hi Jane Doe,
I’m a demo bot for the user guide.
Choose one of the options below
1.Order Status
2.Frequently Asked Questions
3.Transfer To Agent
4.End Chat
There are two methods that the customer can use to send an answer back to the bot.
Method 1: The customer answers the bot with a choice alias or label, for example, “1” for “Order Status”. The message object of the request has the TextMessage type in this case.
// 1a. Answer with the choice alias.
AnyRequestMessage message = new TextMessage()
.text("1")
.type(TextMessage.TypeEnum.TEXT)
.sequenceId(System.currentTimeMillis());
// 1b. Answer with the choice label.
AnyRequestMessage message = new TextMessage()
.text("Order Status")
.type(TextMessage.TypeEnum.TEXT)
.sequenceId(System.currentTimeMillis());
Method 2: The customer answers the bot by selecting a menu choice. The message object of the request has the ChoiceMessage type and a choiceIndex(). The index starts with 0, for example, “Order Status” has an index of 0.
int index=0;
// 2. Answer with the choice index.
AnyRequestMessage message = new ChoiceMessage()
.type(ChoiceMessage.TypeEnum.CHOICE)
.sequenceId(System.currentTimeMillis())
.choiceIndex(index);
Now, build the request with the message.
// Build the request with this message.
BotSendMessageRequest botSendInitMessageRequest = BotRequest
.withMessage(message)
.build();
// Retrieve the RuntimeSessionId from the sessionId returned by the bot when
// the session starts.
RuntimeSessionId runtimeSessionId = new RuntimeSessionId(sessionId);
// Send the request to the bot.
BotResponse response = client
.sendMessage(config, runtimeSessionId, botSendMessageRequest);
Then extract the bot’s message from the response envelope.
// Parse the response envelope.
System.out.println(getResponseMessageAsText(response.getResponseEnvelope()
.getMessages()));
Here is an example output to the customer stating what the order status is.
// Message output
You should receive your item tomorrow.