diff --git a/app/app.py b/app/app.py index 2bd6fad5e4299b859fef14341a9f3ef322277c95..c9cf6a5f3e461ef7358f8e68d201b3fb8b9d52cc 100644 --- a/app/app.py +++ b/app/app.py @@ -14,9 +14,14 @@ def home(): def sendmessage(): message = request.form['message'] print(message) - rasa_response = requests.post('http://localhost:5005/webhooks/rest/webhook', json={"message": message}) - responses = [] + try: + rasa_response = requests.post('http://localhost:5005/webhooks/rest/webhook', json={"message": message}) + except Exception as e: + print(f"error: {e}") + responses.append({"type": "text", "content": "Oh, it appears that I'm not availabe. Please try again later!"}) + return jsonify(responses) + for resp in rasa_response.json(): if "text" in resp: responses.append({"type": "text", "content": resp["text"]}) diff --git a/app/static/javascript/frontend.js b/app/static/javascript/frontend.js index 7f95fb756243f5ca0265dd2346f567ff907a8d50..4e38deacc371098e37bb06914dd893fd51e51f53 100644 --- a/app/static/javascript/frontend.js +++ b/app/static/javascript/frontend.js @@ -34,6 +34,17 @@ function sendMessage() { chat.insertBefore(newDiv, chat.firstChild); // document.getElementById("chat").appendChild(newDiv); + }) + .catch((error) => { + console.error(error); + let newDiv = document.createElement("div"); + newDiv.className = "message-bot"; + + let newP = document.createElement("p"); + newP.textContent = "Oh, it appears that I'm not availabe. Please check your internet connection!"; + newDiv.appendChild(newP); + let chat = document.getElementById("chat"); + chat.insertBefore(newDiv, chat.firstChild); }); } };