Handlebars

Simple example

AgentX lets you use Handlebars templates in your answers, including the Markdown and SSML fields, so you can include variable substitution and conditional elements. Let’s try a simple example to illustrate the concept:

  1. Log in to the Content Designer, and choose Add.

  2. Enter ID: Handlebars.001

  3. Enter question: What is my interaction count?

  4. Enter answer: So far, you have interacted with me {{UserInfo.InteractionCount}} times.

  5. Save the new item.

  6. Use the Web UI to say “What is my interaction count?” to your bot, and listen to it respond.

  7. Ask a few more questions, and then ask “What is my interaction count?” again. Notice that the value has increased.

  8. In Content designer, edit item Handlebars.001

  9. Modify answer to:

    So far, you have interacted with me {{UserInfo.InteractionCount}} times.   
    {{#ifCond UserInfo.TimeSinceLastInteraction '>' 60}}   
       It has been over a minute since I heard from you last.. I almost fell asleep!   
    {{else}}   
       Keep those questions coming fast.. It's been {{UserInfo.TimeSinceLastInteraction}} seconds since your last interaction.   
    {{/ifCond}}
    
  10. Use the Web UI to interact with the bot again. Wait over a minute between interactions and observe the conditional answer in action.

Available variables

AgentX exposes the following content to the Handlebars context:

Context Variable

Notes

ClientType

Detected client type. May be LEX.ChimeSDK.Voice; LEX.AmazonConnect.Text|Voice, LEX.LexWebUI.Text|Voice, or LEX.Text|Voice

UserInfo.UserId

Lex or Alexa user id set by client (For Twilio SMS, UserId is the user’s phone number)

UserInfo.InteractionCount

Accumulating interaction count for UserId (not including current interaction)

UserInfo.FirstSeen

Date and time of user’s first interaction with AgentX

UserInfo.LastSeen

Date and time of user’s most recent previous interaction with AgentX

UserInfo.TimeSinceLastInteraction

Number of seconds between current and previous interaction (seconds since epoch for first interaction)

UserInfo.UserName

UserName: Authenticated users only - from token in session attribute ‘accesstokenjwt’

UserInfo.GivenName

Given Name: Authenticated users only - from token in session attribute ‘accesstokenjwt’

UserInfo.FamilyName

Family Name: Authenticated users only - from token in session attribute ‘accesstokenjwt’

UserInfo.Email

Email address: Authenticated users only - from token in session attribute ‘accesstokenjwt’

UserInfo.isVerifiedIdentity

true or false: Authenticated users only - verifies if token is signed by key in jwks from trusted identity provider per AgentX setting IDENTITY_PROVIDER_JWKS_URLS

SessionAttributes.name

all session attributes are available to the handlebars context - see helpers ‘setSessionAttr’ and ‘getSessionAttr’

Slots.name

all slots filled by Lex are available to the handlebars context - see helper ‘getSlot’

Settings.name

all settings values are available to the handlebars context

Question

the users utterance, or question - translated to English if ENABLE_MULTI_LANGUAGE_SUPPORT is true

OrigQuestion

the users utterance, or question - before translation to English if ENABLE_MULTI_LANGUAGE_SUPPORT is true

PreviousQuestion

the users previous utterance, or question

Sentiment

the detected sentiment value of user’s question/utterance (POSITIVE|NEGATIVE|NEUTRAL|MIXED)

Helpers

You can use any built-in handlerbars helpers.
AgentX also provides these additional helpers:

Helper

Descr

Example

ifCond

Block helper for conditional output.
Supported comparison operators: ==, ===, != ,!==, <, <=, >, >=, &&, ||

{{#ifCond LexOrAlexa '==' 'LEX'}}
output if true
{{else}}
output if false
{{/ifCond}}

getSessionAttr

Returns named session attribute value if it is defined, or default value.

{{getSessionAttr '_attrName_' '_default_'}}

setSessionAttr

Sets a named session attribute to specified value.

{{setSessionAttr '_attrName_' '_value_'}}

getSlot

Returns named slot value if it is defined, or default value.

{{getSlot '_slotName_' '_default_'}}

randomPick

Randomly return a string selected from a list.

{{randomPick
"Greetings."
"Hi there!"
"Howdy"
"Hello, how are you?"
"Whassup dude!"
}}

signS3URL

Converts S3 URL to a signed URL with 300 sec expiration. S3 bucket name must start with QNA or qna, or policy granting bucket read access must be added to ESProxyLambdaRole.

{{signS3URL 'https://example.com/upload'}}

toUpperCase

Convert a string to upper case

{{toUpperCase 'hello, world!'}}

toLowerCase

Convert a string to lower case

{{toLowerCase 'hello, world!'}}

toTitleCase

Convert a string to title case

{{toTitleCase 'hello, world!'}}

Comments

Use the handlebars comment syntax to make your handlebars easier to understand.

{{!-- comment --}}

Examples

{{!-- respond with users name if known --}}
{{#if UserInfo.GivenName}}
   Greetings {{UserInfo.GivenName}}! 
{{else}}
   Greetings friendly human! 
{{/if}}

{{!-- Conditional welcome back message --}}
{{#ifCond UserInfo.TimeSinceLastInteraction '>' 3600}}
   You've been gone for a while. I missed you!
{{/ifCond}}
Ask me a question. Try to stump me. 

{{!-- check and set session Attribute --}}
{{#if SessionAttributes.testAttr}}
   Session Attribute "testAttr" is already set: {{SessionAttributes.testAttr}}
{{else}}
   Setting Session Attribute "testAttr" 
   {{setSessionAttr 'testAttr' 'BobRocks'}}
   Done
{{/if}}

{{!-- get Slot value, or default if slot doesn't exist or is not defined --}}
Hi {{getSlot 'firstname' 'stranger'}}!

{{!-- pick a random answer from list below --}}
{{randomPick 
"Greetings." 
"Hi there!" 
"Howdy"
"Hello, how are you?"
"Whassup dude!"
}}

{{!-- Support different for Slack answer--}}
{{!-- NOTE: AgentX automates conversion of standard markdown to Slack markdown syntax - always author in standard markdown --}}
{{#ifCond ClientType '==' 'LEX.Slack.Text'}}
   Slack Message
{{else}}
   Other client message 
{{/ifCond}}