From 3613ae8138f4274cdf45a6dd976bb0230083802f Mon Sep 17 00:00:00 2001 From: Colin Chung Date: Thu, 4 Apr 2024 16:44:26 -0700 Subject: [PATCH] [INTPROD-9204] Allow Match By Bot User ID --- omnibot/services/slack/__init__.py | 23 +++++++++++++++++++++++ omnibot/services/slack/bot.py | 10 ++++++++++ omnibot/services/slack/message.py | 2 ++ 3 files changed, 35 insertions(+) diff --git a/omnibot/services/slack/__init__.py b/omnibot/services/slack/__init__.py index b6b13c2..e5efa38 100644 --- a/omnibot/services/slack/__init__.py +++ b/omnibot/services/slack/__init__.py @@ -465,6 +465,29 @@ def get_user(bot, user_id): ), ) return {} + + +def get_auth(bot): + """ + Get the auth info for the bot. + """ + redis_client = omniredis.get_redis_client() + auth_info = redis_client.hget(f"auth:{bot.team.name}", bot.name) + if auth_info: + return json.loads(auth_info) + auth_info = client(bot).api_call("auth.test") + if auth_info["ok"]: + redis_client.hset(f"auth:{bot.team.name}", bot.id, json.dumps(auth_info)) + return auth_info + else: + logger.warning( + "Failed to get auth info", + extra=merge_logging_context( + _get_failure_context(auth_info), + bot.logging_context, + ), + ) + return {} def get_name_from_user(user): diff --git a/omnibot/services/slack/bot.py b/omnibot/services/slack/bot.py index d1b6059..ac1c5cf 100644 --- a/omnibot/services/slack/bot.py +++ b/omnibot/services/slack/bot.py @@ -1,4 +1,5 @@ from omnibot import settings +from omnibot.services.slack import get_auth from omnibot.services.slack.team import Team from omnibot.utils import merge_logging_context @@ -82,6 +83,15 @@ def name(self): @property def bot_id(self): return self._bot_data.get("app_id") + + @property + def user_id(self): + user_id = self._bot_data.get("user_id") + if not user_id: + user_id = get_auth(self).get("user_id") + if user_id: + self._bot_data["user_id"] = user_id + return user_id @property def verification_token(self): diff --git a/omnibot/services/slack/message.py b/omnibot/services/slack/message.py index 46db0f1..b6d50be 100644 --- a/omnibot/services/slack/message.py +++ b/omnibot/services/slack/message.py @@ -175,6 +175,8 @@ def _parse_payload(self): for user_id, user_name in self.users.items(): if self.bot.name == user_name: self._payload["mentioned"] = True + if self.bot.user_id == f"<@{user_id}>": + self._payload["mentioned"] = True try: self._payload["command_text"] = parser.extract_command( # Similar to mentions above, we find the command text