airflow.providers.slack.hooks.slack

Module Contents

Classes

FileUploadTypeDef

Represents the structure of the file upload data.

SlackHook

Creates a Slack API Connection to be used for calls.

class airflow.providers.slack.hooks.slack.FileUploadTypeDef[source]

Bases: TypedDict

Represents 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.

file: typing_extensions.NotRequired[str | None][source]
content: typing_extensions.NotRequired[str | None][source]
filename: typing_extensions.NotRequired[str | None][source]
title: typing_extensions.NotRequired[str | None][source]
alt_txt: typing_extensions.NotRequired[str | None][source]
snippet_type: typing_extensions.NotRequired[str | None][source]
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.

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.WebClient constructor 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.

conn_name_attr = 'slack_conn_id'[source]
default_conn_name = 'slack_api_default'[source]
conn_type = 'slack'[source]
hook_name = 'Slack API'[source]
client()[source]

Get the underlying slack_sdk.WebClient (cached).

get_conn()[source]

Get the underlying slack_sdk.WebClient (cached).

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

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.

send_file_v2(*, channel_id=None, file_uploads, initial_comment=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.

send_file_v1_to_v2(*, channels=None, file=None, content=None, filename=None, initial_comment=None, title=None, filetype=None)[source]

Smooth transition between send_file and send_file_v2 methods.

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 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.

  • filetype (str | None) – A file type identifier.

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.

test_connection()[source]

Tests the Slack API connection.

classmethod get_connection_form_widgets()[source]

Return dictionary of widgets to be added for the hook to handle extra values.

classmethod get_ui_field_behaviour()[source]

Return custom field behaviour.

Was this entry helpful?