Skip to content

Commit fe9170e

Browse files
committed
Correctly support plain-text and @mention bot alternative prefixes.
1 parent 9ca2f15 commit fe9170e

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
### Changed
1010
- Update send_message data and allow changing certain things through msg.extras (#112)"
1111
### Fixed
12+
- Fix bot alternative prefix to support @mention and plain-text. #114 (@nzlosh)
1213

1314
## [0.3.1] 2025-04-02
1415
### Changed

src/slackv3/slackv3.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,11 @@ def _register_identifiers_pickling(self):
145145
)
146146

147147
def update_alternate_prefixes(self):
148-
"""Converts BOT_ALT_PREFIXES to use the slack ID instead of name
149-
150-
Slack only acknowledges direct callouts `@username` in chat if referred
151-
by using the ID of that user.
152148
"""
153-
# convert BOT_ALT_PREFIXES to a list
149+
Convert BOT_ALT_PREFIXES items in the form of `@username` to
150+
their equivalent Slack user ID `<@Uxxxxxx>`.
151+
"""
152+
# Cast BOT_ALT_PREFIXES to a list
154153
try:
155154
bot_prefixes = self.bot_config.BOT_ALT_PREFIXES.split(",")
156155
except AttributeError:
@@ -159,16 +158,29 @@ def update_alternate_prefixes(self):
159158
converted_prefixes = []
160159
for prefix in bot_prefixes:
161160
try:
162-
converted_prefixes.append(f"<@{self.username_to_userid(prefix)}>")
163-
except Exception as e:
164-
log.error(
165-
f'Failed to look up Slack userid for alternate prefix "{prefix}": {str(e)}'
166-
)
161+
# Skip prefixes that aren't slack usernames.
162+
new_prefix = prefix
163+
if prefix.startswith("@"):
164+
new_prefix = self.username_to_userid(prefix)
165+
if new_prefix != prefix:
166+
new_prefix = f"<@{new_prefix}>"
167+
log.debug("Alternate prefix %s converted to slack id %s", prefix, new_prefix)
168+
except UserDoesNotExistError as e:
169+
new_prefix = prefix
170+
log.warning("'%s' was not found: %s", prefix, str(e))
171+
except UserNotUniqueError as e:
172+
log.warning("'%s' must be unique: %s", prefix, str(e))
173+
continue
167174

168-
self.bot_alt_prefixes = tuple(
169-
x.lower() for x in converted_prefixes
170-
)
171-
log.debug(f"Converted bot_alt_prefixes: {self.bot_config.BOT_ALT_PREFIXES}")
175+
if self.bot_config.BOT_ALT_PREFIX_CASEINSENSITIVE:
176+
new_prefix = new_prefix.lower()
177+
converted_prefixes.append(new_prefix)
178+
179+
self.bot_alt_prefixes = tuple(converted_prefixes)
180+
181+
log.debug(f"Converted bot_alt_prefixes: {self.bot_config.BOT_ALT_PREFIXES} to {self.bot_alt_prefixes}")
182+
183+
log.debug(f"Converted bot_alt_prefixes: {self.bot_config.BOT_ALT_PREFIXES} to {self.bot_alt_prefixes}")
172184

173185
def _setup_event_callbacks(self):
174186
# List of events obtained from https://api.slack.com/events

0 commit comments

Comments
 (0)