Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling from one intent to another intent in AWS LEX

I am a newbie in the AWS arena. Now I am working on AWS LEX. I want to call from one intent to another intent. I have found the following question but as I cannot comment I have created another one.

How to call Intent B from intent A in AWS lex?

  1. My question is where will I put the codes of the 1st method from the above link?

  2. How to call a lambda function from a intent and what is the code format in JavaScript?

like image 250
TAMIM HAIDER Avatar asked Feb 22 '18 08:02

TAMIM HAIDER


1 Answers

My question is where will i put the codes of the 1st method from the above link?

When the intent-A is called and you are responding back to user, at that time you will use that code. Basically instead of dialogAction type Close we are using ConfirmIntent.
You can read more about response format here.

Complete code:

def build_response(message):
    return {
        "dialogAction":{
            "type":"Close",
            "fulfillmentState":"Fulfilled",
            "message":{
                "contentType":"PlainText",
                "content":message
            }
        }
    }

def delegate(session_attributes, slots):
    return {
        'sessionAttributes': session_attributes,
        'dialogAction': {
            'type': 'Delegate',
            'slots': slots
        }
    }

def confirm_intent(session_attributes, intent_name, slots, message):
    return {
        'sessionAttributes': session_attributes,
        'dialogAction': {
            'type': 'ConfirmIntent',
            'intentName': intent_name,
            'slots': slots,
            'message': {
                'contentType': 'PlainText',
                'content': message
            }
        }
    }

def perform_action_A(intent_request):
    source = intent_request['invocationSource']
    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
    slots = intent_request['currentIntent']['slots']
    # whatever you want to do
    if source == 'DialogCodeHook':
        # Perform basic validation on the supplied input slots.
        return delegate(output_session_attributes, slots)

    if source == 'FulfillmentCodeHook':
        # action fulfillment code
        msg = "Hi, I am a xxx-BOT. i can help you with following: A B C"
        return confirm_intent(output_session_attributes, 'intent-B', slots, msg)


def perform_action_B(intent_request):
    # some code
    if source == 'DialogCodeHook':
        # Perform basic validation on the supplied input slots.
        return delegate(output_session_attributes, slots)
    if source == 'FulfillmentCodeHook':
        # action fulfillment code
        build_response('Final close message')


def dispatch(intent_request):
    intent_name = intent_request['currentIntent']['name']
    # Dispatch to your bot's intent handlers
    if intent_name == 'intent-A':
        return perform_action_A(intent_request)
    if intent_name == 'intent-B':
        return perform_action_B(intent_request)
    raise Exception('Intent with name ' + intent_name + ' not supported')


def lambda_handler(event, context):
    logger.debug(event)
    return dispatch(event)

How to call a lambda function from a intent and what is the code format in javascript?

I have not coded any lex bot in javascript, maybe this link might help you.

Test Event Code:

{
  "currentIntent": {
    "name": "intent-A",
    "slots": {
    }
  },
  "invocationSource": "DialogCodeHook",
  "sessionAttributes": {},
  "bot": {
    "name": "Your_Bot_Name"
  },
  "userId": "Some_User_Id"
}

For Fulfillment change the value of invocationSource to FulfillmentCodeHook. Also, give the slots if there are any.

Just to clarify, configure test events is used for testing the Lambda code by simulating a request. You can directly integrate the Lambda function with Lex and test using Lex console.

Hope it helps.

Edit 1: Updated the answer with code.
Edit 2: Updated test events code.

like image 152
sid8491 Avatar answered Oct 16 '22 14:10

sid8491