I made this simple chat bot in Python and Flask. It works perfectly when I run it in a local server but I uploaded it to Heroku (my first time uploading) and some options only work sometimes.
In the above image, entering 3 should prompt the app to ask user for a city name but it doesn't most of the times.
#imports
import requests
from flask import Flask, render_template, request
app = Flask(__name__)
import random
newsApiKey = 'c0f976e7caac4b608d84c4546e0b892c'
isGreeted = False
isThree = False
def getRequest(location):
global newsApiKey
url = "http://newsapi.org/v2/everything?q={} AND +corona AND english&qInTitle=+corona&language=en&from=2020-03-25&sortBy=relevancy&apiKey={}".format(location,newsApiKey)
r = requests.get(url)
return r
def getGreeting():
greets = ['Hello there General Kenobi!', 'Hi there!', 'Hi, how are you?']
return random.choice(greets)
#define app routes
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get")
#function for the bot response
def get_bot_response():
global isGreeted
global isThree
userText = request.args.get('msg')
# return str(englishBot.get_response(userText))
responseMessage = ""
if userText in ['Hi!', 'Hello!', 'Hi'] and isGreeted == False:
responseMessage = getGreeting() + "\n\nChoose an option from below menu:\n\n1 - What is Covid-19?\n\n2 - What are the symptoms of Covid-19?\n\n3 - Local news about Covid-19\n\n4 - What should I do?"
isGreeted = True
elif userText in ['1','2','3','4'] and isGreeted:
if userText == '1':
responseMessage = "Coronavirus disease (COVID-19) is an infectious disease caused by a newly discovered coronavirus."
elif userText == '2':
responseMessage = "Common symptoms include fever, tiredness, dry cough. Other symptoms include shortness of breath, aches and pains, sore throat, diarrhoea, runny nose. People with mild symptoms should try self isolating and others should seek medical attention."
elif userText == '3' and isThree == False:
responseMessage = 'Enter your city name'
isThree = True
elif userText == '4':
responseMessage = 'Stay at home. If you plan to go outside, avoid using public transportation. Seek medical attention if you have trouble breathing, pain in chest, bluish lips. Cover your face with an N95 mask. Clean your hands often.'
elif isThree:
r = getRequest(userText).json()
# print(r)
isThree = False
counter = 0
for article in r['articles']:
if counter != 3:
responseMessage += article['title'] + '\n' + article['description'] + '\n\n'
counter = counter + 1
else:
break
return responseMessage
if __name__ == "__main__":
app.run()
Does it have anything to do with the way I handle responses? Or the fact I am using global variables? If yes, what is the better way to do this? Thank you in advance.