Send Slack notifications

Setting up a Slack Webhook integration

Create a Webhook Integration in Slack

Navigate to the Slack channel where you want to send notifications to. Go to Settings > Integrations and create a new automation.

Slack integrations settings.

Slack integrations settings.

Add a Webhook and Variables

Select From a webhook and define variables for your messages. Slack provides an example payload to guide the structure of your request.

Slack integration webhook configuration.

Slack integration webhook configuration.

Compose Your Message

Choose the option to send a message to a channel. Use the previously defined variables to create dynamic messages.

Slack message definition user interface.

Slack message definition user interface.

Copy the Webhook URL

Access this by selecting the <> starts with a webhook step. You will use this URL in subsequent steps to integrate with Y42.

Slack web request URL link with copy button.

Slack web request URL link with copy button.

Create a Python Action for Slack notifications

Create a Python Action asset with the following script:

send-slack-notification.py

_32
import json
_32
import pandas as pd
_32
_32
import requests
_32
_32
from y42.v1.decorators import data_action
_32
import logging
_32
_32
# use the @data_action decorator to trigger actions in third party systems
_32
# make sure you have a corresponding .yml exposure definition matching the function's name
_32
# for more information check out our docs: https://www.y42.com/docs/python-actions
_32
_32
@data_action
_32
def business_alert(context,assets):
_32
# Define the Slack webhook URL to which the notification will be sent.
_32
webhook_url = "https://hooks.slack.com/triggers/TQABC123DEF456"
_32
_32
# Reference the 'mrt_completed_orders' dataset from Y42 assets.
_32
purchases = assets.ref('mrt_completed_orders')
_32
_32
# Business logic:
_32
# Filter for high revenue items, defined as those with a total amount greater than 10,000.
_32
high_revenue_items = purchases[purchases['amount_total'] > 10000]
_32
_32
# Check if there are any high revenue items. If so, proceed with the notification.
_32
if not high_revenue_items.empty:
_32
# Send a POST request to the Slack webhook URL with the message payload.
_32
response = requests.post(webhook_url, json={"key_variable_defined_for_automation": "Hello from Y42"}, headers={'Content-Type': 'application/json'})
_32
_32
# Log the response status code and headers for debugging purposes.
_32
logging.info(response.status_code)
_32
logging.info(response.headers)

This script triggers when the mrt_completed_orders asset successfully executes.

Ensure dependencies (i.e. assets refereneced in the script) are synced and included in the Y42 exposure file.

Sync dependencies

Sync dependencies

Commit changes & trigger the sync

Save your changes by committing them. You can build the asset using dag selectors or via the Build history tab.

trigger_exposure

_10
y42 build -s +exposure:send-slack-notification

The + selector, when used in front of the asset name in the command, also triggers all upstream dependencies.

For comprehensive guidance on how to set up Python Action assets, including secret management, check our main documentation page on Python action assets.