diff --git a/slack_bolt/middleware/message_listener_matches/async_message_listener_matches.py b/slack_bolt/middleware/message_listener_matches/async_message_listener_matches.py index 64eb4dcd3..6a76c6a5b 100644 --- a/slack_bolt/middleware/message_listener_matches/async_message_listener_matches.py +++ b/slack_bolt/middleware/message_listener_matches/async_message_listener_matches.py @@ -20,9 +20,13 @@ async def async_process( ) -> BoltResponse: text = req.body.get("event", {}).get("text", "") if text: - m = re.search(self.keyword, text) - if m is not None: - req.context["matches"] = m.groups() # tuple + m = re.findall(self.keyword, text) + if m is not None and m != []: + if type(m[0]) is not tuple: + m = tuple(m) + else: + m = m[0] + req.context["matches"] = m # tuple or list return await next() # As the text doesn't match, skip running the listener diff --git a/slack_bolt/middleware/message_listener_matches/message_listener_matches.py b/slack_bolt/middleware/message_listener_matches/message_listener_matches.py index 41905b381..14b4091eb 100644 --- a/slack_bolt/middleware/message_listener_matches/message_listener_matches.py +++ b/slack_bolt/middleware/message_listener_matches/message_listener_matches.py @@ -20,9 +20,13 @@ def process( ) -> BoltResponse: text = req.body.get("event", {}).get("text", "") if text: - m = re.search(self.keyword, text) - if m is not None: - req.context["matches"] = m.groups() # tuple + m = re.findall(self.keyword, text) + if m is not None and m != []: + if type(m[0]) is not tuple: + m = tuple(m) + else: + m = m[0] + req.context["matches"] = m # tuple or list return next() # As the text doesn't match, skip running the listener diff --git a/tests/scenario_tests/test_message.py b/tests/scenario_tests/test_message.py index fe16f8245..a7f7c0be0 100644 --- a/tests/scenario_tests/test_message.py +++ b/tests/scenario_tests/test_message.py @@ -57,6 +57,9 @@ def build_request(self) -> BoltRequest: def build_request2(self) -> BoltRequest: return self.build_request_from_body(message_body2) + def build_request3(self) -> BoltRequest: + return self.build_request_from_body(message_body3) + def test_string_keyword(self): app = App( client=self.web_client, @@ -101,6 +104,20 @@ def test_string_keyword_capturing2(self): time.sleep(1) # wait a bit after auto ack() assert self.mock_received_requests["/chat.postMessage"] == 1 + def test_string_keyword_capturing_multi_capture(self): + app = App( + client=self.web_client, + signing_secret=self.signing_secret, + ) + app.message(re.compile("([a-z|A-Z]{3,}-[0-9]+)"))(verify_matches_multi) + + request = self.build_request3() + response = app.dispatch(request) + assert response.status == 200 + assert_auth_test_count(self, 1) + time.sleep(1) # wait a bit after auto ack() + assert self.mock_received_requests["/chat.postMessage"] == 1 + def test_string_keyword_unmatched(self): app = App( client=self.web_client, @@ -253,9 +270,40 @@ def whats_up(body, payload, message, say): } +message_body3 = { + "token": "verification_token", + "team_id": "T111", + "enterprise_id": "E111", + "api_app_id": "A111", + "event": { + "client_msg_id": "a8744611-0210-4f85-9f15-5faf7fb225c8", + "type": "message", + "text": "Please fix JIRA-1234, SCM-567 and BUG-169 as soon as you can!", + "user": "W111", + "ts": "1596183880.004200", + "team": "T111", + "channel": "C111", + "event_ts": "1596183880.004200", + "channel_type": "channel", + }, + "type": "event_callback", + "event_id": "Ev111", + "event_time": 1596183880, + "authed_users": ["W111"], +} + + def verify_matches(context, say, body, payload, message): assert context["matches"] == ("103", "you") assert context.matches == ("103", "you") assert body["event"] == message assert payload == message say("Thanks!") + + +def verify_matches_multi(context, say, body, payload, message): + assert context["matches"] == ("JIRA-1234", "SCM-567", "BUG-169") + assert context.matches == ("JIRA-1234", "SCM-567", "BUG-169") + assert body["event"] == message + assert payload == message + say("Thanks!") diff --git a/tests/scenario_tests_async/test_message.py b/tests/scenario_tests_async/test_message.py index b1504d550..43600363e 100644 --- a/tests/scenario_tests_async/test_message.py +++ b/tests/scenario_tests_async/test_message.py @@ -63,6 +63,9 @@ def build_request(self) -> AsyncBoltRequest: def build_request2(self) -> AsyncBoltRequest: return self.build_request_from_body(message_body2) + def build_request3(self) -> AsyncBoltRequest: + return self.build_request_from_body(message_body3) + @pytest.mark.asyncio async def test_string_keyword(self): app = AsyncApp( @@ -110,6 +113,21 @@ async def test_string_keyword_capturing2(self): await asyncio.sleep(1) # wait a bit after auto ack() assert self.mock_received_requests["/chat.postMessage"] == 1 + @pytest.mark.asyncio + async def test_string_keyword_capturing_multi_capture(self): + app = AsyncApp( + client=self.web_client, + signing_secret=self.signing_secret, + ) + app.message(re.compile("([a-z|A-Z]{3,}-[0-9]+)"))(verify_matches_multi) + + request = self.build_request3() + response = await app.async_dispatch(request) + assert response.status == 200 + await assert_auth_test_count_async(self, 1) + await asyncio.sleep(1) # wait a bit after auto ack() + assert self.mock_received_requests["/chat.postMessage"] == 1 + @pytest.mark.asyncio async def test_string_keyword_unmatched(self): app = AsyncApp( @@ -268,7 +286,36 @@ async def whats_up(body, say): } +message_body3 = { + "token": "verification_token", + "team_id": "T111", + "enterprise_id": "E111", + "api_app_id": "A111", + "event": { + "client_msg_id": "a8744611-0210-4f85-9f15-5faf7fb225c8", + "type": "message", + "text": "Please fix JIRA-1234, SCM-567 and BUG-169 as soon as you can!", + "user": "W111", + "ts": "1596183880.004200", + "team": "T111", + "channel": "C111", + "event_ts": "1596183880.004200", + "channel_type": "channel", + }, + "type": "event_callback", + "event_id": "Ev111", + "event_time": 1596183880, + "authed_users": ["W111"], +} + + async def verify_matches(context, say): assert context["matches"] == ("103", "you") assert context.matches == ("103", "you") await say("Thanks!") + + +async def verify_matches_multi(context, say, body, payload, message): + assert context["matches"] == ("JIRA-1234", "SCM-567", "BUG-169") + assert context.matches == ("JIRA-1234", "SCM-567", "BUG-169") + await say("Thanks!")