Hey <@ULVA73B9P>, I implemented below function in ...
# ask-marvin
r
Hey @Marvin, I implemented below function in prefect 2 to send messages to slack. However, they don't work anymore in prefect 3. How can I send slack messages from flow context in prefect 3?
Copy code
def 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}",
    )
m
To send messages to Slack from within Prefect 3, you'll want to utilize the
prefect-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:
Copy code
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:
Copy code
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!
r
Does prefect 3 not support slack blocks anymore?
Which version of prefect-slack needs to be installed for prefect 3, @Marvin?
m
To use
prefect-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:
Copy code
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!
r
@Nate or so, this doesn't seem right
n
this should be right
what problem are you seeing?
r
the slack code that worked in prefect 2 seems to not work anymore in prefect 3
from prefect_slack import SlackTask
That import Marvin suggested failed for me
n
yeah that appears to be a hallucination on marvin's part
so you're saying that
send_chat_message
doesnt work?
how exactly it fails and better yet an MRE would be helpful
r
slack.py
n
ah
so
send_chat_message
is async
so you should make the function
async
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-flows
💡 1
r
OK, I tried that but wasn't successful, will go down deeper that road, thanks!
So the slack block should still work?
n
send_chat_message
is just an
async
task, and yes the
SlackCredentials
block should still work
👍 1