Skip to content

Commit bca80f5

Browse files
authored
Merge pull request #63 from avasilevskii/remove-channel-filter-for-mentions
Chat mode: allow mentions in all channels
2 parents de15da1 + 3348759 commit bca80f5

File tree

5 files changed

+27
-45
lines changed

5 files changed

+27
-45
lines changed

bugzooka/entrypoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def main() -> None:
8484
if args.enable_socket_mode:
8585

8686
logger.info("Starting Socket Mode (WebSocket) for responding to @ mentions")
87-
listener = SlackSocketListener(channel_id=SLACK_CHANNEL_ID, logger=logger)
87+
listener = SlackSocketListener(logger=logger)
8888

8989
# Start socket listener in a separate thread
9090
socket_thread = threading.Thread(

bugzooka/integrations/slack_client_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ class SlackClientBase:
1818
Provides common initialization, message formatting, and utility methods.
1919
"""
2020

21-
def __init__(self, channel_id: str, logger: logging.Logger):
21+
def __init__(self, logger: logging.Logger, channel_id: str = None):
2222
"""
2323
Initialize Slack client with common configuration.
2424
25-
:param channel_id: Slack channel ID to monitor/post to
2625
:param logger: Logger instance
26+
:param channel_id: Optional Slack channel ID to monitor/post to
2727
"""
2828
self.slack_bot_token = SLACK_BOT_TOKEN
2929
self.channel_id = channel_id

bugzooka/integrations/slack_fetcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class SlackMessageFetcher(SlackClientBase):
4444

4545
def __init__(self, channel_id, logger, poll_interval=600):
4646
"""Initialize Slack client and channel details."""
47-
# Initialize base class (handles WebClient, channel_id, logger, running flag, signal handler)
48-
super().__init__(channel_id, logger)
47+
# Initialize base class (handles WebClient, logger, channel_id, running flag, signal handler)
48+
super().__init__(logger, channel_id)
4949

5050
self.poll_interval = poll_interval # How often to fetch messages
5151
self.last_seen_timestamp = None # Track the latest message timestamp

bugzooka/integrations/slack_socket_listener.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,15 @@ class SlackSocketListener(SlackClientBase):
2828
Listens for @ mentions of the bot and processes messages asynchronously in real-time.
2929
"""
3030

31-
def __init__(
32-
self, channel_id: str, logger: logging.Logger, max_workers: int = 5
33-
):
31+
def __init__(self, logger: logging.Logger, max_workers: int = 5):
3432
"""
35-
Initialize Socket Mode client and channel details.
33+
Initialize Socket Mode client.
3634
37-
:param channel_id: Slack channel ID to monitor
3835
:param logger: Logger instance
3936
:param max_workers: Maximum number of concurrent mention handlers (default: 5)
4037
"""
41-
# Initialize base class (handles WebClient, channel_id, logger, running flag, signal handler)
42-
super().__init__(channel_id, logger)
38+
# Initialize base class (handles WebClient, logger, running flag, signal handler)
39+
super().__init__(logger)
4340

4441
self.slack_app_token = SLACK_APP_TOKEN
4542

@@ -66,7 +63,7 @@ def __init__(
6663
def _should_process_message(self, event: Dict[str, Any]) -> bool:
6764
"""
6865
Determine if a message should be processed.
69-
Only process messages that mention the bot and are in the configured channel.
66+
Only process app_mention events not sent by the bot itself.
7067
7168
:param event: Slack event data
7269
:return: True if message should be processed
@@ -75,13 +72,6 @@ def _should_process_message(self, event: Dict[str, Any]) -> bool:
7572
if event.get("type") != "app_mention":
7673
return False
7774

78-
# Check if it's in the right channel
79-
if event.get("channel") != self.channel_id:
80-
self.logger.debug(
81-
f"Ignoring mention in different channel: {event.get('channel')}"
82-
)
83-
return False
84-
8575
# Don't process messages from the bot itself
8676
if event.get("user") == JEDI_BOT_SLACK_USER_ID:
8777
self.logger.debug("Ignoring message from bot itself")
@@ -277,15 +267,8 @@ def run(self, **kwargs) -> None:
277267
278268
:param kwargs: Configuration arguments (not used, for compatibility)
279269
"""
280-
self.logger.info(
281-
f"🚀 Starting Slack Socket Mode Listener for Channel: {self.channel_id}"
282-
)
283-
self.logger.info(
284-
"Bot will respond to @ mentions with a simple greeting message"
285-
)
286-
self.logger.info(
287-
f"Async processing enabled with {self.executor._max_workers} worker threads"
288-
)
270+
self.logger.info("🚀 Starting Slack Socket Mode Listener")
271+
self.logger.info(f"Async processing enabled with {self.executor._max_workers} worker threads")
289272

290273
# Register the event handler
291274
self.socket_client.socket_mode_request_listeners.append(

tests/test_slack_socket_listener.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,20 @@ def test_initialization(self, mock_socket_mode_client, mock_web_client):
7979

8080
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
8181
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
82-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
82+
listener = SlackSocketListener(logger=logger)
8383

84-
assert listener.channel_id == CHANNEL_ID
8584
assert listener.logger == logger
8685
assert listener.running is True
8786
mock_socket_mode_client.assert_called_once()
8887
mock_web_client.assert_called_once()
8988

9089
def test_should_process_app_mention(self, mock_socket_mode_client, mock_web_client):
91-
"""Test that app_mention events in the correct channel are processed."""
90+
"""Test that app_mention events are processed."""
9291
logger = logging.getLogger("test")
9392

9493
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
9594
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
96-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
95+
listener = SlackSocketListener(logger=logger)
9796

9897
event = create_app_mention_event(
9998
text="<@UBOTID> analyze this failure",
@@ -102,23 +101,23 @@ def test_should_process_app_mention(self, mock_socket_mode_client, mock_web_clie
102101

103102
assert listener._should_process_message(event) is True
104103

105-
def test_should_not_process_wrong_channel(
104+
def test_should_process_any_channel(
106105
self, mock_socket_mode_client, mock_web_client
107106
):
108-
"""Test that mentions in other channels are ignored."""
107+
"""Test that mentions in any channel are processed."""
109108
logger = logging.getLogger("test")
110109

111110
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
112111
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
113-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
112+
listener = SlackSocketListener(logger=logger)
114113

115114
event = create_app_mention_event(
116115
text="<@UBOTID> analyze this failure",
117116
user="U12345",
118117
)
119118
event["channel"] = "C_DIFFERENT_CHANNEL"
120119

121-
assert listener._should_process_message(event) is False
120+
assert listener._should_process_message(event) is True
122121

123122
def test_should_not_process_bot_self_mention(
124123
self, mock_socket_mode_client, mock_web_client
@@ -129,7 +128,7 @@ def test_should_not_process_bot_self_mention(
129128
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
130129
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
131130
with patch("bugzooka.integrations.slack_socket_listener.JEDI_BOT_SLACK_USER_ID", "UBOTID"):
132-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
131+
listener = SlackSocketListener(logger=logger)
133132

134133
event = create_app_mention_event(
135134
text="<@UBOTID> analyze this failure",
@@ -146,7 +145,7 @@ def test_process_mention_sends_greeting(
146145

147146
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
148147
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
149-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
148+
listener = SlackSocketListener(logger=logger)
150149

151150
event = create_app_mention_event(
152151
text="<@UBOTID> hello",
@@ -172,7 +171,7 @@ def test_submit_mention_for_processing(
172171

173172
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
174173
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
175-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger, max_workers=2)
174+
listener = SlackSocketListener(logger=logger, max_workers=2)
176175

177176
event = create_app_mention_event(
178177
text="<@UBOTID> async test",
@@ -197,7 +196,7 @@ def test_submit_mention_prevents_duplicates(
197196

198197
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
199198
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
200-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger, max_workers=2)
199+
listener = SlackSocketListener(logger=logger, max_workers=2)
201200

202201
event = create_app_mention_event(
203202
text="<@UBOTID> duplicate test",
@@ -222,7 +221,7 @@ def test_process_socket_request_acknowledgement(
222221

223222
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
224223
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
225-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
224+
listener = SlackSocketListener(logger=logger)
226225

227226
event = create_app_mention_event(text="<@UBOTID> test", ts="1234567890.123456")
228227
socket_request = create_socket_mode_request(event)
@@ -246,7 +245,7 @@ def test_process_socket_request_adds_reaction_immediately(
246245

247246
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
248247
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
249-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
248+
listener = SlackSocketListener(logger=logger)
250249

251250
event = create_app_mention_event(
252251
text="<@UBOTID> test",
@@ -275,7 +274,7 @@ def test_process_socket_request_non_app_mention(
275274

276275
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
277276
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
278-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
277+
listener = SlackSocketListener(logger=logger)
279278

280279
# Create a different event type
281280
event = {
@@ -302,7 +301,7 @@ def test_process_mention_error_handling(
302301

303302
with patch("bugzooka.core.config.SLACK_BOT_TOKEN", "xoxb-test-token"):
304303
with patch("bugzooka.core.config.SLACK_APP_TOKEN", "xapp-test-token"):
305-
listener = SlackSocketListener(channel_id=CHANNEL_ID, logger=logger)
304+
listener = SlackSocketListener(logger=logger)
306305

307306
# Mock chat_postMessage to raise an exception
308307
mock_web_client.return_value.chat_postMessage.side_effect = Exception(

0 commit comments

Comments
 (0)