Skip to content

Commit 744b246

Browse files
authored
[INTPROD-9204] Allow Match By Bot User ID (#106)
This pull request includes changes to the `omnibot` service and its documentation. The changes mainly revolve around the addition of a `user_id` property in the `Bot` class, the modification of the `_parse_payload` method in the `Message` class to include a check for the bot's `user_id`, and the addition of important notes in the `Adding normal slack apps` documentation. Documentation Updates: * [`docs/root/adding_new_slack_apps.rst`](diffhunk://#diff-31bdebcf2f1187f82ac5efc676dd93fb8c7c5f4ee85b81f7fc486a6ef543c9deR48-R53): Added notes about the potential for the bot to be called twice if `app_mention` is added, the need for `bot.name` to match the app `user_handle`, and the need to specify `match_mention: true` if `user_handle` and `bot.name` do not match. Codebase Updates: * [`omnibot/services/slack/bot.py`](diffhunk://#diff-4050ebc6feafe8ceb1145ae4c7149d83a181f55dc32c75e79525a8a5709ac785R2): Imported `get_auth` from `omnibot.services.slack` and added a `user_id` property to the `Bot` class that fetches the `user_id` from `bot_data` or calls `get_auth` if `user_id` is not present. [[1]](diffhunk://#diff-4050ebc6feafe8ceb1145ae4c7149d83a181f55dc32c75e79525a8a5709ac785R2) [[2]](diffhunk://#diff-4050ebc6feafe8ceb1145ae4c7149d83a181f55dc32c75e79525a8a5709ac785R87-R95) * [`omnibot/services/slack/message.py`](diffhunk://#diff-76d2c4d8159d285244ea3ee98f6d2acde313fc5413b55106384fea03f1bf184dL179-R183): Modified the `_parse_payload` method to check if the bot's `user_id` is mentioned in the message. * [`tests/unit/omnibot/services/slack/message_test.py`](diffhunk://#diff-f42e42442277bcdb393292670d558e99345a87f55f76ce133bc0842ce05f514aR14): Updated the test setup to include a `user_id` in the bot's data.
1 parent 9567a20 commit 744b246

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

docs/root/adding_new_slack_apps.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Adding normal slack apps
4545
* message.groups
4646
* message.im
4747
* message.mpim
48+
#.
49+
* Note 1: adding ``app_mention`` may result in your bot being called twice.
50+
* Note 2: the configuration `bot.name` for your bot should match your app user_handle
51+
* Note 3: if your app user_handle does not match your `bot.name` you need to
52+
specify `match_mention: true` to receive callbacks
53+
4854

4955
#. Add interactive component configuration (if the bot needs interactive components), using the ``Interactive Components`` link in the sidebar:
5056

omnibot/services/slack/bot.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from omnibot import settings
2+
from omnibot.services.slack import get_auth
23
from omnibot.services.slack.team import Team
34
from omnibot.utils import merge_logging_context
45

@@ -83,6 +84,15 @@ def name(self):
8384
def bot_id(self):
8485
return self._bot_data.get("app_id")
8586

87+
@property
88+
def user_id(self):
89+
user_id = self._bot_data.get("user_id")
90+
if not user_id:
91+
user_id = get_auth(self).get("user_id")
92+
if user_id:
93+
self._bot_data["user_id"] = user_id
94+
return user_id
95+
8696
@property
8797
def verification_token(self):
8898
try:

omnibot/services/slack/message.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,11 @@ def _parse_payload(self):
176176
extra=self.event_trace,
177177
)
178178
self._payload["mentioned"] = False
179-
for _, user_name in self.users.items():
179+
for user_id, user_name in self.users.items():
180180
if self.bot.name == user_name:
181181
self._payload["mentioned"] = True
182+
if f"<@{self.bot.user_id}>" == user_id:
183+
self._payload["mentioned"] = True
182184
try:
183185
self._payload["command_text"] = parser.extract_command(
184186
# Similar to mentions above, we find the command text

tests/unit/omnibot/services/slack/message_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
def test_message(mocker):
1212
_team = Team.get_team_by_name("testteam")
1313
_bot = Bot.get_bot_by_name(_team, "echobot")
14+
_bot._bot_data["user_id"] = "A12345678"
1415
event = {
1516
"ts": "1234567.12",
1617
"thread_ts": None,

0 commit comments

Comments
 (0)