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.

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

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.

// Build the message with the selected option as a TextMessage type.
AnyRequestMessage message = new TextMessage()
    .text("Transfer To Agent")
    .type(TextMessage.TypeEnum.TEXT)
    .sequenceId(System.currentTimeMillis());

// Build the request.
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);

Retrieve the bot’s message from the response.

// Extract the message from the response envelope.
System.out.println(getResponseMessageAsText(response.getResponseEnvelope()
    .getMessages()));

Here is an example text output from the bot.

Transferring you to agent!
Transferring you to Agent in Target : [class EscalateResponseMessageTargets {
    value: 00GSB0000000d6E2AQ
    type: Salesforce:Core:Queue:Id
}]

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.

// Build the message.
AnyRequestMessage message = **new** **** TransferSucceededRequestMessage()
    .type(TransferSucceededRequestMessage.TypeEnum.TRANSFERSUCCEEDED)
    .sequenceId(System.*currentTimeMillis*());

// Build the request.
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 transfer success message to the bot.
BotResponse response = client
    .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.

// Build the message.
AnyRequestMessage message = TransferFailedRequestMessage()
    .type(TransferFailedRequestMessage.TypeEnum.TRANSFERFAILED)
    .reason(TransferFailedRequestMessage.ReasonEnum.NOAGENTAVAILABLE)
    .sequenceId(System.*currentTimeMillis*());

// Build the request.
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 transfer failed message to the bot.
BotResponse response = client
    .sendMessage(config, runtimeSessionId, botSendMessageRequest);

Here is an example text output from the bot if the client sends a transfer failed message.

// Response output
Unfortunately, there are no agents available at the moment