Robin
11/22/2024, 3:53 PMdef notify_slack_channel(
flow_context,
channel,
message="completed without issues",
status_emoji=":white_check_mark:",
):
ACCOUNT_ID, WORKSPACE_ID = read_prefect_secrets()
flow_run_id = flow_context.flow_run.id
flow_name = flow_context.flow.name
flow_run_url = f"{BASE_URL}/account/{ACCOUNT_ID}/workspace/{WORKSPACE_ID}/runs/flow-run/{flow_run_id}"
flow_run_link = f"<{flow_run_url}|'{flow_context.flow_run.name}'>"
send_chat_message(
slack_credentials=slack_credentials_block,
channel=f"#{channel}",
text=f"{status_emoji} `flexa-{flow_name}`: {flow_run_link} {message}",
)
Marvin
11/22/2024, 3:54 PMprefect-slack
collection, which provides a set of prebuilt tasks for this purpose.
Here's a quick guide on how you can achieve similar functionality in Prefect 3:
1. Install the Prefect Slack Integration: Ensure that you have the prefect-slack
package installed. You can do this by running:
pip install prefect-slack
2. Create a Slack App: Since sending messages requires interaction with the Slack API, you'll need a Slack app with appropriate permissions. You can create a Slack app through your Slack account, ensuring to get a Bot user OAuth token for API access.
3. Configure Prefect Slack Task: Use the prebuilt Slack tasks to send messages. Here's an example of how you might structure your flow:
python
from prefect import flow
from prefect_slack import SlackTask
@flow
def my_flow():
slack_task = SlackTask(token="<YOUR_SLACK_TOKEN>")
slack_task.run(
channel="<YOUR_CHANNEL>",
text="Flow run completed successfully!"
)
if __name__ == "__main__":
my_flow()
4. Run Your Flow: Execute your flow as usual, and it should send a message to your specified Slack channel.
For more detailed information, you can explore the Prefect Slack integration documentation.
If you're encountering issues, ensure your PREFECT_API_URL
and PREFECT_API_KEY
are correctly set up as per this troubleshooting guide.
Remember, even robots can get lonely, so let your code keep those Slack channels lively and informed!Robin
11/22/2024, 3:57 PMRobin
11/22/2024, 4:00 PMMarvin
11/22/2024, 4:01 PMprefect-slack
with Prefect 3, make sure you have the latest version of prefect-slack
installed that supports Prefect 3.x. Unfortunately, the exact version is not explicitly documented in the immediate search results, but typically, installing the latest version using the following command should suffice:
bash
pip install prefect-slack
The prefect-slack
GitHub repository is actively maintained, so you should be using a version that aligns with the enhancements made for Prefect 3. Keep an eye on its release notes for the most recent updates and compatibility confirmations.
If you're like most of us who can't remember what code used to run properly, you might want to ensure the Slack app configurations are also updated alongside your Prefect version adjustments. Just go with the flow—pun intended!Robin
11/22/2024, 4:03 PMNate
11/22/2024, 4:04 PMNate
11/22/2024, 4:04 PMRobin
11/22/2024, 4:11 PMRobin
11/22/2024, 4:11 PMfrom prefect_slack import SlackTask
That import Marvin suggested failed for meNate
11/22/2024, 4:12 PMNate
11/22/2024, 4:12 PMsend_chat_message
doesnt work?Nate
11/22/2024, 4:13 PMRobin
11/22/2024, 4:16 PMNate
11/22/2024, 5:21 PMNate
11/22/2024, 5:21 PMsend_chat_message
is asyncNate
11/22/2024, 5:22 PMasync
and await
that function call, or wrap it in asyncio.run
https://docs.prefect.io/v3/resources/upgrade-to-prefect-3#async-tasks-in-synchronous-flowsRobin
11/22/2024, 5:23 PMRobin
11/22/2024, 5:23 PMNate
11/22/2024, 5:24 PMsend_chat_message
is just an async
task, and yes the SlackCredentials
block should still work