From 4bb5953315f8cf931f0a775134ee471004781f41 Mon Sep 17 00:00:00 2001
From: Dominic Kraemer
 <Dominic_Daniel.Kraemer@Student.Reutlingen-University.de>
Date: Thu, 20 Jun 2024 13:37:10 +0200
Subject: [PATCH] add basic integration into html

---
 app/app.py                        | 20 ++++++++++++++++++++
 app/static/javascript/frontend.js | 15 +++++++++++++++
 app/templates/index.html          |  5 +++++
 rasa/endpoints.yml                |  4 ++--
 4 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 app/static/javascript/frontend.js

diff --git a/app/app.py b/app/app.py
index 1333743..2bd6fad 100644
--- a/app/app.py
+++ b/app/app.py
@@ -1,5 +1,8 @@
 from flask import Flask
 from flask import render_template
+from flask import request
+from flask import jsonify
+import requests
 
 app = Flask(__name__)
 
@@ -7,5 +10,22 @@ app = Flask(__name__)
 def home():
     return render_template("index.html")
 
+@app.route('/sendmessage', methods=['POST'])
+def sendmessage():
+    message = request.form['message']
+    print(message)
+    rasa_response = requests.post('http://localhost:5005/webhooks/rest/webhook', json={"message": message})
+
+    responses = []
+    for resp in rasa_response.json():
+        if "text" in resp:
+            responses.append({"type": "text", "content": resp["text"]})
+        if "image" in resp:
+            responses.append({"type": "image", "content": resp["image"]})
+        if "buttons" in resp:
+            responses.append({"type": "buttons", "content": resp["buttons"]})
+        
+        return jsonify(responses)
+
 if __name__ == '__main__':
     app.run(debug=True)
diff --git a/app/static/javascript/frontend.js b/app/static/javascript/frontend.js
new file mode 100644
index 0000000..3a11f09
--- /dev/null
+++ b/app/static/javascript/frontend.js
@@ -0,0 +1,15 @@
+function sendMessage() {
+    message = document.getElementById("inputField").value;
+    message = "message=" + message;
+    fetch("http://localhost:5000/sendmessage", {
+        method: "POST",
+        headers: {
+            "Content-Type": "application/x-www-form-urlencoded"
+        },
+        body: message
+    })
+    .then((response) => response.json())
+    .then((json) => {
+        console.log(json[0].content);
+    });
+};
\ No newline at end of file
diff --git a/app/templates/index.html b/app/templates/index.html
index 91f0930..19301a4 100644
--- a/app/templates/index.html
+++ b/app/templates/index.html
@@ -8,5 +8,10 @@
 
     <body>
         <h1>Welcome to the Space Station Chatbot!</h1>
+        <div>
+            <input type="text" id="inputField" placeholder="Gib etwas ein">
+            <button onclick="sendMessage()">CLICK ME</button>
+        </div>
+        <script src="static/javascript/frontend.js" type="application/javascript"></script>
     </body>
 </html>
\ No newline at end of file
diff --git a/rasa/endpoints.yml b/rasa/endpoints.yml
index 1128e1d..5f65275 100644
--- a/rasa/endpoints.yml
+++ b/rasa/endpoints.yml
@@ -10,8 +10,8 @@
 # Server which runs your custom actions.
 # https://rasa.com/docs/rasa/custom-actions
 
-#action_endpoint:
-#  url: "http://localhost:5055/webhook"
+action_endpoint:
+  url: "http://localhost:5055/webhook"
 
 # Tracker store which is used to store the conversations.
 # By default the conversations are stored in memory.
-- 
GitLab