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"

TAMIM HAIDER
  • 641
  • 1
  • 10
  • 19
  • No i am working with AWS LEX and AWS Lambda function. As per as i know Alexa is based on AWS LEX. – TAMIM HAIDER Feb 28 '18 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. – Jay A. Little Mar 01 '18 at 01: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. – TAMIM HAIDER Mar 01 '18 at 10:17

1 Answers1

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.

sid8491
  • 6,622
  • 6
  • 38
  • 64
  • 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? – TAMIM HAIDER Mar 01 '18 at 10:16
  • @TAMIMHAIDER initialize the value of `a` before evaluating it. updated the code. mark as accepted & upvote if it helped you. – sid8491 Mar 01 '18 at 10:27
  • 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. – TAMIM HAIDER Mar 01 '18 at 10:40
  • 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 https://stackoverflow.com/questions/47938140/trouble-with-amazon-lex-chatbot-slots for details about slot. – sid8491 Mar 01 '18 at 11:08