Browse Source

Set up commands to run in a separate thread, shortened info msg to avoid groupme's limits

Kirk Trombley 6 years ago
parent
commit
49308ed11a
2 changed files with 29 additions and 44 deletions
  1. 28 20
      src/app.py
  2. 1 24
      src/plugins/simple.py

+ 28 - 20
src/app.py

@@ -1,5 +1,7 @@
 import atexit
+import time
 from logging.config import dictConfig
+from threading import Thread
 
 from flask import Flask, request, render_template
 
@@ -57,25 +59,6 @@ def services():
     return render_template("services.html", r=None)
 
 
-def run_command_and_respond(msg):
-    response = rollbot.run_command(msg)
-
-    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}")
-
-    bot_id = BOTS_LOOKUP[msg.group_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(response.failure_msg, bot_id=bot_id)
-
-
 
 @app.route("/rollbot", methods=["POST"])
 def execute():
@@ -90,7 +73,32 @@ def execute():
         app.logger.warn(f"Received message from unknown group ID {msg.group_id}")
         return "Invalid group ID", 403
 
-    run_command_and_respond(msg)
+
+    def run_command_and_respond():
+        app.logger.info(f"Entering command thread for {msg.message_id}")
+        t = time.time()
+        response = rollbot.run_command(msg)
+
+        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}")
+
+        bot_id = BOTS_LOOKUP[msg.group_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(response.failure_msg, bot_id=bot_id)
+
+        t = time.time() - t
+        app.logger.info(f"Exiting command thread for {msg.message_id} after {t:.3f}s")
+
+    t = Thread(target=run_command_and_respond)
+    t.start()
 
     return "OK", 200
 

+ 1 - 24
src/plugins/simple.py

@@ -5,32 +5,9 @@ from command_system import as_plugin, RollbotResponse
 
 HELP_MSG = """
 Hello!
-I am Rollbot 2.0 - a reimplementation of the previous Rollbot.
+I am Rollbot 3.0 - a reimplementation of the previous Rollbot.
 I am a simple chatbot built in Python.
 I am licensed as open source under the MIT license.
-I can perform some basic operations, by using one of the following commands.
-I also have "secret" functionality as well - feel free to try other commands!
-
-!info - print this message.
-
-!echo <text> - echo back the provided text.
-
-!greet - print a simple greeting.
-
-!thanks - tell me thanks!
-
-!roll <expr> - roll a set of dice, or solve a simple math problem.
-This uses the Python dice library.
-
-!teamspeak - see who is in TeamSpeak currently. Not guaranteed to work!
-Telnet is complicated!
-
-!session <action> {<arg>} - manage the next DnD session
-    next (<date>|<day>) [<time>] - set the next session's date and time
-    view - view the date and time of the next session
-    late - determine how late you are for the next session
-    cancel [<blame>] - cancel the next session, and optionally blame someone
-    worst - see who is causing the most last-minute cancellations
 """