|
@@ -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__":
|