Amazon Lex not prompting for missing variables when using CodeHook Validation

前端 未结 1 1161
再見小時候
再見小時候 2021-01-24 03:29

I am building an agent in Amazon Lex with around 3 intents. All the 3 intents have a variable which has been ticked as \'required\', meaning the agent has to prompt for those va

相关标签:
1条回答
  • 2021-01-24 04:03

    This is happening because Lambda initialization and validation happens before Slot filling in Amazon Lex. You can still check that user have provided the "person" slot in DialogCodeHook i.e validation part. Something like below code will get your work done:

    def build_validation_result(is_valid, violated_slot, message_content):
        if message_content == None:
            return {
                'isValid': is_valid,
                'violatedSlot': violated_slot
            }
        return {
            'isValid': is_valid,
            'violatedSlot': violated_slot,
            'message': {'contentType': 'PlainText', 'content': message_content}
        }
    
    
    def validate_person(person):
        # apply validations here
        if person is None:
            return build_validation_result(False, 'person', 'Please enter the person name')
        return build_validation_result(True, None, None)
    
    
    def get_notes(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']
        person = slots['person']
        if source == 'DialogCodeHook':
            # check existence of "person" slot
            validation_result = validate_person(person)
            if not validation_result['isValid']:
                slots[ticket_validation_result['violatedSlot']] = None
                return elicit_slot(
                    output_session_attributes,
                    intent_request['currentIntent']['name'],
                    slots,
                    validation_result['violatedSlot'],
                    validation_result['message']
                )
            return delegate(output_session_attributes, slots)
    

    If the "person" slot is empty, user will be prompted the error message. This way you don't need to tick on "Required" on the slot. This was the only workaround I could think of when I faced this problem while using DialogCodeHook. Hope it helps.

    0 讨论(0)
提交回复
热议问题