airflow.providers.slack.hooks.slack¶
Hook for Slack.
Classes¶
Represents the structure of the file upload data. |
|
Creates a Slack API Connection to be used for calls. |
Module Contents¶
- class airflow.providers.slack.hooks.slack.FileUploadTypeDef[source]¶
Bases:
TypedDictRepresents the structure of the file upload data.
- Variables:
file – Optional. Path to file which need to be sent.
content – Optional. File contents. If omitting this parameter, you must provide a file.
filename – Optional. Displayed filename.
title – Optional. The title of the uploaded file.
alt_txt – Optional. Description of image for screen-reader.
snippet_type – Optional. Syntax type of the snippet being uploaded.
- 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.providers.common.compat.sdk.BaseHookCreates 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.
- 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!")
Additional arguments which are not listed into parameters exposed into the rest of
slack.WebClientconstructor args.- 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]¶
Call 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
- async async_call(api_method, **kwargs)[source]¶
Call Slack WebClient AsyncWebClient.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.async_client.AsyncSlackResponse
- send_file_v2(*, channel_id=None, file_uploads, initial_comment=None, thread_ts=None)[source]¶
Send one or more files to a Slack channel using the Slack SDK Client method files_upload_v2.
- Parameters:
channel_id (str | None) – The ID of the channel to send the file to. If omitting this parameter, then file will send to workspace.
file_uploads (FileUploadTypeDef | list[FileUploadTypeDef]) – The file(s) specification to upload.
initial_comment (str | None) – The message text introducing the file in specified
channel.thread_ts (str | None) – Provide another message’s
tsvalue to upload the file as a reply in a thread. See https://api.slack.com/messaging#threading.
- send_file_v1_to_v2(*, channels=None, file=None, content=None, filename=None, initial_comment=None, title=None, snippet_type=None, thread_ts=None)[source]¶
Smooth transition between
send_fileandsend_file_v2methods.- Parameters:
channels (str | collections.abc.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 would be uploaded for each channel individually.
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.
initial_comment (str | None) – The message text introducing the file in specified
channels.title (str | None) – Title of the file.
snippet_type (str | None) – Syntax type for the content being uploaded.
thread_ts (str | None) – Provide another message’s
tsvalue to upload the file as a reply in a thread. See https://api.slack.com/messaging#threading.
- get_channel_id(channel_name)[source]¶
Retrieve a Slack channel id by a channel name.
It continuously iterates over all Slack channels (public and private) until it finds the desired channel name in addition cache results for further usage.
- Parameters:
channel_name (str) – The name of the Slack channel for which ID has to be found.