Complete beginner in flask, ngrok and twilio. I ran this piece of python code to create a flask app for twilio :
from flask import Flask, request, redirect
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.route("/sms", methods=['GET', 'POST'])
def incoming_sms():
"""Send a dynamic reply to an incoming text message"""
# Get the message the user sent our Twilio number
body = request.values.get('Body', None)
# Start our TwiML response
resp = MessagingResponse()
# Determine the right reply for this message
if body == 'hello':
resp.message("Hi!")
elif body == 'bye':
resp.message("Goodbye")
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
It was hosted on http://127.0.0.1:5000
I then ran "ngrok http http://127.0.0.1:5000" which create an ngrok link, but when I used it I get 404 not found. What is causing this issue and how can I fix it ?
UPDATE: http://127.0.0.1:5000/sms does not work, shows blank page
1. Run ngrok 5000
2. Here is the flask code for generating, sending and validating OTP:
@app.route('/getOTP', methods=['GET', 'POST'])
def getOTP():
mobNum = request.get_json().get("mobNum")
length = len(mobNum)
val = 0
if length == 13 :
val = getOTPApi(mobNum)
elif length == '' or length < 12:
message = "Failure"
print(message)
return message
{'ContentType':'application/json'}
if val:
message = "Success"
print(message)
return message
else:
message = "NaN"
print(message)
return message
@app.route('/validateOTP', methods=['POST'])
def validateOTP():
otp = request.get_json().get("otp")
length = len(otp)
if 'response' in session:
s = session['response']
if s == otp:
message = "Success"
print(message)
return message
elif length == 0 or length < 6:
message = "NaN"
print(message)
return message
else:
message = "Failure"
print(message)
return message
session.pop('response', None)
def generateOTP():
return random.randrange(100000, 999999)
def getOTPApi(mobNum):
account_sid = 'XXXxxXXXXXX'
auth_token = 'XXXxxXXXXXX'
client = Client(account_sid, auth_token)
otp = generateOTP()
session['response'] = str(otp)
body = 'Your OTP is ' + str(otp)
message = client.messages.create(
from_='+1123456789',
body=body,
to=mobNum
)
if message.sid:
return True
else:
return False