Jelajahi Sumber

Adding web front end, clearing out some remaining bugs

Kirk Trombley 6 tahun lalu
induk
melakukan
4ef678cb82
8 mengubah file dengan 199 tambahan dan 13 penghapusan
  1. 5 3
      Dockerfile
  2. 32 9
      src/app.py
  3. 2 1
      src/rollbot.py
  4. 2 0
      stack-testing.yml
  5. 2 0
      stack.yml
  6. 119 0
      static/rollbot.css
  7. 29 0
      templates/services.html
  8. 8 0
      templates/teamspeak.html

+ 5 - 3
Dockerfile

@@ -1,10 +1,12 @@
 FROM python:3.7
 WORKDIR /rollbot
+ENV ROLLBOT_CFG_DIR /rollbot
+EXPOSE 6070
 ADD requirements.txt /rollbot
 RUN pip install -r requirements.txt
-ADD src/ /rollbot
+ADD static/ /rollbot/static
+ADD templates/ /rollbot/templates
 ADD config/config.toml /rollbot
 ADD config/secrets.toml /rollbot
-ENV ROLLBOT_CFG_DIR /rollbot
-EXPOSE 6070
+ADD src/ /rollbot
 CMD ["gunicorn", "rollbot:app", "--bind", "0.0.0.0:6070", "--workers", "9"]

+ 32 - 9
src/app.py

@@ -24,7 +24,6 @@ dictConfig({
     }
 })
 
-
 app = Flask(__name__)
 
 rollbot = Rollbot(app.logger)
@@ -32,6 +31,30 @@ rollbot.start_plugins()
 atexit.register(rollbot.shutdown_plugins)
 
 
+@app.route("/teamspeak", methods=["GET"])
+def teamspeak():
+    response = rollbot.run_command(RollbotMessage.from_web("!teamspeak"))
+    if response.is_success:
+        response = response.txt
+    else:
+        response = response.failure_msg
+    return render_template("teamspeak.html", r=response)
+
+
+@app.route("/services", methods=["GET", "POST"])
+def services():
+    if request.method == "POST":
+        msg = RollbotMessage.from_web(request.form["cmd"])
+        if msg.is_command:
+            response = rollbot.run_command(msg)
+            if response.is_success:
+                txt = response.txt
+                img = response.img
+            else:
+                txt = response.failure_msg
+                img = None
+            return render_template("services.html", r=response.respond, txt=txt, img=img)
+    return render_template("services.html", r=None)
 
 
 @app.route("/rollbot", methods=["POST"])
@@ -42,15 +65,15 @@ def execute():
         bot_id = BOTS_LOOKUP[msg.group_id]
         if msg.is_command:
             response = rollbot.run_command(msg)
-            if result.respond:
+            if response.respond:
                 app.logger.info(f"Responding to message {message.message_id}")
-                if result.is_success():
-                    if result.txt is not None:
-                        post_message(result.txt, bot_id=bot_id)
-                    if result.img is not None:
-                        post_message(result.img, bot_id=bot_id)
+                if response.is_success:
+                    if response.txt is not None:
+                        post_message(response.txt, bot_id=bot_id)
+                    if response.img is not None:
+                        post_message(response.img, bot_id=bot_id)
                 else:
-                    post_message(result.failure_msg, bot_id=bot_id)
+                    post_message(response.failure_msg, bot_id=bot_id)
             else:
                 app.logger.info(f"Skipping response to message {message.message_id}")
     else:
@@ -60,4 +83,4 @@ def execute():
 
 if __name__ == "__main__":
     # default deployment in debug mode
-    app.run(host="localhost", port=6071, debug=True)
+    app.run(host="0.0.0.0", port=6071, debug=True)

+ 2 - 1
src/rollbot.py

@@ -2,6 +2,7 @@ import importlib
 
 import db
 from config import PLUGINS, BOTS_LOOKUP
+from command_system import RollbotResponse, RollbotFailure
 
 class Rollbot:
     def __init__(self, logger):
@@ -40,7 +41,7 @@ class Rollbot:
 
         plugin = self.commands.get(message.command, None)
         if plugin is None:
-            response = RollbotResponse(message, failure_reason=RollbotFailure.INVALID_COMMAND)
+            response = RollbotResponse(message, failure=RollbotFailure.INVALID_COMMAND)
         else:
             with db.session_scope() as session:
                 response = plugin.on_command(session, message)

+ 2 - 0
stack-testing.yml

@@ -7,6 +7,8 @@ services:
     ports:
       - "6071:6070"
     restart: always
