OTP verification using Python
Introduction:
In this project, we have made an OTP verification System with Help of various libraries. First of all, we made use of Tkinter for creating the GUI for our project. Next to that, to generate the random Numbers as OTP we used a random module. At last, forgetting and checking the OTP we used an API Twilio.
A random number will be sent to the stated mobile number with the help of the API and then our project will check whether the OTP is valid or not, and thus this will build an OTP Verification Project using Python.
Explanation:
The main objectives that need to be fulfilled while making this projects are:
Creating a GUI for this project
Generating a random number as OTP
Sending the OTP to the stated Mobile number
Checking whether the OTP is valid or not
Resending the OTP
We will work on these objectives step by step to complete this project.
First of all, creating a GUI for our project will make use of the “tkinter” module, from “future. moves” we will import the module. The most major step is to create the instance of the tkinter frame i.e. tk(). This will help to display the window and manage all the components of the tkinter application. In addition to this, with the help of “.title()” & “.geometry()” we will set the title and dimensions for the window. With this, we will also draw the Canvas for OTP Verification with the help of “.canvas()” and by using “.place()” we will provide the dimensions as parameters for placing our canvas.
Source code 👇👇 # Importing the libraries
import
twilio.rest
import random
from future.moves import tkinter
from tkinter import messagebox
# Creating Window
root = tkinter.Tk()
root.title("OTP Verification")
root.geometry("600x550")
# Twilio account details
account_sid = ""
auth_token = ""
# resend the OTP
def resendOTP():
n = random.randint(1000, 9999)
client = twilio.rest.Client(account_sid, auth_token)
client.messages.create(to=[""], from_=" ", body=n)
# Checking the OTP
def checkOTP():
global n
try:
user_input = int(user.get(1.0, "end01c"))
if user_input == n:
messagebox.showinfo("showinfo", "Login Success")
n = "done"
elif n == "done":
messagebox.showinfo("showinfo", "Already entered")
else:
messagebox.showinfo("showinfo", "wrong OTP")
except:
messagebox.showinfo("showinfo", "Invalid OTP")
# Drawing the canvas
c = tkinter.Canvas(root, bg="white", width=400, height=300)
c.place(x=100, y=60)
# Label widget
login = tkinter.Label(root, text="OTP Verification", font="bold,20", bg="white")
login.place(x=210, y=90)
# Entry widget
user = tkinter.Text(root, borderwidth=2, wrap="word", width=29, height=2)
user.place(x=190, y=160)
# Sending the otp
n = random.randint(1000, 9999)
client = twilio.rest.Client(account_sid, auth_token)
client.messages.create(to=[""], from_="", body=n)
# Submit button
submit_button = tkinter.Button(root, text="Submit", command=checkOTP(), font=('bold', 15))
submit_button.place(x=258, y=250)
# Resend Button
resend_button = tkinter.Button(root, text="Resend OTP", command=resendOTP(), font=("bold", 15))
resend_button.place(x=240, y=400)
# Event Loop
root.mainloop()