Call Lex Intent From Amazon Connect Without User Input

Lex can be integrated within Amazon connect using the “Get customer input” widget. This widget plays an initial prompt. Once the user states something then intent is triggered based on the relevant utterance.

However, there might be situations where you want to trigger a specific intent without any user input probably based on some condition. Currently, when writing this, there is no such option present neither in Lex or Amazon Connect.

A quick workaround is to have an intermediate Lambda, like a proxy, between Amazon connect and Lex for the initial invocation, and then you can connect to Lex directly using the “Get customer input” widget.

The overall flow looks as in this screenshot.

Amazon Connect To Lex With Lambda Proxy

Proxy Lambda

Create a Lambda which accepts the Bot Id, Alias Id, Session Id, Locale Id, and the utterance of the intent that needs to be triggered. Within this lambda just call the Lex using these parameters. Here is the code for the sample proxy Lambda with NodeJS v18.

import { LexRuntimeV2Client, RecognizeTextCommand } from '@aws-sdk/client-lex-runtime-v2'

const lex = new LexRuntimeV2Client()

export async function handler (event) {
  console.log('event - %j', event)
  return invokeLex(event.Details.Parameters)
}

async function invokeLex (input) {
  console.log('lex:input - %j', input)

  const command = new RecognizeTextCommand(input)
  const res = await lex.send(command)
  console.log('lex:res - %j', res)

  const message = res.messages.map((msg) => msg.content).join(' ')
  const lambdaRes = { message, ...res.sessionState.sessionAttributes }
  console.log('lambda:res - %j', lambdaRes)

  return lambdaRes
}

Assign the AWS policy AmazonLexRunBotsOnly to this Lambda Role. Or you can create a more granular inline policy.

e.g.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "lex:RecognizeText"
            ],
            "Resource": "*"
        }
    ]
}

Invoke the Lambda

Invoke the above-mentioned Lambda using the “Invoke AWS Lambda function” widget with the following parameters

  • botId
  • botAliasId
  • localeId
  • sessionId
  • text

Amazon Connect To Lambda

Note: The parameter name and case should exactly be the same as these parameters are directly passed to Lex Runtime API as in the code above. Refer to this link for more details of the parameters

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-lex-runtime-v2/interfaces/recognizetextcommandinput.html

Important: Make sure to set the contact Id as the session id because in the next step when you configure the Lex, Amazon connect uses this contact id as the session id.

Amazon Connect Lambda Session Id

Invoke Lex

Invoke the Lex with the message that you received from the lambda as the initial prompt of the “Get customer input” widget.

Amazon Connect Lambda to Lex

Configure the Lex within the same “Get customer input”.

Amazon Connect to Lex

Important: If you are setting any session attributes in the lex during the lex proxy call then set them too so you won’t lose the session.

Amazon Connect Lex session params