Skip to content

Commit 37d994a

Browse files
committed
Apply internal adjustments for Socket Mode support
1 parent 5033ae4 commit 37d994a

File tree

11 files changed

+810
-34
lines changed

11 files changed

+810
-34
lines changed

slack_bolt/app/app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from slack_bolt.listener_matcher.listener_matcher import ListenerMatcher
3131
from slack_bolt.logger import get_bolt_app_logger, get_bolt_logger
3232
from slack_bolt.logger.messages import (
33-
error_signing_secret_not_found,
3433
warning_client_prioritized_and_token_skipped,
3534
warning_token_skipped,
3635
error_auth_test_failure,
@@ -106,9 +105,6 @@ def __init__(
106105
signing_secret = signing_secret or os.environ.get("SLACK_SIGNING_SECRET")
107106
token = token or os.environ.get("SLACK_BOT_TOKEN")
108107

109-
if signing_secret is None or signing_secret == "":
110-
raise BoltError(error_signing_secret_not_found())
111-
112108
self._name: str = name or inspect.stack()[1].filename.split(os.path.sep)[-1]
113109
self._signing_secret: str = signing_secret
114110

slack_bolt/app/async_app.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
)
2424
from slack_bolt.error import BoltError
2525
from slack_bolt.logger.messages import (
26-
error_signing_secret_not_found,
2726
warning_client_prioritized_and_token_skipped,
2827
warning_token_skipped,
2928
error_token_required,
@@ -115,9 +114,6 @@ def __init__(
115114
signing_secret = signing_secret or os.environ.get("SLACK_SIGNING_SECRET")
116115
token = token or os.environ.get("SLACK_BOT_TOKEN")
117116

118-
if signing_secret is None or signing_secret == "":
119-
raise BoltError(error_signing_secret_not_found())
120-
121117
self._name: str = name or inspect.stack()[1].filename.split(os.path.sep)[-1]
122118
self._signing_secret: str = signing_secret
123119
self._verification_token: Optional[str] = verification_token or os.environ.get(

slack_bolt/logger/messages.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
# -------------------------------
1111

1212

13-
def error_signing_secret_not_found() -> str:
14-
return (
15-
"Signing secret not found, so could not initialize the Bolt app."
16-
"Copy your Signing Secret from the Basic Information page "
17-
"and then store it in a new environment variable"
18-
)
19-
20-
2113
def error_client_invalid_type() -> str:
2214
return "`client` must be a slack_sdk.web.WebClient"
2315

slack_bolt/middleware/request_verification/async_request_verification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def async_process(
1414
resp: BoltResponse,
1515
next: Callable[[], Awaitable[BoltResponse]],
1616
) -> BoltResponse:
17-
if self._can_skip(req.body):
17+
if self._can_skip(req.mode, req.body):
1818
return await next()
1919

2020
body = req.raw_body

slack_bolt/middleware/request_verification/request_verification.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, signing_secret: str):
2121
def process(
2222
self, *, req: BoltRequest, resp: BoltResponse, next: Callable[[], BoltResponse],
2323
) -> BoltResponse:
24-
if self._can_skip(req.body):
24+
if self._can_skip(req.mode, req.body):
2525
return next()
2626

2727
body = req.raw_body
@@ -36,8 +36,10 @@ def process(
3636
# -----------------------------------------
3737

3838
@staticmethod
39-
def _can_skip(body: Dict[str, Any]) -> bool:
40-
return body is not None and body.get("ssl_check") == "1"
39+
def _can_skip(mode: str, body: Dict[str, Any]) -> bool:
40+
return mode == "socket_mode" or (
41+
body is not None and body.get("ssl_check") == "1"
42+
)
4143

4244
@staticmethod
4345
def _build_error_response() -> BoltResponse:

slack_bolt/request/async_request.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from typing import Dict, Optional, Union, Any, Sequence
22

33
from slack_bolt.context.async_context import AsyncBoltContext
4+
from slack_bolt.error import BoltError
45
from slack_bolt.request.async_internals import build_async_context
56
from slack_bolt.request.internals import (
67
parse_query,
78
parse_body,
89
build_normalized_headers,
910
extract_content_type,
11+
error_message_raw_body_required_in_http_mode,
12+
error_message_unknown_request_body_type,
1013
)
1114

1215

@@ -19,31 +22,42 @@ class AsyncBoltRequest:
1922
context: AsyncBoltContext
2023
lazy_only: bool
2124
lazy_function_name: Optional[str]
25+
mode: str # either "http" or "socket_mode"
2226

2327
def __init__(
2428
self,
2529
*,
26-
body: str,
30+
body: Union[str, dict],
2731
query: Optional[Union[str, Dict[str, str], Dict[str, Sequence[str]]]] = None,
2832
headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None,
2933
context: Optional[Dict[str, str]] = None,
34+
mode: str = "http", # either "http" or "socket_mode"
3035
):
3136
"""Request to a Bolt app.
3237
33-
:param body: The raw request body (only plain text is supported)
38+
:param body: The raw request body (only plain text is supported for "http" mode)
3439
:param query: The query string data in any data format.
3540
:param headers: The request headers.
3641
:param context: The context in this request.
42+
:param mode: The mode used for this request. (either "http" or "socket_mode")
3743
"""
38-
self.raw_body = body
44+
if mode == "http" and not isinstance(body, str):
45+
raise BoltError(error_message_raw_body_required_in_http_mode())
46+
self.raw_body = body if mode == "http" else ""
3947
self.query = parse_query(query)
4048
self.headers = build_normalized_headers(headers)
4149
self.content_type = extract_content_type(self.headers)
42-
self.body = parse_body(self.raw_body, self.content_type)
50+
if isinstance(body, str):
51+
self.body = parse_body(self.raw_body, self.content_type)
52+
elif isinstance(body, dict):
53+
self.body = body
54+
else:
55+
raise BoltError(error_message_unknown_request_body_type())
4356
self.context = build_async_context(
4457
AsyncBoltContext(context if context else {}), self.body
4558
)
4659
self.lazy_only = self.headers.get("x-slack-bolt-lazy-only", [False])[0]
4760
self.lazy_function_name = self.headers.get(
4861
"x-slack-bolt-lazy-function-name", [None]
4962
)[0]
63+
self.mode = mode

slack_bolt/request/internals.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,11 @@ def build_normalized_headers(
159159
f"Unsupported type ({type(value)}) of element in headers ({headers})"
160160
)
161161
return normalized_headers # type: ignore
162+
163+
164+
def error_message_raw_body_required_in_http_mode() -> str:
165+
return "`body` must be a raw string data when running in the HTTP server mode"
166+
167+
168+
def error_message_unknown_request_body_type() -> str:
169+
return "`body` must be either str or dict"

slack_bolt/request/request.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from typing import Dict, Optional, Union, Any, Sequence
22

33
from slack_bolt.context.context import BoltContext
4+
from slack_bolt.error import BoltError
45
from slack_bolt.request.internals import (
56
parse_query,
67
parse_body,
78
build_normalized_headers,
89
build_context,
910
extract_content_type,
11+
error_message_raw_body_required_in_http_mode,
12+
error_message_unknown_request_body_type,
1013
)
1114

1215

@@ -19,29 +22,41 @@ class BoltRequest:
1922
context: BoltContext
2023
lazy_only: bool
2124
lazy_function_name: Optional[str]
25+
mode: str # either "http" or "socket_mode"
2226

2327
def __init__(
2428
self,
2529
*,
26-
body: str,
30+
body: Union[str, dict],
2731
query: Optional[Union[str, Dict[str, str], Dict[str, Sequence[str]]]] = None,
2832
headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None,
2933
context: Optional[Dict[str, str]] = None,
34+
mode: str = "http", # either "http" or "socket_mode"
3035
):
3136
"""Request to a Bolt app.
3237
33-
:param body: The raw request body (only plain text is supported)
38+
:param body: The raw request body (only plain text is supported for "http" mode)
3439
:param query: The query string data in any data format.
3540
:param headers: The request headers.
3641
:param context: The context in this request.
42+
:param mode: The mode used for this request. (either "http" or "socket_mode")
3743
"""
38-
self.raw_body = body
44+
if mode == "http" and not isinstance(body, str):
45+
raise BoltError(error_message_raw_body_required_in_http_mode())
46+
self.raw_body = body if mode == "http" else ""
3947
self.query = parse_query(query)
4048
self.headers = build_normalized_headers(headers)
4149
self.content_type = extract_content_type(self.headers)
42-
self.body = parse_body(self.raw_body, self.content_type)
50+
if isinstance(body, str):
51+
self.body = parse_body(self.raw_body, self.content_type)
52+
elif isinstance(body, dict):
53+
self.body = body
54+
else:
55+
raise BoltError(error_message_unknown_request_body_type())
56+
4357
self.context = build_context(BoltContext(context if context else {}), self.body)
4458
self.lazy_only = self.headers.get("x-slack-bolt-lazy-only", [False])[0]
4559
self.lazy_function_name = self.headers.get(
4660
"x-slack-bolt-lazy-function-name", [None]
4761
)[0]
62+
self.mode = mode

0 commit comments

Comments
 (0)