Lucky
(Lucky)
November 20, 2020, 10:48am
1
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 '
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
jjuzl
(Joe Juzl)
November 20, 2020, 2:51pm
2
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
Lucky
(Lucky)
November 21, 2020, 4:17am
3
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}
jjuzl
(Joe Juzl)
November 23, 2020, 10:44am
4
I think the function name has to exactly match the slot name i.e. validate_<slot_name>
. So in this case validate_mobile_number
Lucky
(Lucky)
November 24, 2020, 3:42am
5
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