Amazon Lex not prompting for missing variables when using CodeHook Validation

梦想的初衷 提交于 2019-12-02 06:53:23

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!