Skip to content

Commit 36ab652

Browse files
authored
Enable developers to define app.message listener without args to capture all messages (slackapi#848)
1 parent ade7bc4 commit 36ab652

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

slack_bolt/app/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def __call__(*args, **kwargs):
743743

744744
def message(
745745
self,
746-
keyword: Union[str, Pattern],
746+
keyword: Union[str, Pattern] = "",
747747
matchers: Optional[Sequence[Callable[..., bool]]] = None,
748748
middleware: Optional[Sequence[Union[Callable, Middleware]]] = None,
749749
) -> Callable[..., Optional[Callable[..., Optional[BoltResponse]]]]:

slack_bolt/app/async_app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def __call__(*args, **kwargs):
773773

774774
def message(
775775
self,
776-
keyword: Union[str, Pattern],
776+
keyword: Union[str, Pattern] = "",
777777
matchers: Optional[Sequence[Callable[..., Awaitable[bool]]]] = None,
778778
middleware: Optional[Sequence[Union[Callable, AsyncMiddleware]]] = None,
779779
) -> Callable[..., Optional[Callable[..., Awaitable[Optional[BoltResponse]]]]]:

tests/scenario_tests/test_message.py

+34
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,40 @@ def test_string_keyword(self):
7474
time.sleep(1) # wait a bit after auto ack()
7575
assert self.mock_received_requests["/chat.postMessage"] == 1
7676

77+
def test_all_message_matching_1(self):
78+
app = App(
79+
client=self.web_client,
80+
signing_secret=self.signing_secret,
81+
)
82+
83+
@app.message("")
84+
def handle_all_new_messages(say):
85+
say("Thanks!")
86+
87+
request = self.build_request2()
88+
response = app.dispatch(request)
89+
assert response.status == 200
90+
assert_auth_test_count(self, 1)
91+
time.sleep(1) # wait a bit after auto ack()
92+
assert self.mock_received_requests["/chat.postMessage"] == 1
93+
94+
def test_all_message_matching_2(self):
95+
app = App(
96+
client=self.web_client,
97+
signing_secret=self.signing_secret,
98+
)
99+
100+
@app.message()
101+
def handle_all_new_messages(say):
102+
say("Thanks!")
103+
104+
request = self.build_request2()
105+
response = app.dispatch(request)
106+
assert response.status == 200
107+
assert_auth_test_count(self, 1)
108+
time.sleep(1) # wait a bit after auto ack()
109+
assert self.mock_received_requests["/chat.postMessage"] == 1
110+
77111
def test_string_keyword_capturing(self):
78112
app = App(
79113
client=self.web_client,

tests/scenario_tests_async/test_message.py

+36
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ async def test_string_keyword(self):
8181
await asyncio.sleep(1) # wait a bit after auto ack()
8282
assert self.mock_received_requests["/chat.postMessage"] == 1
8383

84+
@pytest.mark.asyncio
85+
async def test_all_message_matching_1(self):
86+
app = AsyncApp(
87+
client=self.web_client,
88+
signing_secret=self.signing_secret,
89+
)
90+
91+
@app.message("")
92+
async def handle_all_new_messages(say):
93+
await say("Thanks!")
94+
95+
request = self.build_request2()
96+
response = await app.async_dispatch(request)
97+
assert response.status == 200
98+
await assert_auth_test_count_async(self, 1)
99+
await asyncio.sleep(1) # wait a bit after auto ack()
100+
assert self.mock_received_requests["/chat.postMessage"] == 1
101+
102+
@pytest.mark.asyncio
103+
async def test_all_message_matching_2(self):
104+
app = AsyncApp(
105+
client=self.web_client,
106+
signing_secret=self.signing_secret,
107+
)
108+
109+
@app.message()
110+
async def handle_all_new_messages(say):
111+
await say("Thanks!")
112+
113+
request = self.build_request2()
114+
response = await app.async_dispatch(request)
115+
assert response.status == 200
116+
await assert_auth_test_count_async(self, 1)
117+
await asyncio.sleep(1) # wait a bit after auto ack()
118+
assert self.mock_received_requests["/chat.postMessage"] == 1
119+
84120
@pytest.mark.asyncio
85121
async def test_string_keyword_capturing(self):
86122
app = AsyncApp(

0 commit comments

Comments
 (0)