Operations (Sending OTP to mobile number) within submit form

Hello community, How to make some operations within the submit form. Means I need to get OTP when mobile number slot filled up. But I am not getting. OTP is getting after submitting the entire form.

def submit(self, dispatcher, tracker, domain):

    mobile_number=tracker.get_slot('mobile_number')

    OTP=otp_verification(mobile_number)

    slotvars={

        'mobile_number':mobile_number

    }

    dispatcher.utter_message(template="utter_mobile_number_verification_success" ,**slotvars)

    return [

                SlotSet("mobile_number", None),

                SlotSet("otp",None)

            ]

OTP getting code :

def otp_verification(number):

OTP= random.randint(10000,99999)

print("number----------------------------------",'+91'+ str(number))

account_sid ='xyz'

auth_token ='xyz![image_2020_11_20T10_41_05_732Z|690x256](upload://xyHY2CyYVYp7rBjJQe5ZjJi764G.png) '

client = Client(account_sid, auth_token)

message = client.messages.create(

        body='OTP is:' +str(OTP),

        from_='+12345678',

        to='+91'+ str(number)

    )

print(message.sid)

return OTP

Hi @Lucky, welcome to the forum!

Have you tried putting the validation code in a method named validate_mobile_number? That should allow you to validate it when the slot is filled, rather than when the whole form is submitted. See: Forms

Hi @jjuzl , Sorry for the late reply. Yes, I have tried validation.

Here is code for that :

def validate_mob_number(

    self,

    value: Text,

    dispatcher: CollectingDispatcher,

    tracker: Tracker,

    domain: Dict[Text, Any],

    ) -> Dict[Text, Any]:

    mobile_number1 = tracker.get_slot("mobile_number")

    # mobile_number2=mobile_number1

    # OTP=otp_verification(mobile_number2)

    

    # otp_data = tracker.get_slot("otp")

    # print('otp-------------------------------------------------------------------',otp_data)

    if len(str(mobile_number1))==10:

        return {'mobile_number':mobile_number1}    

    else:

        dispatcher.utter_message(template="utter_mobile_number_invalid")

    return {'mobile_number': None}

I think the function name has to exactly match the slot name i.e. validate_<slot_name>. So in this case validate_mobile_number

Hi @jjuzl, Yea I changed it. Needed to generate OTP in validate function and it’s working now as per my requirement.

Thanks for support

1 Like