Package: prefect-slack
Reliably send Slack messages with prefect-slackVisit the full docshereto see additional examples and the API reference.prefect-slackis a collection of prebuilt Prefect integrations that can be used to interact with Slack.Getting StartedSaving credentials to blockIn 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 theapps pagefor your Slack account and selectingCreate New App.Slack'sBasic app setupguide 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 flowsprefect-slackmakes sending Slack messages effortless, giving you peace of mind that your messages are being sent as expected.First, installprefect-slackandsave your Slack credentials to a blockto 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 messagePerhaps you want a Slack notification with the details of the exception when your flow run fails.prefect-slackcan be wrapped in anexceptstatement to do just that!fromprefectimportflowfromprefect.contextimportget_run_contextfromprefect_slackimportSlackWebhookdefnotify_exc_by_slack(exc):context=get_run_context()flow_run_name=context.flow_run.nameslack_webhook=SlackWebhook.load("BLOCK-NAME-PLACEHOLDER")slack_webhook.notify(body=f"Flow run{flow_run_name!r}failed due to{exc}.")@flowdefexample_flow():try:1/0exceptExceptionasexc:notify_exc_by_slack(exc)raiseexample_flow()ResourcesFor more tips on how to use tasks and flows in a Collection, check outUsing Collections!InstallationInstallprefect-slack:pipinstallprefect-slackRequires 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 thePrefect documentation.FeedbackIf you encounter any bugs while usingprefect-slack, feel free to open an issue in theprefect-slackrepository.If you have any questions or issues while usingprefect-slack, you can find help in either thePrefect Discourse forumor thePrefect Slack community.Feel free to star or watchprefect-slackfor updates too!ContributingIf you'd like to help contribute to fix an issue or add a feature toprefect-slack, pleasepropose changes through a pull request from a fork of the repository.Here are the steps:Fork the repositoryClone the forked repositoryInstall the repository and its dependencies:pip install -e ".[dev]"Make desired changesAdd testsInsert an entry toCHANGELOG.mdInstallpre-committo perform quality checks prior to commit:pre-commit installgit commit,git push, and create a pull request