在AWS LEX中从一个意图调用另一个意图

时间:2018-02-22 08:58:34

标签: amazon-web-services aws-lambda bots chatbot amazon-lex

我是AWS竞技场的新手。现在我正在使用AWS LEX。我想从一个意图到另一个意图。我发现了以下问题,但由于我无法发表评论,因此我创建了另一个问题。

How to call Intent B from intent A in AWS lex?

  1. 我的问题是,我将从上面的链接中放置第一种方法的代码?

  2. 如何从intent调用lambda函数,JavaScript中的代码格式是什么?

1 个答案:

答案 0 :(得分:1)

  

我的问题是我将把第一种方法的代码放在哪里   以上链接?

当调用intent-A并且您正在回复用户时,那时您将使用该代码。基本上我们使用:Inshtml而不是dialogAction类型Close 您可以阅读有关回复格式here的更多信息。

完整代码:

ConfirmIntent
  

如何从intent调用lambda函数以及代码是什么   javascript中的格式?

我没有用javascript编写任何lex机器人,也许this link可能对你有帮助。

测试事件代码:

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)

对于Fulfillment,将{ "currentIntent": { "name": "intent-A", "slots": { } }, "invocationSource": "DialogCodeHook", "sessionAttributes": {}, "bot": { "name": "Your_Bot_Name" }, "userId": "Some_User_Id" } 的值更改为invocationSource。另外,如果有任何插槽,请给出插槽。

为了澄清,FulfillmentCodeHook用于通过模拟请求来测试Lambda代码。您可以直接将Lambda函数与Lex集成,并使用Lex控制台进行测试。

希望它有所帮助。

编辑1:使用代码更新了答案 修改2:更新了测试事件代码。