airflow.providers.slack.transfers.sql_to_slack_webhook

Module Contents

Classes

SqlToSlackWebhookOperator

Executes an SQL statement in a given SQL connection and sends the results to Slack Incoming Webhook.

class airflow.providers.slack.transfers.sql_to_slack_webhook.SqlToSlackWebhookOperator(*, sql, sql_conn_id, slack_webhook_conn_id=None, sql_hook_params=None, slack_channel=None, slack_message, results_df_name='results_df', parameters=None, slack_conn_id=NOTSET, **kwargs)[source]

Bases: airflow.providers.slack.transfers.base_sql_to_slack.BaseSqlToSlackOperator

Executes an SQL statement in a given SQL connection and sends the results to Slack Incoming Webhook.

The results of the query are rendered into the ‘slack_message’ parameter as a Pandas dataframe using a JINJA variable called ‘{{ results_df }}’. The ‘results_df’ variable name can be changed by specifying a different ‘results_df_name’ parameter. The Tabulate library is added to the JINJA environment as a filter to allow the dataframe to be rendered nicely. For example, set ‘slack_message’ to {{ results_df | tabulate(tablefmt=”pretty”, headers=”keys”) }} to send the results to Slack as an ascii rendered table.

See also

For more information on how to use this operator, take a look at the guide: SqlToSlackWebhookOperator

Note

You cannot override the default channel (chosen by the user who installed your app), Instead, these values will always inherit from the associated Slack App configuration (link). It is possible to change this values only in Legacy Slack Integration Incoming Webhook.

Warning

This hook intend to use Slack Incoming Webhook connection and might not work correctly with Slack API connection.

Parameters
  • sql (str) – The SQL query to be executed (templated)

  • slack_message (str) – The templated Slack message to send with the data returned from the SQL connection. You can use the default JINJA variable {{ results_df }} to access the pandas dataframe containing the SQL results

  • sql_conn_id (str) – reference to a specific database.

  • sql_hook_params (dict | None) – Extra config params to be passed to the underlying hook. Should match the desired hook constructor params.

  • slack_webhook_conn_id (str | None) – Slack Incoming Webhook connection id that has Incoming Webhook token in the password field.

  • slack_channel (str | None) – The channel to send message.

  • results_df_name (str) – The name of the JINJA template’s dataframe variable, default is ‘results_df’

  • parameters (list | tuple | Mapping[str, Any] | None) – The parameters to pass to the SQL query

property slack_conn_id[source]
template_fields: Sequence[str] = ('sql', 'slack_message')[source]
template_ext: Sequence[str] = ('.sql', '.jinja', '.j2')[source]
template_fields_renderers[source]
times_rendered = 0[source]
render_template_fields(context, jinja_env=None)[source]

Template all attributes listed in self.template_fields.

This mutates the attributes in-place and is irreversible.

Parameters
  • context – Context dict with values to apply on content.

  • jinja_env – Jinja’s environment to use for rendering.

execute(context)[source]

Derive when creating an operator.

Context is the same dictionary used as when rendering jinja templates.

Refer to get_template_context for more context.

Was this entry helpful?