Skip to content
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
2 changes: 2 additions & 0 deletions docs/english/concepts/web-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ You can call [any Web API method](/reference/methods) using the `WebClient` prov

The token used to initialize Bolt can be found in the `context` object, which is required to call most Web API methods.

The `SLACK_API_URL` environment variable sets the `base_url` of the client that Bolt creates, with a trailing slash added when missing. A `client` passed to `App` or `AsyncApp` keeps its own `base_url`.

:::info[Refer to [the module document](https://docs.slack.dev/tools/bolt-python/reference/kwargs_injection/args.html) to learn the available listener arguments.]

:::
Expand Down
3 changes: 2 additions & 1 deletion slack_bolt/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ def message_hello(message, say):
signing_secret: The Signing Secret value used for verifying requests from Slack.
token: The bot/user access token required only for single-workspace app.
token_verification_enabled: Verifies the validity of the given token if True.
client: The singleton `slack_sdk.WebClient` instance for this app.
client: The singleton `slack_sdk.WebClient` instance for this app. When absent, the created client's
`base_url` defaults to the `SLACK_API_URL` environment variable if set.
Comment thread
zimeg marked this conversation as resolved.
Resolved
before_authorize: A global middleware that can be executed right before authorize function
authorize: The function to authorize an incoming request from Slack
by checking if there is a team/user in the installation data.
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 @@ -179,7 +179,8 @@ async def message_hello(message, say): # async function
the built-in handler, which pints warning logs and returns 404 to Slack (Default: False)
signing_secret: The Signing Secret value used for verifying requests from Slack.
token: The bot/user access token required only for single-workspace app.
client: The singleton `slack_sdk.web.async_client.AsyncWebClient` instance for this app.
client: The singleton `slack_sdk.web.async_client.AsyncWebClient` instance for this app. When absent,
the created client's `base_url` defaults to the `SLACK_API_URL` environment variable if set.
before_authorize: A global middleware that can be executed right before authorize function
authorize: The function to authorize an incoming request from Slack
by checking if there is a team/user in the installation data.
Expand Down
2 changes: 2 additions & 0 deletions slack_bolt/util/async_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from logging import Logger
from typing import Optional

Expand All @@ -8,6 +9,7 @@

def create_async_web_client(token: Optional[str] = None, logger: Optional[Logger] = None) -> AsyncWebClient:
return AsyncWebClient(
base_url=os.environ.get("SLACK_API_URL") or AsyncWebClient.BASE_URL,
token=token,
logger=logger,
user_agent_prefix=f"Bolt-Async/{bolt_version}",
Expand Down
2 changes: 2 additions & 0 deletions slack_bolt/util/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import inspect
import os
import sys
from logging import Logger
from typing import Optional, Union, Dict, Any, Sequence, Callable, List
Expand All @@ -13,6 +14,7 @@

def create_web_client(token: Optional[str] = None, logger: Optional[Logger] = None) -> WebClient:
return WebClient(
base_url=os.environ.get("SLACK_API_URL") or WebClient.BASE_URL,
token=token,
logger=logger,
user_agent_prefix=f"Bolt/{bolt_version}",
Expand Down
30 changes: 30 additions & 0 deletions tests/scenario_tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ def test_socket_mode_app_without_signing_secret(self):
)
assert app is not None

def test_slack_api_url_env_sets_base_url(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/")
app = App(
signing_secret="valid",
token="xoxb-valid",
token_verification_enabled=False,
)
assert app.client.base_url == "https://qa.slack.com/api/"

def test_slack_api_url_env_skipped_for_given_client(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/")
app = App(
signing_secret="valid",
client=WebClient(
token="xoxb-valid",
base_url="https://custom.slack.com/api/",
),
token_verification_enabled=False,
)
assert app.client.base_url == "https://custom.slack.com/api/"

def test_slack_api_url_env_empty_keeps_default_base_url(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "")
app = App(
signing_secret="valid",
token="xoxb-valid",
token_verification_enabled=False,
)
assert app.client.base_url == "https://slack.com/api/"

# --------------------------
# multi teams auth
# --------------------------
Expand Down
27 changes: 27 additions & 0 deletions tests/scenario_tests_async/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ def test_token_absence(self):
with pytest.raises(BoltError):
AsyncApp(signing_secret="valid", token="")

def test_slack_api_url_env_sets_base_url(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/")
app = AsyncApp(
signing_secret="valid",
token="xoxb-valid",
)
assert app.client.base_url == "https://qa.slack.com/api/"

def test_slack_api_url_env_skipped_for_given_client(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/")
app = AsyncApp(
signing_secret="valid",
client=AsyncWebClient(
token="xoxb-valid",
base_url="https://custom.slack.com/api/",
),
)
assert app.client.base_url == "https://custom.slack.com/api/"

def test_slack_api_url_env_empty_keeps_default_base_url(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "")
app = AsyncApp(
signing_secret="valid",
token="xoxb-valid",
)
assert app.client.base_url == "https://slack.com/api/"

# --------------------------
# multi teams auth
# --------------------------
Expand Down
13 changes: 13 additions & 0 deletions tests/slack_bolt/oauth/test_oauth_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def test_instantiation(self):
assert oauth_flow.logger is not None
assert oauth_flow.client is not None

def test_instantiation_slack_api_url_env(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/")
oauth_flow = OAuthFlow(
settings=OAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
)
assert oauth_flow.client.base_url == "https://qa.slack.com/api/"

def test_handle_installation_default(self):
oauth_flow = OAuthFlow(
settings=OAuthSettings(
Expand Down
15 changes: 15 additions & 0 deletions tests/slack_bolt_async/oauth/test_async_oauth_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ async def test_instantiation(self):
assert oauth_flow.logger is not None
assert oauth_flow.client is not None

@pytest.mark.asyncio
async def test_instantiation_slack_api_url_env(self, monkeypatch):
monkeypatch.setenv("SLACK_API_URL", "https://qa.slack.com/api/")
oauth_flow = AsyncOAuthFlow(
settings=AsyncOAuthSettings(
client_id="111.222",
client_secret="xxx",
scopes=["chat:write", "commands"],
user_scopes=["search:read"],
installation_store=FileInstallationStore(),
state_store=FileOAuthStateStore(expiration_seconds=120),
)
)
assert oauth_flow.client.base_url == "https://qa.slack.com/api/"

@pytest.mark.asyncio
async def test_scopes_as_str(self):
settings = AsyncOAuthSettings(
Expand Down