浏览代码

Changing callback to take message and group ID, bot conversion done in app now. also renaming the util function

Kirk Trombley 6 年之前
父节点
当前提交
72fc1b115f
共有 3 个文件被更改,包括 12 次插入16 次删除
  1. 5 3
      src/app.py
  2. 6 12
      src/rollbot.py
  3. 1 1
      src/util.py

+ 5 - 3
src/app.py

@@ -8,7 +8,7 @@ import db
 from config import BOTS_LOOKUP, get_config, get_secret
 from command_system import RollbotMessage, RollbotPlugin
 from rollbot import Rollbot
-from util import post_message, find_all_subclasses
+from util import post_groupme_message, find_all_subclasses
 import plugins
 
 GLOBAL_ADMINS = get_secret("auths.global")
@@ -37,9 +37,8 @@ rollbot = Rollbot(
     plugin_classes=find_all_subclasses(RollbotPlugin),
     aliases=get_config("aliases"),
     responses=get_config("responses"),
-    lookup=BOTS_LOOKUP,
     sleep_time=float(get_config("sleep_time")),
-    callback=post_message,
+    callback=lambda msg, group_id: post_groupme_message(msg, BOTS_LOOKUP[group_id]),
     session_factory=db.session_scope
 )
 app.logger.info("Initializing database tables")
@@ -63,6 +62,9 @@ def teamspeak():
 def execute():
     json = request.get_json()
     msg = RollbotMessage.from_groupme(json, global_admins=GLOBAL_ADMINS, group_admins=GROUP_ADMINS)
+    if msg.group_id in BOTS_LOOKUP:
+        self.logger.warning(f"Received message from unknown group ID {msg.group_id}")
+        return "", 400
     t = Thread(target=lambda: rollbot.handle_command(msg))
     t.start()
     return "", 204

+ 6 - 12
src/rollbot.py

@@ -12,10 +12,10 @@ def lift_response(call, response):
 
 
 class Rollbot:
-    def __init__(self, logger=logging.getLogger(__name__), plugin_classes={}, aliases={}, responses={}, lookup={}, sleep_time=0.0, session_factory=None, callback=None):
+    def __init__(self, logger=logging.getLogger(__name__), plugin_classes={}, aliases={}, responses={}, sleep_time=0.0, session_factory=None, callback=None):
         self.logger = logger
         self.session_factory = session_factory or (lambda: None)
-        self.callback = callback or (lambda txt, gid: self.logger.info(f"Responding to {gid} with {txt}"))
+        self.post_callback = callback or (lambda txt, gid: self.logger.info(f"Responding to {gid} with {txt}"))
         self.commands = {}
         self.to_start = set()
         self.to_stop = set()
@@ -52,7 +52,6 @@ class Rollbot:
             self.commands[alias] = self.commands[cmd]
         self.logger.info(f"Finished loading aliases, {len(self.commands)} total commands + aliases available")
 
-        self.bot_lookup = lookup
         self.sleep_time = sleep_time
 
     def start_plugins(self):
@@ -94,10 +93,6 @@ class Rollbot:
             self.logger.debug("Ignoring non-command message")
             return
 
-        if message.group_id not in self.bot_lookup:
-            self.logger.warning(f"Received message from unknown group ID {message.group_id}")
-            return
-
         self.logger.info(f"Handling message {message.message_id}")
         t = time.time()
         try:
@@ -117,18 +112,17 @@ class Rollbot:
             self.logger.info(f"Sleeping for {sleep:.3f}s before responding")
             time.sleep(sleep)
 
-        bot_id = self.bot_lookup[message.group_id]
         if response.is_success:
             if response.txt is not None:
-                self.callback(response.txt, bot_id)
+                self.post_callback(response.txt, message.group_id)
             if response.img is not None:
-                self.callback(response.img, bot_id)
+                self.post_callback(response.img, message.group_id)
         else:
-            self.callback(response.failure_msg, bot_id)
+            self.post_callback(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 {message.message_id} after {t:.3f}s")
 
     def manually_post_message(self, message_text, group_id):
-        self.callback(message_text, self.bot_lookup[group_id])
+        self.post_callback(message_text, group_id)

+ 1 - 1
src/util.py

@@ -5,7 +5,7 @@ import requests
 from requests.exceptions import ConnectionError
 
 
-def post_message(msg, bot_id):
+def post_groupme_message(msg, bot_id):
     requests.post(
         "https://api.groupme.com/v3/bots/post",
         json={