# Workflow to Enable a Live Agent

Enable a Live Agent by setting up the `Contact Live Agent` Intent and other related Intents/Flows.

You can use the `Contact Live Agent` Intent as the identifier whether your application or bot is setup with Live Agent or not.

There are 3 options for the Fulfillments of this `Contact Live Agent` Intent.

* Set up a flow
  * This flow will **only** get invoked when a user directly types a query that triggers intent `Contact Live Agent` , for example, `I want to talk to a live agent`
  * This flow will **NOT** get invoked when a user starts handing off to live agent from `Escalation Feedback` flow.
  * In the `Contact Live Agent Flow`, the flow builder can ask user to confirm `contact live agent` or collect any necessary information if needed.
  * After the execution of this flow, if the next step to really to contact an agent in the external system, please output a variable `handleOffToLiveAgent` as `True`, as the example showed in the next section. Please don’t misuse the sub-flow by calling `Feedback Escalation flow` or `Preprocess Live Agent flow` as sub-flows.\ <br>

    <figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FvcHFYiBnj1HslfkzAMBD%2Fimage.png?alt=media&#x26;token=d73fa57b-a925-4a44-a572-af5407915b45" alt=""><figcaption></figcaption></figure>
* Set up a customized message
  * This customized message will be presented to users rather than handing off to the external Live Agent system
* Leave fulfillment empty

### 2. Import `Preprocess Live Agent` skill (Optional) <a href="#id-2.-import-preprocess-live-agent-skill-optional" id="id-2.-import-preprocess-live-agent-skill-optional"></a>

If there is any preparation needs to do before starting a session with external `Live Agent`, then `Preprocess Live Agent` skill should get imported. For example,

* Get Conversation History
* Create a ticket before handing off to Live Agent
* Authorize the user (OAuth)

**Note:**

