Retrieve the Bot’s Response

The BotResponse object provides methods to retrieve the HTTP status code, response HTTP header parameters and response envelope from the bot’s message.

1// Retrieve the HTTP status code.
2int httpStatusCode = response.getHttpStatusCode();
3
4// Retrieve the response HTTP header parameters.
5BotHttpHeaders httpHeaders = response.getHttpHeaders();
6// Retrieve all header parameters.
7Map<String, Collection<String>> allHeaders = httpHeaders.getAll()
8// Retrieve a specific header parameter.
9Optional<String> requestId = httpHeaders.getRequestIdHeader();
10
11// Retrieve the response envelope.
12ResponseEnvelope responseEnvelope = endSessionResponse
13    .getResponseEnvelope();

Here is an example of a response body from the bot in JSON format. You can find the API response body schema in our Reference for API v5. This example messages array has messages with the text type and choices type. These types correspond to the TextResponseMessage and ChoicesResponseMessage schemas in the API reference.

1{
2  "sessionId": "32f468af-bbea-48ed-947d-95e34c05023b",
3  "botVersion": "0X9SB00000007cb0AA",
4  "processedSequenceIds": [1650390206434],
5  "messages": [
6    {
7      "type": "text",
8      "id": "fde67029-c480-454d-9326-8e3024076b92",
9      "text": "What is Refund policy?",
10      "schedule": {
11        "responseDelayMilliseconds": 1200
12      }
13    },
14    {
15      "type": "text",
16      "id": "48ddb49d-cd4e-4f6a-b897-3e6730265f8b",
17      "text": "Product are refundable with 30 days of purchase.",
18      "schedule": {
19        "responseDelayMilliseconds": 1200
20      }
21    },
22    {
23      "type": "text",
24      "id": "2ef4993b-4e48-4686-806d-2b0bcf053f2b",
25      "text": "Choose one of the options below",
26      "schedule": {
27        "responseDelayMilliseconds": 1200
28      }
29    },
30    {
31      "type": "choices",
32      "id": "0267b1d2-d48b-4457-b364-e65e7b52a84e",
33      "choices": [
34        {
35          "label": "Order Status",
36          "alias": "1",
37          "id": "8acdd120-0bf7-4f4f-bece-6946dd8ece04"
38        },
39        {
40          "label": "Frequently Asked Questions",
41          "alias": "2",
42          "id": "95356019-f46c-4fc6-bd81-df67679b91ea"
43        },
44        {
45          "label": "Transfer To Agent",
46          "alias": "3",
47          "id": "3e7debac-cb3d-4154-a923-802f45b1f0b7"
48        },
49        {
50          "label": "End Chat",
51          "alias": "4",
52          "id": "96ad8494-b407-439e-968d-62762d902157"
53        }
54      ],
55      "widget": "menu",
56      "schedule": {
57        "responseDelayMilliseconds": 1200
58      }
59    }
60  ],
61  "variables": null,
62  "_links": null
63}

The SDK automatically deserializes the JSON body into Java classes. This sample code demonstrates how to extract the bot’s message to the customer from the response body. Not all possible response body schemas and fields are included here.

1private String getResponseMessageAsText(List<AnyResponseMessage> messages) {
2    StringBuilder sb = new StringBuilder();
3    for(AnyResponseMessage message : messages){
4      if (message instanceof TextResponseMessage){
5        sb.append(((TextResponseMessage) message).getText())
6            .append("\n");
7      }else if (message instanceof ChoicesResponseMessage){
8        List<ChoicesResponseMessageChoices> choices =
9            ((ChoicesResponseMessage) message)
10            .getChoices();
11        for (ChoicesResponseMessageChoices choice : choices){
12          sb.append(choice.getAlias())
13              .append(".")
14              .append(choice.getLabel())
15              .append("\n");
16        }
17      }else if (message instanceof EscalateResponseMessage){
18        List<EscalateResponseMessageTargets> targets =
19            ((EscalateResponseMessage) message)
20            .getTargets();
21        // Add code here to transfer the bot to another target.
22        sb.append("Transferring you to Agent in Target : " + targets);
23
24      }else if (message instanceof SessionEndedResponseMessage){
25        ReasonEnum reason = ((SessionEndedResponseMessage) message)
26            .getReason();
27        // Add code here to clean up the channel.
28        sb.append("The Chat session ended with Reason : " + reason);str
29      }
30    }
31    return sb.toString();
32}
33
34System.out.println(getResponseMessageAsText(response.getResponseEnvelope()
35    .getMessages()));

Here’s the extracted bot’s message to the customer in text format.

1Hi,
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