Send a Message to the Bot

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.

1Hi Jane Doe,
2I’m a demo bot for the user guide.
3Choose one of the options below
41.Order Status
52.Frequently Asked Questions
63.Transfer To Agent
74.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.

1// 1a. Answer with the choice alias.
2AnyRequestMessage message = new TextMessage()
3    .text("1")
4    .type(TextMessage.TypeEnum.TEXT)
5    .sequenceId(System.currentTimeMillis());
6
7// 1b. Answer with the choice label.
8AnyRequestMessage message = new TextMessage()
9    .text("Order Status")
10    .type(TextMessage.TypeEnum.TEXT)
11    .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.

1int index=0;
2
3// 2. Answer with the choice index.
4AnyRequestMessage message = new ChoiceMessage()
5    .type(ChoiceMessage.TypeEnum.CHOICE)
6    .sequenceId(System.currentTimeMillis())
7    .choiceIndex(index);

Now, build the request with the message.

1// Build the request with this message.
2BotSendMessageRequest botSendInitMessageRequest = BotRequest
3    .withMessage(message)
4    .build();
5
6// Retrieve the RuntimeSessionId from the sessionId returned by the bot when
7// the session starts.
8RuntimeSessionId runtimeSessionId = new RuntimeSessionId(sessionId);
9
10// Send the request to the bot.
11BotResponse response = client
12    .sendMessage(config, runtimeSessionId, botSendMessageRequest);

Then extract the bot’s message from the response envelope.

1// Parse the response envelope.
2System.out.println(getResponseMessageAsText(response.getResponseEnvelope()
3    .getMessages()));

Here is an example output to the customer stating what the order status is.

1// Message output
2You should receive your item tomorrow.