为AWS LEX编写条件AWS Lambda函数

问题描述 投票:1回答:1

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

如果a = = 90 ...... Intent-1将起作用并说“梅西是最好的足球运动员”,如果<90 ......意图-2将起作用并说“罗纳尔多是最好的足球运动员”

javascript python amazon-web-services aws-lambda amazon-lex
1个回答
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)

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.