Trying to create a whatsapp bot on Twilio that limits the number of requests a user can make within a 24 hour period.
However, when I send through a request I get this error message on ngrok
File "C:\Users\User\Documents\GitHub\gradientboostwhatsapp\venv\lib\site-packages\flask\app.py", line 2097, in make_response"The view function did not return a valid response. The"
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
This is what I see on my twilio console:
MESSAGE
Internal Server Error
and on my terminal:
File "C:\Users\User\Documents\GitHub\gradientboostwhatsapp\venv\lib\site-packages\flask\app.py", line 2097, in make_response"The view function did not return a valid response. The"
Here is the code I wrote
from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse
import random
from pathlib import Path
from twilio.rest import Client
from datetime import datetime
import pytz
import reapp = Flask(__name__)
#this will store information about user session such as the time of user's first request and request counter
sessionStorage = {}
#addng user to session storage with current time and setting request counter to 0
time = datetime.now(pytz.timezone('Africa/Harare'))
counter = 0
def add_user(user):sessionStorage[user] = {}#time when first session startssessionStorage[user][time] = datetime.now(pytz.timezone('Africa/Harare'))sessionStorage[user][counter] = 0
#checking if user can perform a request and updating time if required
def request_check(user):difference = sessionStorage[user][time] - datetime.now(pytz.timezone('Africa/Harare'))#check if it has been 24 hours after first request, if so then reset request counter and set last request time to current timesessionStorage[user][counter] = 0sessionStorage[user][time] = datetime.now(pytz.timezone('Africa/Harare'))#if user requests exceed 5 then do not allow any more requestsif sessionStorage[user][counter] > 5:return False#in other cases allow user to continue requestingreturn True
#function to increment request counter for current user
def increment_request_counter(user):sessionStorage[user][counter] +=1@app.route('/bot', methods=['POST'])
def bot():incoming_msg = request.values.get('Body', '').lower()resp = MessagingResponse()#extract phone number from ngroknumber = request.values.get('From', '')#remove non numerical valuescleaned_number = re.sub('[^0-9]', '', number)msg = resp.message()#create new useradd_user(user=cleaned_number)responded = Falseif request_check(user=cleaned_number):if incoming_msg == 'help':output = 'Introduction text.'msg.body(output)responded = Trueif 'testing' in incoming_msg:msg.body(cleaned_number)responded = Trueif 'a' in incoming_msg:pre = 'More textmsg.body(pre)responded = Trueif 'b' in incoming_msg:pre = 'Test text'msg.body(pre)responded = Trueif 'c' in incoming_msg:pre = 'I do not currently have any stats challanges, but will be adding a few soon.'msg.body(pre)responded = Trueif not responded:msg.body('Test message.')return str(resp)increment_request_counter(user=cleaned_number)if __name__ == '__main__':app.run(debug=True)
The problem started when I tried to add the logic to limit the number of requests a user can make within a 24 hour period