In order to hand off to external `Live Agent`, there are two required skill/flow outputs. (If they are missing, please manually add them.&#x20;

1. **initMsg** - the intial message sent to the agent
2. **handleOffToLiveAgent** - boolean choice
   1. `True`: Continue to hand off to live agent
   2. `False`: Stop handing off to live agent

#### Use Case Example 1 <a href="#example-1" id="example-1"></a>

When you only need to get the conversation history and then send the history to the agent as initial messages.

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FYhsDdFiHsISaZ0c03jJl%2Fimage.png?alt=media&#x26;token=dfc21260-707f-47f2-b390-86d226f0e904" alt=""><figcaption></figcaption></figure>

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FVBKgMRIHmrmqdSRMWvpJ%2Fimage.png?alt=media&#x26;token=82216a87-9926-45a6-b0d3-90c9f61b5377" alt="" width="408"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2F0U8Wb2LfpEUY687SKfMS%2Fimage.png?alt=media&#x26;token=73b4a3c0-e1dc-4a55-97df-f929ec448fe9" alt="" width="406"><figcaption></figcaption></figure></div>

&#x20;

#### Use Case Example 2 <a href="#example-2" id="example-2"></a>

Before handing off to a live agent, you need to Authorize Users, Create a Ticket, and Send the Ticket URL as the initial message.

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FKJlyzYtpuMWANLGggziU%2Fimage.png?alt=media&#x26;token=473f45ee-fc51-43a5-90fe-b37c38028d06" alt="" width="563"><figcaption></figcaption></figure></div>

#### 3. Import `Message Enrichment` skill (Optional) <a href="#id-3.-import-message-enrichment-skill-optional" id="id-3.-import-message-enrichment-skill-optional"></a>

When receiving a message from external `Live Agent`, it is possible to enrich the message and send it to the user. For example, receive a URL, that represents a ticket, from `Live Agent`, the `Message Enrichment` Intent is able to customize and convert it to a ticket card and send it to the user.

**Note:** if the `Message Enrichment` is not imported, or, `Message Enrichment` is not able to enrich a message, this message will be presented to user as it is.

**Example:** Parse the Ticket URL into a Ticket card

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FkGdEbvTtcpb3zCTHRtti%2Fimage.png?alt=media&#x26;token=e4fb4528-87c9-40fe-a17e-07343b240b6c" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2Fl5ortoDgqJLQQ1I7PQUJ%2Fimage.png?alt=media&#x26;token=d469d5aa-d894-4abc-9afa-a4c2866d319e" alt=""><figcaption></figcaption></figure>

**Example:** Make the URL link clickable.

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FbETcb259AV1pHjTJ5uAf%2Fimage.png?alt=media&#x26;token=09676a00-d11c-462e-bc3f-48ebe62adb9f" alt=""><figcaption></figcaption></figure>

```javascript
var Pattern = Java.type('java.util.regex.Pattern');
var Matcher = Java.type('java.util.regex.Matcher');
var StringBuffer = Java.type('java.lang.StringBuffer');
var urlRegex = "((https?:\\/\\/|www\\.)"
      + "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*"
      + "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*))";
var pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
var urlMatcher = pattern.matcher(message);
var sb = new StringBuffer();
while(urlMatcher.find()) {
      var found =urlMatcher.group(0);
      if (!found.toLowerCase().startsWith("http") && ! found.toLowerCase().startsWith("ftp")) {
        urlMatcher.appendReplacement(sb, "<a href='http://" + found + "'>" + found + "</a>");
      } else {
        urlMatcher.appendReplacement(sb, "<a href='" + found + "'>" + found + "</a>");
      }
}
urlMatcher.appendTail(sb);
message = sb.toString();
return message;
```

#### 4. Import `Fallback` skill (Optional) <a href="#id-4.-import-fallback-skill-optional" id="id-4.-import-fallback-skill-optional"></a>

The `Fallback` Intent is used in the `exception` cases before the agent picks up the user’s request, when we handoff the user to the agent.

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FfOIgdzYieVZP9Hbi5DJ2%2Fimage.png?alt=media&#x26;token=330eba0d-e0f0-4dd4-950d-3aeb0e9b434e" alt=""><figcaption></figcaption></figure>

**Live Agent Connection Timeout**

There could be a delay, after we handoff a user to live agent and before an agent picks up the user’s request. So, the user has to wait until an agent picks up the request.

If the delay (the user’s waiting time) is longer than the pre-configured live agent connection timeout setting (in minutes), then, AISERA shall automatically end the un-picked live agent conversation.

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FAgy3Y0vGmfReXBZOU4wd%2Fimage.png?alt=media&#x26;token=7aef2dd3-8aa3-46c4-9db3-71774e8d7f1a" alt=""><figcaption></figcaption></figure></div>

When the connection to Live Agent timeouts, a conversation message with key `live_agent_connection_timeout`

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FAkYSfYqqPaOxuD7BfD7w%2Fimage.png?alt=media&#x26;token=ab494f98-a543-4dd7-89ee-7ef7c165d00e" alt=""><figcaption></figcaption></figure>

For example,

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2Fy2Zsa1IVXpcrqN2fFldA%2Fimage.png?alt=media&#x26;token=788e32a4-6767-42cb-88d4-d170df3f323a" alt=""><figcaption></figcaption></figure>

If `Fallback` skill is imported to the bot, then, this skill will be considered as the fallback option for live agent and used in the live agent connection timeout case.

When the live agent connection timeouts, then, apart from the live agent connection timeout message showing up, a button (button label is configured through conversation message with key `live_agent_connection_timeout_fallback_button_label`) will also show up in the webchat.

When clicking the button, `Fallback` skill will be triggered, with the `fallback type = LiveAgentTimeout`. In the `Fallback` skill flow, the customer can config the expected behavior, including Create Ticket.

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FeW9cgRtMOl7aW6ietUd5%2Fimage.png?alt=media&#x26;token=bf39bace-ece8-45bd-85b5-2ef9bc734aa2" alt="" width="547"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FsShhvPmFJU4ousrH5BZK%2Fimage.png?alt=media&#x26;token=efb3c4de-510c-4394-9c9f-18990dff9134" alt="" width="559"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FX7EgyxJwMwldipjxNpf7%2Fimage.png?alt=media&#x26;token=a2253b3a-7089-41c6-b165-bcb52dab7e4f" alt="" width="563"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FqbINRKApJWoETn9BhITs%2Fimage.png?alt=media&#x26;token=9d070341-06d1-400d-accc-7e28dec9d40b" alt="" width="392"><figcaption></figcaption></figure></div>

2\. No Agent: fallback option

For some external Live Agent system, for example, `Salesforce Live Agent`, if there is no agent available, instead of returning `failure` when we start a live agent conversation, `Salesforce Server` will fail the request.

Live Agent Systems that apply to : `Salesforce`, `Servicenow` and `Genesys`

So, if the external live agent system fails the request due to no available agent, we shall use `Fallback` skill with a new branch`fallbackType = NoLiveAgent` to provide other options for the user.

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2F4EG7iPvBIwzGDNkCVzE2%2Fimage.png?alt=media&#x26;token=5966288f-523a-4d36-a555-ca4ff108fab4" alt="" width="399"><figcaption></figcaption></figure></div>

&#x20;

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FF6oLH3XKxqREG4RuEJs3%2Fimage.png?alt=media&#x26;token=44f67c34-17f2-413f-ab08-93aa7b80fa75" alt=""><figcaption></figcaption></figure>

&#x20;

#### 5. Deflect initial Live Agent request (Optional) <a href="#id-5.-deflect-initial-live-agent-request-optional" id="id-5.-deflect-initial-live-agent-request-optional"></a>

When the user types `contact live agent`, instead of directly handing off to the live agent, the admin can try to deflect this request by asking user to clarify the question, eg, `Would you please clarify your question?`

If the user continues to say `contact live agent`, AISERA will hand the user off to the live agent. Otherwise, AISERA will serve based on the user’s clarified question.

Go to `Settings => Deflect Escalate Intents => Deflect Initial Live Agent Request`

<div align="left"><figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2F3xmv4nSx6hvbbbXxwQGz%2Fimage.png?alt=media&#x26;token=16ffb120-0b22-4b38-a7eb-845767af3fc6" alt="" width="557"><figcaption></figcaption></figure></div>

You shall see this after user says `Contact Live Agent`

<figure><img src="https://3281977978-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvBFXjH9S1CAy9f5hzg5Q%2Fuploads%2FWq8ISLJgQ08al1vxDIzz%2Fimage.png?alt=media&#x26;token=4916e405-9fee-42a5-beec-ee5cc4dd8dfd" alt=""><figcaption></figcaption></figure>

Under app Conversation messages, search for “live\_agent\_pre\_fulfillment\_msg” for tuning this message.
