airflow.providers.slack.hooks.slack
¶
Module Contents¶
Classes¶
Creates a Slack API Connection to be used for calls. |
- class airflow.providers.slack.hooks.slack.SlackHook(*, slack_conn_id=default_conn_name, base_url=None, timeout=None, proxy=None, retry_handlers=None, **extra_client_args)[source]¶
Bases:
airflow.hooks.base.BaseHook
Creates a Slack API Connection to be used for calls.
This class provide a thin wrapper around the
slack_sdk.WebClient
.See also
Warning
This hook intend to use Slack API connection and might not work correctly with Slack Incoming Webhook and HTTP connections.
Takes both Slack API token directly and connection that has Slack API token. If both are supplied, Slack API token will be used. Also exposes the rest of slack.WebClient args.
- Examples:
# Create hook slack_hook = SlackHook(slack_conn_id="slack_api_default") # Call generic API with parameters (errors are handled by hook) # For more details check https://api.slack.com/methods/chat.postMessage slack_hook.call("chat.postMessage", json={"channel": "#random", "text": "Hello world!"}) # Call method from Slack SDK (you have to handle errors yourself) # For more details check https://slack.dev/python-slack-sdk/web/index.html#messaging slack_hook.client.chat_postMessage(channel="#random", text="Hello world!")
- Parameters
slack_conn_id (str) – Slack connection id that has Slack API token in the password field.
timeout (int | None) – The maximum number of seconds the client will wait to connect and receive a response from Slack. If not set than default WebClient value will use.
base_url (str | None) – A string representing the Slack API base URL. If not set than default WebClient BASE_URL will use (
https://www.slack.com/api/
).proxy (str | None) – Proxy to make the Slack API call.
retry_handlers (list[slack_sdk.http_retry.RetryHandler] | None) – List of handlers to customize retry logic in
slack_sdk.WebClient
.
- call(api_method, **kwargs)[source]¶
Calls Slack WebClient WebClient.api_call with given arguments.
- Parameters
api_method (str) – The target Slack API method. e.g. ‘chat.postMessage’. Required.
http_verb – HTTP Verb. Optional (defaults to ‘POST’)
files – Files to multipart upload. e.g. {imageORfile: file_objectORfile_path}
data – The body to attach to the request. If a dictionary is provided, form-encoding will take place. Optional.
params – The URL parameters to append to the URL. Optional.
json – JSON for the body to attach to the request. Optional.
- Returns
The server’s response to an HTTP request. Data from the response can be accessed like a dict. If the response included ‘next_cursor’ it can be iterated on to execute subsequent requests.
- Return type
slack_sdk.web.slack_response.SlackResponse
- send_file(*, channels=None, file=None, content=None, filename=None, filetype=None, initial_comment=None, title=None)[source]¶
Create or upload an existing file.
- Parameters
channels (str | Sequence[str] | None) – Comma-separated list of channel names or IDs where the file will be shared. If omitting this parameter, then file will send to workspace.
file (str | pathlib.Path | None) – Path to file which need to be sent.
content (str | None) – File contents. If omitting this parameter, you must provide a file.
filename (str | None) – Displayed filename.
filetype (str | None) – A file type identifier.
initial_comment (str | None) – The message text introducing the file in specified
channels
.title (str | None) – Title of file.
See also