Transfer the Bot Session to Another Target
The bot can send an escalation message to the client or an option to transfer the session to another target because of, for example, an error, or by user request. Another target can be, for example, a different bot, agent, agent queue, Salesforce skill, or Salesforce flow. 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
In this example, the client selects the “Transfer To Agent” option. This sample code shows how the client sends the option as a TextMessage type to the bot. See Send a Message to the Bot.
1// Build the message with the selected option as a TextMessage type.
2AnyRequestMessage message = new TextMessage()
3 .text("Transfer To Agent")
4 .type(TextMessage.TypeEnum.TEXT)
5 .sequenceId(System.currentTimeMillis());
6
7// Build the request.
8BotSendMessageRequest botSendInitMessageRequest = BotRequest
9 .withMessage(message)
10 .build();
11
12// Retrieve the RuntimeSessionId from the sessionId returned by the bot when
13// the session starts.
14RuntimeSessionId runtimeSessionId = new RuntimeSessionId(sessionId);
15
16// Send the request to the bot.
17BotResponse response = client
18 .sendMessage(config, runtimeSessionId, botSendMessageRequest);
Retrieve the bot’s message from the response.
1// Extract the message from the response envelope.
2System.out.println(getResponseMessageAsText(response.getResponseEnvelope()
3 .getMessages()));
Here is an example text output from the bot.
1Transferring you to agent!
2Transferring you to Agent in Target : [class EscalateResponseMessageTargets {
3 value: 00GSB0000000d6E2AQ
4 type: Salesforce:Core:Queue:Id
5}]
The API itself doesn’t route the bot session to another target. The API only responds with an array of target objects configured by the bot-admin. The client is responsible for transferring the session to one of the targets after receiving an escalation message from the bot. After the client routes the session to another target, the client must send back a transfer success or transfer failed message to the bot.
If the client successfully transfers to an agent, the client must send a message with type TransferSucceededRequestMessage to the bot, as described in the next section. If it fails, the client must send a message with type TransferFailedRequestMessage to the bot. The client can then End a Bot Session.
Here is an example of a transfer success message from the client to the bot.
1// Build the message.
2AnyRequestMessage message = **new** **** TransferSucceededRequestMessage()
3 .type(TransferSucceededRequestMessage.TypeEnum.TRANSFERSUCCEEDED)
4 .sequenceId(System.*currentTimeMillis*());
5
6// Build the request.
7BotSendMessageRequest botSendInitMessageRequest = BotRequest
8 .withMessage(message)
9 .build();
10
11// Retrieve the RuntimeSessionId from the sessionId returned by the bot when
12// the session starts.
13RuntimeSessionId runtimeSessionId = new RuntimeSessionId(sessionId);
14
15// Send the transfer success message to the bot.
16BotResponse response = client
17 .sendMessage(config, runtimeSessionId, botSendMessageRequest);
If you extract the text output from the bot’s response, the output must be empty for a transfer success response.
Here’s an example of a transfer failed message from the client to the bot. If the bot sends an escalation message but the client isn’t able to transfer the customer to the directed source, the client must send a transfer failed message to the bot.
1// Build the message.
2AnyRequestMessage message = TransferFailedRequestMessage()
3 .type(TransferFailedRequestMessage.TypeEnum.TRANSFERFAILED)
4 .reason(TransferFailedRequestMessage.ReasonEnum.NOAGENTAVAILABLE)
5 .sequenceId(System.*currentTimeMillis*());
6
7// Build the request.
8BotSendMessageRequest botSendInitMessageRequest = BotRequest
9 .withMessage(message)
10 .build();
11
12// Retrieve the RuntimeSessionId from the sessionId returned by the bot when
13// the session starts.
14RuntimeSessionId runtimeSessionId = new RuntimeSessionId(sessionId);
15
16// Send the transfer failed message to the bot.
17BotResponse response = client
18 .sendMessage(config, runtimeSessionId, botSendMessageRequest);
Here is an example text output from the bot if the client sends a transfer failed message.
1// Response output
2Unfortunately, there are no agents available at the moment