Skip to content

Examples Catalog

Below is a list of examples for prefect-slack.

Credentials Module

Load stored Slack webhook:

from prefect_slack import SlackWebhook
slack_webhook_block = SlackWebhook.load("BLOCK_NAME")

Get a Slack webhook client:

from prefect_slack import SlackWebhook
slack_webhook_block = SlackWebhook.load("BLOCK_NAME")
client = slack_webhook_block.get_client()

Send a notification in Slack:

from prefect_slack import SlackWebhook
slack_webhook_block = SlackWebhook.load("BLOCK_NAME")
slack_webhook_block.notify("Hello, world!")
Load stored Slack credentials:
from prefect_slack import SlackCredentials
slack_credentials_block = SlackCredentials.load("BLOCK_NAME")

Get a Slack client:

from prefect_slack import SlackCredentials
slack_credentials_block = SlackCredentials.load("BLOCK_NAME")
client = slack_credentials_block.get_client()

Messages Module

Post a message at the end of a flow run.

from prefect import flow
from prefect.context import get_run_context
from prefect_slack import SlackCredentials
from prefect_slack.messages import send_chat_message


@flow
def example_send_message_flow():
    context = get_run_context()

    # Run other tasks and subflows here

    token = "xoxb-your-bot-token-here"
    send_chat_message(
        slack_credentials=SlackCredentials(token),
        channel="#prefect",
        text=f"Flow run {context.flow_run.name} completed :tada:"
    )

example_send_message_flow()
Post a message at the end of a flow run.

from prefect import flow
from prefect_slack import SlackWebhook
from prefect_slack.messages import send_incoming_webhook_message


@flow
def example_send_message_flow():
    # Run other tasks and subflows here

    webhook_url = "https://hooks.slack.com/XXX"
    send_incoming_webhook_message(
        slack_webhook=SlackWebhook(
            url=webhook_url
        ),
        text="Warehouse loading flow completed :sparkles:"
    )

example_send_message_flow()