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

时间:2018-01-08 09:45:24

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

我正在研究aws lex 我有意图 - A。我把它命名为welcomeMsg。我想从intent(B)拨打另一个intent-A。欢迎msg(intent-A),它会说:

> `"Hi, I am a xxx-BOT. i can help you with following:`
       A
       B
       C

如果我说B,它应该去意图-B。这是我想做的,但我无法做到这一点。任何python代码的帮助将不胜感激。

1 个答案:

答案 0 :(得分:8)

我找到了这三种从intent-A调用intent-B的方法。

第一种方法(使用ConfirmIntent):

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
            }
        }
    }
msg = "Hi, I am a xxx-BOT. i can help you with following: A B C"

return confirm_intent(output_session_attributes, 'intent-B', new_slot, msg)

第二种方法(假装是Lex并调用Lambda方法):

client = boto3.client('lambda')
data = {'messageVersion': '1.0', 'invocationSource': 'FulfillmentCodeHook', 'userId': '###', 
        'sessionAttributes': {}, 'requestAttributes': None, 
        'bot': {'name': '###', 'alias': '$LATEST', 'version': '$LATEST'}, 
        'outputDialogMode': 'Text', 
        'currentIntent': {'name': '###', 'slots': {'###': '###'}, 
        'slotDetails': {'###': {'resolutions': [], 'originalValue': '###'}}, 
        'confirmationStatus': 'None'}, 
        'inputTranscript': '###'}
response = client.invoke(
    FunctionName='{intent-B_lambda_function}',
    InvocationType='RequestResponse',
    Payload=json.dumps(data)
)
output = json.loads(response['Payload'].read())['dialogAction']['message']['content']

第三种方法(使用ElicitSlot):

def elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message):   
    return {
        sessionAttributes,
        dialogAction: {
            type: 'ElicitSlot',
            intentName,
            slots,
            slotToElicit,
            message,
        }
    }

intentRequest['currentIntent']['name'] = 'intent-B'
param1 = {
    slot-B:null
    }
intentRequest['currentIntent']['slots'] = param1
return elicitSlot(outputSessionAttributes, 'intent-B', intentRequest['currentIntent']['slots'], 'slot-B', 'some_message')

请检查这些方法,使用它们并根据需要进行调整。我认为方法1 最适合您的需求,而且最简单。

如果您遇到一些问题,请注意。

希望它有所帮助。