Browse Source

Moving threaded logic into rollbot itself, removing services endpoint

Kirk Trombley 6 years ago
parent
commit
09470c3e71
4 changed files with 46 additions and 93 deletions
  1. 1 0
      config/config.toml
  2. 8 62
      src/app.py
  3. 37 2
      src/rollbot.py
  4. 0 29
      src/templates/services.html

+ 1 - 0
config/config.toml

@@ -36,3 +36,4 @@ bump = "Bumping the chat!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\
 [teamspeak]
 host = "kirkleon.ddns.net"
 user = "serveradmin"
+scrolling = true

+ 8 - 62
src/app.py

@@ -3,9 +3,9 @@ import time
 from logging.config import dictConfig
 from threading import Thread
 
-from flask import Flask, request, render_template
+from flask import Flask, request, render_template, jsonify
 
-from config import BOTS_LOOKUP, SLEEP_TIME
+from config import BOTS_LOOKUP, SLEEP_TIME, get_config
 from command_system import RollbotMessage, RollbotResponse, RollbotFailure
 from rollbot import Rollbot
 from util import post_message
@@ -40,24 +40,7 @@ def teamspeak():
         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)
-
+    return render_template("teamspeak.html", r=response, scrolling=get_config("teamspeak.scrolling"))
 
 
 @app.route("/rollbot", methods=["POST"])
@@ -65,52 +48,15 @@ def execute():
     json = request.get_json()
     msg = RollbotMessage.from_groupme(json)
 
-    if not msg.is_command:
-        app.logger.debug("Received non-command message")
-        return "", 204
-
     if msg.group_id not in BOTS_LOOKUP:
         app.logger.warning(f"Received message from unknown group ID {msg.group_id}")
-        return "Invalid group ID", 403
-
-
-    def run_command_and_respond():
-        app.logger.info(f"Entering command thread for {msg.message_id}")
-        t = time.time()
-        try:
-            response = rollbot.run_command(msg)
-        except Exception as e:
-            app.logger.error(f"Exception during command execution {e}, for message {msg.message_id}")
-            response = RollbotResponse(msg, failure=RollbotFailure.INTERNAL_ERROR)
-
-        if not response.respond:
-            app.logger.info(f"Skipping response to message {msg.message_id}")
-            return
-
-        app.logger.info(f"Responding to message {msg.message_id}")
-
-        sleep = SLEEP_TIME - time.time() + t
-        if sleep > 0:
-            app.logger.info(f"Sleeping for {sleep:.3f}s before responding")
-            time.sleep(sleep)
-
-        bot_id = BOTS_LOOKUP[msg.group_id]
-        if response.is_success:
-            if response.txt is not None:
-                post_message(response.txt, bot_id)
-            if response.img is not None:
-                post_message(response.img, bot_id)
-        else:
-            post_message(response.failure_msg, bot_id)
-            app.logger.warning(f"Failed command response: {response}")
-
-        t = time.time() - t
-        app.logger.info(f"Exiting command thread for {msg.message_id} after {t:.3f}s")
+        return jsonify({"message": "Invalid group ID"}), 403
 
-    t = Thread(target=run_command_and_respond)
-    t.start()
+    if msg.is_command:
+        t = Thread(target=lambda: rollbot.handle_message(msg))
+        t.start()
 
-    return "OK", 200
+    return "", 204
 
 
 if __name__ == "__main__":

+ 37 - 2
src/rollbot.py

@@ -1,8 +1,10 @@
 import importlib
+import time
 
 import db
-from config import PLUGINS, ALIASES, RESPONSES, BOTS_LOOKUP
+from config import PLUGINS, ALIASES, RESPONSES, BOTS_LOOKUP, SLEEP_TIME
 from command_system import RollbotResponse, RollbotFailure
+from util import post_message
 
 
 class Rollbot:
@@ -41,7 +43,7 @@ class Rollbot:
     def run_command(self, message):
         if not message.is_command:
             self.logger.warn(f"Tried to run non-command message {message.message_id}")
-            return
+            return RollbotResponse(msg, failure=RollbotFailure.INTERNAL_ERROR)
 
         # if this command is aliased, resolve that first, otherwise use the literal command
         cmd = ALIASES.get(message.command, message.command)
@@ -63,3 +65,36 @@ class Rollbot:
             self.logger.warn(response.info)
 
         return response
+
+    def handle_message(self, msg):
+        self.logger.info(f"Handling message {msg.message_id}")
+        t = time.time()
+        try:
+            response = self.run_command(msg)
+        except Exception as e:
+            self.logger.error(f"Exception during command execution {e}, for message {msg.message_id}")
+            response = RollbotResponse(msg, failure=RollbotFailure.INTERNAL_ERROR)
+
+        if not response.respond:
+            self.logger.info(f"Skipping response to message {msg.message_id}")
+            return
+
+        self.logger.info(f"Responding to message {msg.message_id}")
+
+        sleep = SLEEP_TIME - time.time() + t
+        if sleep > 0:
+            self.logger.info(f"Sleeping for {sleep:.3f}s before responding")
+            time.sleep(sleep)
+
+        bot_id = BOTS_LOOKUP[msg.group_id]
+        if response.is_success:
+            if response.txt is not None:
+                post_message(response.txt, bot_id)
+            if response.img is not None:
+                post_message(response.img, bot_id)
+        else:
+            post_message(response.failure_msg, bot_id)
+            self.logger.warning(f"Failed command response: {response}")
+
+        t = time.time() - t
+        self.logger.info(f"Exiting command thread for {msg.message_id} after {t:.3f}s")

+ 0 - 29
src/templates/services.html

@@ -1,29 +0,0 @@
-<!doctype html>
-<title>Rollbot 3.0</title>
-<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='rollbot.css') }}">
-<div class="page">
-    <h1>Rollbot 3.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>