Skip to content

Commit a09582c

Browse files
feat: expose auto acknowledge for function handlers (#1173)
1 parent 56d5ca3 commit a09582c

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

slack_bolt/app/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ def function(
877877
callback_id: Union[str, Pattern],
878878
matchers: Optional[Sequence[Callable[..., bool]]] = None,
879879
middleware: Optional[Sequence[Union[Callable, Middleware]]] = None,
880+
auto_acknowledge: bool = True,
880881
) -> Callable[..., Optional[Callable[..., Optional[BoltResponse]]]]:
881882
"""Registers a new Function listener.
882883
This method can be used as either a decorator or a method.
@@ -911,7 +912,7 @@ def reverse_string(ack: Ack, inputs: dict, complete: Complete, fail: Fail):
911912
def __call__(*args, **kwargs):
912913
functions = self._to_listener_functions(kwargs) if kwargs else list(args)
913914
primary_matcher = builtin_matchers.function_executed(callback_id=callback_id, base_logger=self._base_logger)
914-
return self._register_listener(functions, primary_matcher, matchers, middleware, True)
915+
return self._register_listener(functions, primary_matcher, matchers, middleware, auto_acknowledge)
915916

916917
return __call__
917918

slack_bolt/app/async_app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ def function(
911911
callback_id: Union[str, Pattern],
912912
matchers: Optional[Sequence[Callable[..., Awaitable[bool]]]] = None,
913913
middleware: Optional[Sequence[Union[Callable, AsyncMiddleware]]] = None,
914+
auto_acknowledge: bool = True,
914915
) -> Callable[..., Optional[Callable[..., Awaitable[BoltResponse]]]]:
915916
"""Registers a new Function listener.
916917
This method can be used as either a decorator or a method.
@@ -947,7 +948,7 @@ def __call__(*args, **kwargs):
947948
primary_matcher = builtin_matchers.function_executed(
948949
callback_id=callback_id, base_logger=self._base_logger, asyncio=True
949950
)
950-
return self._register_listener(functions, primary_matcher, matchers, middleware, True)
951+
return self._register_listener(functions, primary_matcher, matchers, middleware, auto_acknowledge)
951952

952953
return __call__
953954

tests/scenario_tests/test_function.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,32 @@ def test_invalid_declaration(self):
112112
with pytest.raises(TypeError):
113113
func("hello world")
114114

115+
def test_auto_acknowledge_false_with_acknowledging(self):
116+
app = App(
117+
client=self.web_client,
118+
signing_secret=self.signing_secret,
119+
)
120+
app.function("reverse", auto_acknowledge=False)(just_ack)
121+
122+
request = self.build_request_from_body(function_body)
123+
response = app.dispatch(request)
124+
assert response.status == 200
125+
assert_auth_test_count(self, 1)
126+
127+
def test_auto_acknowledge_false_without_acknowledging(self, caplog):
128+
app = App(
129+
client=self.web_client,
130+
signing_secret=self.signing_secret,
131+
)
132+
app.function("reverse", auto_acknowledge=False)(just_no_ack)
133+
134+
request = self.build_request_from_body(function_body)
135+
response = app.dispatch(request)
136+
137+
assert response.status == 404
138+
assert_auth_test_count(self, 1)
139+
assert f"WARNING {just_no_ack.__name__} didn't call ack()" in caplog.text
140+
115141

116142
function_body = {
117143
"token": "verification_token",
@@ -230,3 +256,14 @@ def complete_it(body, event, complete):
230256
assert body == function_body
231257
assert event == function_body["event"]
232258
complete(outputs={})
259+
260+
261+
def just_ack(ack, body, event):
262+
assert body == function_body
263+
assert event == function_body["event"]
264+
ack()
265+
266+
267+
def just_no_ack(body, event):
268+
assert body == function_body
269+
assert event == function_body["event"]

tests/scenario_tests_async/test_function.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ async def test_invalid_callback_id(self):
116116
assert response.status == 404
117117
await assert_auth_test_count_async(self, 1)
118118

119+
@pytest.mark.asyncio
120+
async def test_auto_acknowledge_false_with_acknowledging(self):
121+
app = AsyncApp(
122+
client=self.web_client,
123+
signing_secret=self.signing_secret,
124+
)
125+
app.function("reverse", auto_acknowledge=False)(just_ack)
126+
127+
request = self.build_request_from_body(function_body)
128+
response = await app.async_dispatch(request)
129+
assert response.status == 200
130+
await assert_auth_test_count_async(self, 1)
131+
132+
@pytest.mark.asyncio
133+
async def test_auto_acknowledge_false_without_acknowledging(self, caplog):
134+
app = AsyncApp(
135+
client=self.web_client,
136+
signing_secret=self.signing_secret,
137+
)
138+
app.function("reverse", auto_acknowledge=False)(just_no_ack)
139+
140+
request = self.build_request_from_body(function_body)
141+
response = await app.async_dispatch(request)
142+
assert response.status == 404
143+
await assert_auth_test_count_async(self, 1)
144+
assert f"WARNING {just_no_ack.__name__} didn't call ack()" in caplog.text
145+
119146

120147
function_body = {
121148
"token": "verification_token",
@@ -238,3 +265,14 @@ async def complete_it(body, event, complete):
238265
await complete(
239266
outputs={},
240267
)
268+
269+
270+
async def just_ack(ack, body, event):
271+
assert body == function_body
272+
assert event == function_body["event"]
273+
await ack()
274+
275+
276+
async def just_no_ack(body, event):
277+
assert body == function_body
278+
assert event == function_body["event"]

0 commit comments

Comments
 (0)