+    depends_on:
+      - "db"
     command: [ "python3", "app.py" ]
 
   db:

+ 2 - 0
stack.yml

@@ -7,6 +7,8 @@ services:
     ports:
       - "6071:6070"
     restart: always
+    depends_on:
+      - "db"
     networks:
       hiramnet:
         ipv4_address: "172.18.0.22"

+ 119 - 0
static/rollbot.css

@@ -0,0 +1,119 @@
+/* Taken from http://www.thenoodleincident.com/tutorials/box_lesson/basic_fixed.html */
+
+body {
+    margin: 0px 0px 0px 0px;
+    padding: 0px 0px 0px 0px;
+    font-family: verdana, arial, helvetica, sans-serif;
+    color: #ccc;
+    background-color: #333;
+    }
+a {
+    text-decoration: none;
+    font-weight: bold;
+    color:  #ccc;
+    outline: none;
+    }
+a:visited {
+    color:  #ccc;
+    }
+a:active {
+    color:  #ccc;
+    }
+a:hover {
+    color: #ccc;
+    text-decoration: underline;
+    }
+.ahem {
+    display: none;
+    }
+strong, b {
+    font-weight: bold;
+    }
+p {
+    font-size: 12px;
+    line-height: 22px;
+    margin-top: 20px;
+    margin-bottom: 10px;
+    }
+
+/* weird ie5win bug: all line-height to font-size ratios must agree or box gets pushed around. UPDATE: this has turned out to be very rare. my current recommendation is IGNORE this warning. at the moment i'm leaving it in only in case the issue turns up again. possibly the original bug in march 2001 was caused by an unusual combination of factors, although this solved it at the time.*/
+
+h1 {
+    font-size: 24px;
+    line-height: 44px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+    }
+h2 {
+    font-size: 18px;
+    line-height: 40px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+    }
+h3 {
+    font-size: 16px;
+    line-height: 22px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+    }
+h4 {
+    font-size: 14px;
+    line-height: 26px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+    }
+h5 {
+    font-size: 12px;
+    line-height: 22px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+    }
+h6 {
+    font-size: 10px;
+    line-height: 18px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+    }
+img {
+    border: 0;
+    }
+.nowrap {
+    white-space: nowrap;
+    font-size: 10px;
+    font-weight: bold;
+    margin-top: 0;
+    margin-bottom: 0;
+/* must be combined with nobr in html for ie5win */
+    }
+.tiny {
+    font-size: 9px;
+    line-height: 16px;
+    margin-top: 15px;
+    margin-bottom: 5px;
+    }
+#content {
+    float: left;
+    padding: 10px;
+    margin: 20px;
+    background: #666;
+    border: 5px solid #ccc;
+    width: 400px; /* ie5win fudge begins */
+    voice-family: "\"}\"";
+    voice-family:inherit;
+    width: 370px;
+    }
+html>body #content {
+    width: 370px; /* ie5win fudge ends */
+    }
+pre {
+    font-size: 12px;
+    line-height: 22px;
+    margin-top: 20px;
+    margin-bottom: 10px;
+    }

+ 29 - 0
templates/services.html

@@ -0,0 +1,29 @@
+<!doctype html>
+<title>Rollbot 2.0</title>
+<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='rollbot.css') }}">
+<div class="page">
+    <h1>Rollbot 2.0</h1>
+    {% if r is not none %}
+        {% if r %}
+            {% if txt is not none%}
+                {{ txt }}<br/>
+            {% endif %}
+            {% if img is not none%}
+                <img src="{{ img }}"/><br/>
+            {% endif %}
+        {% else %}
+            Operation produced no response!
+        {% endif %}
+        Try entering another command if you need help!
+    {% else %}
+        Hello! I'm Rollbot! You can enter a command below!<br/>
+        You can also elide the initial "!" if you want!
+    {% endif %}
+    <br/>
+    <br/>
+    <br/>
+    <form action="/services" method=POST>
+        <input type=text autofocus name="cmd"/><br/><br/>
+        <button type=submit>Submit</button>
+    </form>
+</div>

+ 8 - 0
templates/teamspeak.html

@@ -0,0 +1,8 @@
+<!doctype html>
+<title>Rollbot 2.0 - Teamspeak Server Status</title>
+<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='rollbot.css') }}">
+<div class="page">
+    <h1>Rollbot 2.0 - TeamSpeak Server Status</h1>
+    {{ r }}<br/>
+    <a href="{{ url_for('services') }}">Click here to return to my main front-end!</a>
+</div>