Regex validation code in not accepting in AWS lexbot validation

292 views Asked by At

I used regex code for validating emails and phone numbers in lambda for validating AWS lexbot but it was throwing an error (An error has occurred: Invalid Lambda Response: Received error response from Lambda: Unhandled).

enter image description here

I had created 3 intents in this particular bot. In this 2 intents are working but one intent was not at all detecting because of this regex validation code. If I remove this regex validation code it was working fine.

This is the lambda code of intent where I am getting error.

""" --- Start greeting --- """


def validate_greeting_intent(usermood,phonenumber,email):

    UserMood = ['okay', 'better', 'too gud', 'fine', 'gud', 'happy', 'good', 'great']
    if usermood is not None and usermood.lower() not in UserMood:
        return build_validation_result(False,
                                       'UserMood',
                                       'Hey start with good greeting conversation, please'
                                       )

    if not PHONE_REGEX.match(phonenumber):

        return build_validation_result(
        False,
        'PhoneNumber',
        'Please enter valid phone number which contains 10 digits'
        )

    if not EMAIL_REGEX.match(email):

        return build_validation_result(
        False,
        'Email',
        'Please enter valid email address'
        )


    return build_validation_result(True, None, None)


def greeting_intent(intent_request):

    usermood = get_slots(intent_request)['UserMood']
    phonenumber = get_slots(intent_request)['PhoneNumber']
    email = get_slots(intent_request)['Email']
    source = intent_request['invocationSource']

    if source == 'DialogCodeHook':
        slots = get_slots(intent_request)
        validation_result = validate_greeting_intent(usermood,phonenumber,email)

        if not validation_result['isValid']:

            slots[validation_result['violatedSlot']] = None
            return elicit_slot(intent_request['sessionAttributes'],
                               intent_request['currentIntent']['name'],
                               slots,
                               validation_result['violatedSlot'],
                               validation_result['message'])

        # Pass the price of the pizza back through session attributes to be used in various prompts defined
        # on the bot model.
        output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
        if usermood is not None:
            output_session_attributes['Price'] = len(usermood) * 5  # Elegant pricing model

        return delegate(output_session_attributes, get_slots(intent_request))

    return close(intent_request['sessionAttributes'],
                 'Fulfilled',
                 {'contentType': 'PlainText',
                  'content': 'What you want to order'})


""" --- End Greeting --- """
0

There are 0 answers