Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created August 7, 2024 09:03
Show Gist options
  • Save ehzawad/1f6aefeeb92483fbe9d6797133b2b7ad to your computer and use it in GitHub Desktop.
Save ehzawad/1f6aefeeb92483fbe9d6797133b2b7ad to your computer and use it in GitHub Desktop.
OTP-sending
# OTP Implementation Flow and Code
## 1. Generate OTP
When a sensitive operation is requested, a new OTP is generated.
```python
def opt_generator(self):
opt_number = random.randint(self.otp_srange, self.opt_erange)
return opt_number
g_otp = otp.opt_generator()
```
## 2. Store OTP
The generated OTP is stored along with the client identifier (CLI) and timestamp.
```python
def store_using_cli(self, cli, otp, start_time):
if otp not in self.data:
self.data[cli] = {"otp": otp, "stime": start_time}
stime = time.time()
otp.store_using_cli(cli, g_otp, stime)
```
## 3. Send OTP to User
The system informs the user that an OTP has been sent to their mobile number.
```python
dispatcher.utter_message(text = f"আপনার মোবাইল নাম্বার এ, সিক্স ডিজিট ওটিপি এস,এম,এস এর মাধ্যমে পাঠানো হয়েছে, দয়া করে ওটিপি দুই মিনিট আর মধ্যে টাইপ করুন, আমরা, আপনার জন্য অপেক্ষা করতেছি")
```
## 4. User Inputs OTP
The system extracts the numerical value from the user's input.
```python
filterd_opt_extraction = hp_obj.get_numerical_value(last_text, limit=4)
```
## 5. Validate OTP
The input OTP is checked against the stored OTP.
```python
if filterd_opt_extraction[0] == str(opt_data[cli]["otp"]):
# OTP is correct
return {
"credit_card_otp_number": filterd_opt_extraction[0],
"process_continue_status": True,
"process_state": "complete",
"process_interruption": 0
}
elif filterd_opt_extraction[0] != str(opt_data[cli]["otp"]):
# OTP is incorrect
dispatcher.utter_message(text = f"দুঃখিত, আপনার প্রবেশকৃত ওটিপি সঠিক নয়, অনুগ্রহপূর্বক পুনরায় ওটিপি প্রবেশ করান")
return {
"credit_card_otp_number": None,
"requested_slot": "credit_card_otp_number",
"process_state": "repeated",
"process_continue_status": False,
"process_interruption": process_interruption
}
```
## 6. Check OTP Expiration
The system checks if the OTP has expired based on the timestamp.
```python
def check_status_cli_opt(self, cli, end_time):
status = False
if cli in self.data:
stime = self.data[cli]["stime"]
if end_time-stime > self.sleep_time:
status = True
return status
timeout_status = otp.check_status_cli_opt(cli, endtime)
if timeout_status:
# OTP has expired, generate a new one
stime = time.time()
g_otp = otp.opt_generator()
otp.remove_cli(cli)
otp.store_using_cli(cli, g_otp, stime)
dispatcher.utter_message(text = f"আপনার ওটিপি সাবমিট এর সময় শেষ, আবার ওটিপি সাবমিট করেন")
```
## 7. Handle Repeated Attempts
The system tracks the number of failed attempts and transfers to an agent if the limit is exceeded.
```python
process_interruption += 1
if process_interruption > cfg.PREPETED_TIME:
dispatcher.utter_message(text = response_db["agent_transfer"]["text"])
return cfg.CREDIT_CARD_RESET
```
## 8. Handle OTP Not Received
If the user indicates they didn't receive the OTP, a new one is generated and sent.
```python
not_get_status = hp_obj.opt_not_get_status(last_text)
if not_get_status == True:
dispatcher.utter_message(text = f"আপনার মোবাইল নাম্বার এ, সিক্স ডিজিট ওটিপি এস,এম,এস এর মাধ্যমে পাঠানো হয়েছে, দয়া করে ওটিপি দুই মিনিট আর মধ্যে টাইপ করুন, আমরা, আপনার জন্য অপেক্ষা করতেছি")
stime = time.time()
callid, cli = hp_obj.get_cli_and_callid(sender_id)
g_otp = otp.opt_generator()
otp.store_using_cli(cli, g_otp, stime)
```
## 9. Complete Requested Action
If the OTP is correctly validated, the system proceeds with the requested action (e.g., card activation).
```python
# This would be part of the larger conversation flow, not directly in the OTP validation code
if credit_card_otp_number_valid:
# Proceed with card activation or other sensitive operation
perform_requested_action()
```
This structured flow provides a comprehensive view of the OTP implementation, from generation to final action completion, along with the corresponding code snippets for each step.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment