Mobile OTP verification with Python Flask with the help of Twilio
app.py
from flask import *
from twilio.rest import Client
import random
app = Flask(__name__, template_folder='template')
otp=random.randrange(000000,999999)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/verify', methods=['POST'])
def getotp():
number = request.form['number']
val = otpapi(number)
if val:
return render_template('verify.html')
def otpapi(number):
account_sid = "******************"
account_token = "****************"
client = Client(account_sid,account_token)
body = "your otp is" + str(otp)
message = client.messages.create(
body=body,
from_ = '+19285698961',
to = number
)
if message.sid:
return True
else:
False
@app.route('/success', methods=['POST'])
def validate():
user_otp=request.form['otp']
if otp==int(user_otp):
return "<h3>Mobile OTP varification succesfull</h3>"
return "<h3>Please Try Again</h3>"
if __name__ == '__main__':
app.run(debug=True)
"""get account_sid and account_token from twilio account"""
templates folder
index.html:
<!DOCTYPE html>
<html>
<head>
<title> Dinakarn </title>
</head>
<body>
<form action = "/verify" method = "post">
<div>
<label> Enter Mobile NUmber</label></br>
<input type = "text" name = "number"/>
<button type = "submit"> Get Otp </button>
</div>
</form>
</body>
</html>
verify.html
<html>
<head>
<title>Dinakaran</title>
</head>
<body>
<form action="/success" method = "post">
<div>
<label> Enter OTP</label></br>
<input type="text" name="otp"/>
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>
emotp.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dinakaran</title>
</head>
<body>
<p> OTP Verification is successful</p>
</body>
</html>
common file/folder structure in flask app:
/yourapp
/run.py
/config.py
/app
/__init__.py
/views.py
/models.py
/static/
/main.css
/templates/
/base.html
/requirements.txt
/yourappenv
Comments
Post a Comment