为AWS LEX编写条件AWS Lambda函数

时间:2018-02-28 16:00:41

标签: javascript python amazon-web-services aws-lambda amazon-lex

我是AWS Arena的新手。这是关于AWS Lambda函数和AWS LEX的第二个问题。我想写一个lambda函数,根据没有任何用户话语的东西的值触发2个不同的意图。例如

如果一个> = 90 .....意图-1会起作用并且说'"梅西是最好的足球运动员"和 如果a< 90 ......意图2将起作用并且说'#34;罗纳尔多是最好的足球运动员"

1 个答案:

答案 0 :(得分:2)

它不应该像那样工作,根据用户类型触发意图。例如,您可以制作一个意图BestFootballer,它将在话语who is the best footballer上触发。

现在,一旦触发意图,您可以应用一些逻辑来动态创建响应。

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)

希望它有所帮助。