1

I am newbie in AWS Arena. This is my 2nd question regarding AWS Lambda function and AWS LEX. I want to write a lambda function to trigger 2 different intents based on the value of something without any user Utterance. For example

if a >= 90.....Intent-1 will work and say "Messi is the best Footballer" and if a < 90......Intent-2 will work and say "Ronaldo is the best Footballer"

3
  • No i am working with AWS LEX and AWS Lambda function. As per as i know Alexa is based on AWS LEX. Commented Feb 28, 2018 at 16:28
  • 1
    If the only difference between the intents is the response, you should merge them into one intent, and build the response in your Lambda Function, based on whatever logic you want. Changing intents just to output a different response means you are using a poorly structured intent schema. Commented Mar 1, 2018 at 1:37
  • Got your point. I will follow it. Can you suggest how can i put the value of the logic to test the lambda function + lex. Commented Mar 1, 2018 at 10:17

1 Answer 1

2

It is not supposed to work like that, intents are triggered based on what user types. For example, you can make an intent BestFootballer and it will be triggered on utterance who is the best footballer.

Now, once the intent is triggered you can apply some logic to dynamically create a response.

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

def perform_action(intent_request):
    source = intent_request['invocationSource']
    output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
    if source == 'FulfillmentCodeHook':
        a = 100
        if a < 90:
            return build_response('Ronaldo is the best Footballer')
        else:
            return build_response('Messi is the best Footballer')

def dispatch(intent_request):
    intent_name = intent_request['currentIntent']['name']
    if intent_name == 'BestFootballer':
        return perform_action(intent_request)
    raise Exception('Intent with name ' + intent_name + ' not supported')

def lambda_handler(event, context):
    return dispatch(event)

Hope it helps.

Sign up to request clarification or add additional context in comments.

4 Comments

It is great. I have got a very solid overview. But i want to taste this. So 1st i have to make an Intent, need to give an Utterance for triggering it. So without utterance triggering an Intent is not possible. Then i have to use the lambda function on Fullfillment. Based on my logic and the Iex will say who is the best footballer (answer will come from Lambda function). But my question is how can i put the value of a, to check it is working or not?
@TAMIMHAIDER initialize the value of a before evaluating it. updated the code. mark as accepted & upvote if it helped you.
Already up voted. You mean i need to initialize the value of a inside the Lambda Function? But if i put the value inside the function how can be it a dynamic one? Sorry for the troubles and lots of confusions.
for making it dynamic generally slots are used. other than that you can get user location, user information. anything. i was just giving an example. check stackoverflow.com/questions/47938140/… for details about slot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.