Skip to content

Commit e042273

Browse files
committed
Improvement related to #199: add event type name validation
1 parent 90bfe6a commit e042273

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

slack_bolt/listener_matcher/builtins.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
to_action,
2222
is_workflow_step_save,
2323
)
24+
from ..logger.messages import error_message_event_type
2425

2526
if sys.version_info.major == 3 and sys.version_info.minor <= 6:
2627
from re import _pattern_type as Pattern
@@ -82,13 +83,15 @@ def event(
8283
) -> Union[ListenerMatcher, "AsyncListenerMatcher"]:
8384
if isinstance(constraints, (str, Pattern)):
8485
event_type: Union[str, Pattern] = constraints
86+
_verify_message_event_type(event_type)
8587

8688
def func(body: Dict[str, Any]) -> bool:
8789
return is_event(body) and _matches(event_type, body["event"]["type"])
8890

8991
return build_listener_matcher(func, asyncio)
9092

9193
elif "type" in constraints:
94+
_verify_message_event_type(constraints["type"])
9295

9396
def func(body: Dict[str, Any]) -> bool:
9497
if is_event(body):
@@ -132,6 +135,13 @@ def func(body: Dict[str, Any]) -> bool:
132135
)
133136

134137

138+
def _verify_message_event_type(event_type: str) -> None:
139+
if isinstance(event_type, str) and event_type.startswith("message."):
140+
raise ValueError(error_message_event_type(event_type))
141+
if isinstance(event_type, Pattern) and "message\\." in event_type.pattern:
142+
raise ValueError(error_message_event_type(event_type))
143+
144+
135145
def workflow_step_execute(
136146
callback_id: Union[str, Pattern],
137147
asyncio: bool = False,

slack_bolt/logger/messages.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def error_authorize_conflicts() -> str:
5050
return "`authorize` in the top-level arguments is not allowed when you pass either `oauth_settings` or `oauth_flow`"
5151

5252

53+
def error_message_event_type(event_type: str) -> str:
54+
return (
55+
f'Although the document mentions "{event_type}", '
56+
'it is not a valid event type. Use "message" instead. '
57+
"If you want to filter message events, you can use `event.channel_type` for it."
58+
)
59+
60+
5361
# -------------------------------
5462
# Warning
5563
# -------------------------------

tests/scenario_tests/test_events.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
from time import time, sleep
44

5+
import pytest
56
from slack_sdk.signature import SignatureVerifier
67
from slack_sdk.web import WebClient
78

@@ -553,3 +554,30 @@ def handler1(event):
553554
)
554555
response = app.dispatch(request)
555556
assert response.status == 200
557+
558+
# https://github.com/slackapi/bolt-python/issues/199
559+
def test_invalid_message_events(self):
560+
app = App(client=self.web_client, signing_secret=self.signing_secret)
561+
562+
def handle():
563+
pass
564+
565+
# valid
566+
app.event("message")(handle)
567+
568+
with pytest.raises(ValueError):
569+
app.event("message.channels")(handle)
570+
with pytest.raises(ValueError):
571+
app.event("message.groups")(handle)
572+
with pytest.raises(ValueError):
573+
app.event("message.im")(handle)
574+
with pytest.raises(ValueError):
575+
app.event("message.mpim")(handle)
576+
577+
with pytest.raises(ValueError):
578+
app.event(re.compile("message\\..*"))(handle)
579+
580+
with pytest.raises(ValueError):
581+
app.event({"type": "message.channels"})(handle)
582+
with pytest.raises(ValueError):
583+
app.event({"type": re.compile("message\\..*")})(handle)

tests/scenario_tests_async/test_events.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,37 @@ async def handler1(event):
561561
response = await app.async_dispatch(request)
562562
assert response.status == 200
563563

564+
# https://github.com/slackapi/bolt-python/issues/199
565+
@pytest.mark.asyncio
566+
async def test_invalid_message_events(self):
567+
app = AsyncApp(
568+
client=self.web_client,
569+
signing_secret=self.signing_secret,
570+
)
571+
572+
async def handle():
573+
pass
574+
575+
# valid
576+
app.event("message")(handle)
577+
578+
with pytest.raises(ValueError):
579+
app.event("message.channels")(handle)
580+
with pytest.raises(ValueError):
581+
app.event("message.groups")(handle)
582+
with pytest.raises(ValueError):
583+
app.event("message.im")(handle)
584+
with pytest.raises(ValueError):
585+
app.event("message.mpim")(handle)
586+
587+
with pytest.raises(ValueError):
588+
app.event(re.compile("message\\..*"))(handle)
589+
590+
with pytest.raises(ValueError):
591+
app.event({"type": "message.channels"})(handle)
592+
with pytest.raises(ValueError):
593+
app.event({"type": re.compile("message\\..*")})(handle)
594+
564595

565596
app_mention_body = {
566597
"token": "verification_token",

0 commit comments

Comments
 (0)