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

TAM*_*DER 5 bots chatbot amazon-web-services aws-lambda amazon-lex

我是AWS领域的新手。现在,我正在研究AWS LEX。我想从一种意图调用到另一种意图。我发现了以下问题,但由于无法发表评论,我创建了另一个问题。

如何在AWS lex中从意图A调用意图B?

  1. 我的问题是从上面的链接将第一种方法的代码放在哪里?

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

sid*_*491 3

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

当意图 A 被调用并且您正在响应用户时,此时您将使用该代码。基本上我们使用的不是dialogAction类型。 您可以在此处阅读有关响应格式的更多信息。CloseConfirmIntent

完整代码:

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)
Run Code Online (Sandbox Code Playgroud)

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

我没有用 javascript 编写任何 lex 机器人,也许这个链接可能对您有帮助。

测试事件代码:

{
  "currentIntent": {
    "name": "intent-A",
    "slots": {
    }
  },
  "invocationSource": "DialogCodeHook",
  "sessionAttributes": {},
  "bot": {
    "name": "Your_Bot_Name"
  },
  "userId": "Some_User_Id"
}
Run Code Online (Sandbox Code Playgroud)

对于 Fulfillment,将 的值更改invocationSourceFulfillmentCodeHook。另外,如果有的话,请提供插槽。

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

希望能帮助到你。

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