Search code examples
pythonamazon-web-servicesaws-lambdachatbotamazon-lex

How to validate phone number and email in aws lex code hook(in lambda)


How can I validate a phone number and email in AWS Lex code hook (in Lambda).

I had tried using the following code to validate the phone number and email address in AWS Lex chatbot. I am getting errors.

import re
EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")

if len(str(phonenumber)) <= 10 or len(str(phonenumber)) >= 10:
       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'
                                       )

Solution

  • Firstly, you will want to fix some of your formatting. Following the guide here will serve you well both to improve the readability of your code for yourself and others who you want help from or who need to maintain code later on.

    Secondly, I am assuming you are omitting the vast majority of your code here, and that some of the errors in your indenting come from issues pasting to stackoverflow. I have fixed these errors, but if you are missing other important information regarding interacting with the aws api no one can help you until you post the code and ideally a full traceback of your error.

    Not everyone might agree with me on this, but unless you are an expert with regular expressions, it is generally best to copy regex made by gurus and test it thoroughly to verify it produces your desired result rather than making one yourself. The regex I am using below was copied from here. I have tested it with a long list of valid emails I have and not one of them failed to match.

    import re
    
    PHONE_REGEX = re.compile(r'[0-9]{10}')
    
    EMAIL_REGEX = re.compile(r"""(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#'$"""+
        r"""%&*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d"""+
        r"""-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*"""+
        r"""[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4]["""+
        r"""0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|["""+
        r"""a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|"""+
        r"""\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])""")
    
    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'
            )