Skip to content

Commit

Permalink
♻️ (message_handlers): use regex for AI message handling to improve f…
Browse files Browse the repository at this point in the history
…lexibility and case insensitivity
  • Loading branch information
kyaukyuai committed Jan 28, 2025
1 parent 8cd828b commit 104ac61
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion slack_ai_agent/slack/handler/message_handlers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Slack message handlers module."""

import logging
import re
from typing import Any
from typing import Dict
from typing import List
Expand Down Expand Up @@ -55,7 +56,7 @@ def handle_hello_message(message: Dict[str, Any], say: Any) -> None:
text=f"Hey there <@{message['user']}>!",
)

@app.message("ai")
@app.message(re.compile(r"^ai\s+", re.IGNORECASE))
def handle_ai_message(message: Dict[str, Any], say: Any) -> None:
"""Handle messages that should be processed by the AI agent.
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test configuration module."""

import re
from typing import Any
from typing import Dict

Expand All @@ -11,6 +12,7 @@
@pytest.fixture
def mock_handlers(mocker: MockerFixture) -> Dict[str, Any]:
"""Create mock handlers for testing."""
ai_pattern = re.compile(r"^ai\s+", re.IGNORECASE)
return {
"action": mocker.MagicMock(),
"event": {
Expand All @@ -20,7 +22,7 @@ def mock_handlers(mocker: MockerFixture) -> Dict[str, Any]:
},
"message": {
"hello": mocker.MagicMock(),
"ai": mocker.MagicMock(),
ai_pattern: mocker.MagicMock(),
},
}

Expand Down
18 changes: 8 additions & 10 deletions tests/slack/handler/test_message_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test module for Slack message handlers."""

import re
from typing import Any
from typing import Dict

Expand All @@ -22,9 +23,7 @@ def test_handle_hello_message(
mock_app.message.assert_any_call("hello")
handler = mock_handlers["message"]["hello"].handler
message = {"user": "U123"}

handler(message=message, say=mock_say)

mock_say.assert_called_once()
call_args = mock_say.call_args[1]
blocks = call_args["blocks"]
Expand All @@ -41,6 +40,8 @@ def test_handle_ai_message(
mocker: MockerFixture,
) -> None:
"""Test AI message handler."""
ai_pattern = re.compile(r"^ai\s+", re.IGNORECASE)

mock_agent = mocker.MagicMock()
mocker.patch(
"slack_ai_agent.slack.handler.message_handlers.create_agent",
Expand All @@ -53,12 +54,10 @@ def test_handle_ai_message(
)

setup_message_handlers(mock_app)
mock_app.message.assert_any_call("ai")
handler = mock_handlers["message"]["ai"].handler
mock_app.message.assert_any_call(ai_pattern)
handler = mock_handlers["message"][ai_pattern].handler
message = {"text": "ai test message", "ts": "123.456"}

handler(message=message, say=mock_say)

mock_say.assert_called_once_with(text="Test response", thread_ts="123.456")


Expand All @@ -75,13 +74,12 @@ def test_handle_ai_message_empty(
return_value=mocker.MagicMock(),
)

ai_pattern = re.compile(r"^ai\s+", re.IGNORECASE)
setup_message_handlers(mock_app)
mock_app.message.assert_any_call("ai")
handler = mock_handlers["message"]["ai"].handler
mock_app.message.assert_any_call(ai_pattern)
handler = mock_handlers["message"][ai_pattern].handler
message = {"text": "ai", "ts": "123.456"}

handler(message=message, say=mock_say)

mock_say.assert_called_once_with(
text="Please provide a message for the AI agent to process.",
thread_ts="123.456",
Expand Down

0 comments on commit 104ac61

Please sign in to comment.