Skip to main content

Prefect tasks and subflows for interacting with Slack

Project description

Reliably send Slack messages with prefect-slack


PyPI

Visit the full docs here to see additional examples and the API reference.

prefect-slack is a collection of prebuilt Prefect integrations that can be used to interact with Slack.

Getting Started

Saving credentials to block

In order to use tasks in the collection, you'll first need to create a Slack app and install it in your Slack workspace.

You can create a Slack app by navigating to the apps page for your Slack account and selecting Create New App.

Slack's Basic app setup guide provides additional details on setting up a Slack app.

=== "Webhook"

For integrations that require a Webhook URL, you can generate new Webhook URLs by navigating to your app's __Incoming Webhooks__ page.

Click __Add New Webhook to Workspace__ and copy the webhook URL, formatted like `https://hooks.slack.com/services/...`.

Then, create and run a short script, replacing the placeholders.

```python
from prefect_slack import SlackWebhook

SlackWebhook(url="WEBHOOK_URL_PLACEHOLDER").save("BLOCK-NAME-PLACEHOLDER")
```

Congrats! You can now easily load the saved block, which holds your credentials:

```python
from prefect_slack import SlackWebhook

SlackWebhook.load("BLOCK-NAME-PLACEHOLDER")
```

=== "OAuth"

For integrations that require a Bot user OAuth token, you can get a token for your app by navigating to your app's __OAuth & Permissions__ page.

Locate __Bot User OAuth Token__ and copy the token, formatted like `xoxb-...`.

Then, create and run a short script, replacing placeholders.

```python
from prefect_slack import SlackCredentials

SlackCredentials(token="TOKEN-PLACEHOLDER").save("BLOCK-NAME-PLACEHOLDER")
```

Next, update the scope to include `chat:write`, which will prompt you to reinstall the app.

Congrats! You can now easily load the saved block, which holds your credentials:

```python
from prefect_slack import SlackCredentials

SlackCredentials.load("BLOCK-NAME-PLACEHOLDER")
```

!!! info "Unsure whether to authenticate with Webhook or OAuth?"

Webhook requires slightly less configuration and is limited to a single channel, which makes it suitable for getting started.

Integrate with Prefect flows

prefect-slack makes sending Slack messages effortless, giving you peace of mind that your messages are being sent as expected.

First, install prefect-slack and save your Slack credentials to a block to run the examples below!

=== "Webhook"

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

@flow
def example_slack_send_message_flow():
    slack_webhook = SlackWebhook.load("BLOCK-NAME-PLACEHOLDER")
    result = send_incoming_webhook_message(
        slack_webhook=slack_webhook,
        text="This proves send_incoming_webhook_message works!",
    )
    return result

example_slack_send_message_flow()
```

Outputs:
```bash
16:30:47.101 | INFO    | prefect.engine - Created flow run 'scrupulous-avocet' for flow 'example-slack-send-message-flow'
16:30:48.389 | INFO    | Flow run 'scrupulous-avocet' - Created task run 'send_incoming_webhook_message-a90deb5e-0' for task 'send_incoming_webhook_message'
16:30:48.391 | INFO    | Flow run 'scrupulous-avocet' - Executing 'send_incoming_webhook_message-a90deb5e-0' immediately...
16:30:48.861 | INFO    | Task run 'send_incoming_webhook_message-a90deb5e-0' - Posting message to provided webhook
16:30:49.390 | INFO    | Task run 'send_incoming_webhook_message-a90deb5e-0' - Finished in state Completed()
16:30:49.571 | INFO    | Flow run 'scrupulous-avocet' - Finished in state Completed('All states completed.')
```

=== "OAuth"

```python
from prefect import flow
from prefect_slack import SlackCredentials
from prefect_slack.messages import send_chat_message

@flow
def example_slack_send_message_flow():
    slack_credentials = SlackCredentials.load("BLOCK-NAME-PLACEHOLDER")
    result = send_chat_message(
        slack_credentials=slack_credentials,
        text="This proves send_chat_message works!",
        channel="CHANNEL-NAME-PLACEHOLDER",
    )
    return result

example_slack_send_message_flow()
```

Outputs:

```bash
16:28:04.294 | INFO    | prefect.engine - Created flow run 'resourceful-koala' for flow 'example-slack-send-message-flow'
16:28:05.675 | INFO    | Flow run 'resourceful-koala' - Created task run 'send_chat_message-0403e84a-0' for task 'send_chat_message'
16:28:05.678 | INFO    | Flow run 'resourceful-koala' - Executing 'send_chat_message-0403e84a-0' immediately...
16:28:06.160 | INFO    | Task run 'send_chat_message-0403e84a-0' - Posting chat message to testing-slack
16:28:06.674 | INFO    | Task run 'send_chat_message-0403e84a-0' - Finished in state Completed()
16:28:06.848 | INFO    | Flow run 'resourceful-koala' - Finished in state Completed()
```

Capture exceptions and notify by Slack message

Perhaps you want a Slack notification with the details of the exception when your flow run fails.

prefect-slack can be wrapped in an except statement to do just that!

from prefect import flow
from prefect.context import get_run_context
from prefect_slack import SlackWebhook

def notify_exc_by_slack(exc):
    context = get_run_context()
    flow_run_name = context.flow_run.name
    slack_webhook = SlackWebhook.load("BLOCK-NAME-PLACEHOLDER")
    slack_webhook.notify(body=f"Flow run {flow_run_name!r} failed due to {exc}.")

@flow
def example_flow():
    try:
        1 / 0
    except Exception as exc:
        notify_exc_by_slack(exc)
        raise

example_flow()

Resources

For more tips on how to use tasks and flows in a Collection, check out Using Collections!

Installation

Install prefect-slack:

pip install prefect-slack

Requires an installation of Python 3.7+.

We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.

These tasks are designed to work with Prefect 2. For more information about how to use Prefect, please refer to the Prefect documentation.

Feedback

If you encounter any bugs while using prefect-slack, feel free to open an issue in the prefect-slack repository.

If you have any questions or issues while using prefect-slack, you can find help in either the Prefect Discourse forum or the Prefect Slack community.

Feel free to star or watch prefect-slack for updates too!

Contributing

If you'd like to help contribute to fix an issue or add a feature to prefect-slack, please propose changes through a pull request from a fork of the repository.

Here are the steps:

  1. Fork the repository
  2. Clone the forked repository
  3. Install the repository and its dependencies:
pip install -e ".[dev]"
  1. Make desired changes
  2. Add tests
  3. Insert an entry to CHANGELOG.md
  4. Install pre-commit to perform quality checks prior to commit:
pre-commit install
  1. git commit, git push, and create a pull request

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prefect-slack-0.2.0.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prefect_slack-0.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file prefect-slack-0.2.0.tar.gz.

File metadata

  • Download URL: prefect-slack-0.2.0.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for prefect-slack-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8b8382fe57d6e51c2184b72a8fd2a5074bfce649d6f6dbe85ce1e1270678c111
MD5 90afbcbfdf45a80ec21f330debf47e5b
BLAKE2b-256 9b138ff0459214fa03d7edb625912da8597ebb3866cf78644f2e0df9716d9f61

See more details on using hashes here.

File details

Details for the file prefect_slack-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: prefect_slack-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for prefect_slack-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f2a785c87e67e62a6a5274fd1316f3cab18e8c4e11a5bc2aa81b57ec18c628b
MD5 7b37307d47fe63ef58f7353653d63450
BLAKE2b-256 1c1041f249d728431f1d09a8fe70f366a22b07189a3a2dd99e8553bbcd68bdda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page