Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose auto acknowledge for function handlers #1173

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion slack_bolt/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ def function(
callback_id: Union[str, Pattern],
matchers: Optional[Sequence[Callable[..., bool]]] = None,
middleware: Optional[Sequence[Union[Callable, Middleware]]] = None,
auto_acknowledge: bool = True,
) -> Callable[..., Optional[Callable[..., Optional[BoltResponse]]]]:
"""Registers a new Function listener.
This method can be used as either a decorator or a method.
Expand Down Expand Up @@ -911,7 +912,7 @@ def reverse_string(ack: Ack, inputs: dict, complete: Complete, fail: Fail):
def __call__(*args, **kwargs):
functions = self._to_listener_functions(kwargs) if kwargs else list(args)
primary_matcher = builtin_matchers.function_executed(callback_id=callback_id, base_logger=self._base_logger)
return self._register_listener(functions, primary_matcher, matchers, middleware, True)
return self._register_listener(functions, primary_matcher, matchers, middleware, auto_acknowledge)

return __call__

Expand Down
3 changes: 2 additions & 1 deletion slack_bolt/app/async_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ def function(
callback_id: Union[str, Pattern],
matchers: Optional[Sequence[Callable[..., Awaitable[bool]]]] = None,
middleware: Optional[Sequence[Union[Callable, AsyncMiddleware]]] = None,
auto_acknowledge: bool = True,
) -> Callable[..., Optional[Callable[..., Awaitable[BoltResponse]]]]:
"""Registers a new Function listener.
This method can be used as either a decorator or a method.
Expand Down Expand Up @@ -947,7 +948,7 @@ def __call__(*args, **kwargs):
primary_matcher = builtin_matchers.function_executed(
callback_id=callback_id, base_logger=self._base_logger, asyncio=True
)
return self._register_listener(functions, primary_matcher, matchers, middleware, True)
return self._register_listener(functions, primary_matcher, matchers, middleware, auto_acknowledge)

return __call__

Expand Down
37 changes: 37 additions & 0 deletions tests/scenario_tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,32 @@ def test_invalid_declaration(self):
with pytest.raises(TypeError):
func("hello world")

def test_auto_acknowledge_false_with_acknowledging(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False)(just_ack)

request = self.build_request_from_body(function_body)
response = app.dispatch(request)
assert response.status == 200
assert_auth_test_count(self, 1)

def test_auto_acknowledge_false_without_acknowledging(self, caplog):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False)(just_no_ack)

request = self.build_request_from_body(function_body)
response = app.dispatch(request)

assert response.status == 404
assert_auth_test_count(self, 1)
assert f"WARNING {just_no_ack.__name__} didn't call ack()" in caplog.text


function_body = {
"token": "verification_token",
Expand Down Expand Up @@ -230,3 +256,14 @@ def complete_it(body, event, complete):
assert body == function_body
assert event == function_body["event"]
complete(outputs={})


def just_ack(ack, body, event):
assert body == function_body
assert event == function_body["event"]
ack()


def just_no_ack(body, event):
assert body == function_body
assert event == function_body["event"]
38 changes: 38 additions & 0 deletions tests/scenario_tests_async/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ async def test_invalid_callback_id(self):
assert response.status == 404
await assert_auth_test_count_async(self, 1)

@pytest.mark.asyncio
async def test_auto_acknowledge_false_with_acknowledging(self):
app = AsyncApp(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False)(just_ack)

request = self.build_request_from_body(function_body)
response = await app.async_dispatch(request)
assert response.status == 200
await assert_auth_test_count_async(self, 1)

@pytest.mark.asyncio
async def test_auto_acknowledge_false_without_acknowledging(self, caplog):
app = AsyncApp(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.function("reverse", auto_acknowledge=False)(just_no_ack)

request = self.build_request_from_body(function_body)
response = await app.async_dispatch(request)
assert response.status == 404
await assert_auth_test_count_async(self, 1)
assert f"WARNING {just_no_ack.__name__} didn't call ack()" in caplog.text


function_body = {
"token": "verification_token",
Expand Down Expand Up @@ -238,3 +265,14 @@ async def complete_it(body, event, complete):
await complete(
outputs={},
)


async def just_ack(ack, body, event):
assert body == function_body
assert event == function_body["event"]
await ack()


async def just_no_ack(body, event):
assert body == function_body
assert event == function_body["event"]