Calling from one intent to another intent in AWS LEX
Asked Answered
E

1

8

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?

Eiland answered 22/2, 2018 at 8:58 Comment(1)
Hello @sid8491 can you please help meEiland
S
3

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.

Strontian answered 23/2, 2018 at 17:58 Comment(13)
in Configure test event which Intent detail i should put....Current Intent-A or new Intent-BEiland
@TAMIMHAIDER in configure test event you should write general sentence/utterance which will invoke intent-A. i have updated the answer with code as well. do check.Strontian
as i have understood i can use this lambda function in both of "Lambda initialization and validation" and "Fullfillment". I just need to select the name of this lambda function. Is is possible to give the sample of "configure test event". As a newbie i have lots of confusions. TIAEiland
@TAMIMHAIDER check the updated answer, Lambda initialization and validation will invoke DialogCodeHook and Fullfillment will invoke FulfillmentCodeHook. And configure test events is used to simulate a request being made to Lambda, you can directly test it using Lex that will be simpler i think.Strontian
i am getting the ConfirmIntent message from current Intent (A). But as per logic simultaneously my second Intent need to trigger. But how can i come to know , the second Intent (B) is triggered successfully?Eiland
@TAMIMHAIDER after getting confirmIntent message, you have to type yes/no, based on that intent-B will be called, to verify you can log something in intent-B or simply reply with some message from intent-B.Strontian
#47811775 read this as well for better understandingStrontian
come to the chatStrontian
Intent-B is triggered successfully but getting error on Intent-B, Added the error message and lambda function code for the newly called Intent. See the last part of your AnswerEiland
Is DialogAction possible only when using Lambda functions?Monge
@Monge what do you mean?Strontian
@Strontian In my app I also process partially-filled intents, akin to filling out a jigsaw puzzle. So I use aws-sdk and AWS.LexRuntime.postContent but there is no documentation addressing dialogAction property. Afterwards I read that this is available only for lambda users.Monge
@Monge postContent is used fr sending a message to the bot. dialogaction is used for generating a reply back from the bot, that reply can be a prompt to ask some slot or a close message. it will be defined in the webhook you will provide to the bot, it can be lambda functionStrontian

© 2022 - 2024 — McMap. All rights reserved.