https://prefect.io logo
Title
l

Luis Cebrián

03/23/2023, 3:28 PM
Hi, I would like to send a slack notification with some information about the result of a flow run together with a link to that flow run in the prefect server so that I can inspect the flow run if I want to. In prefect 1.0 I would do something like this within a task or a flow to get the url:
from prefect.client import Client
import prefect

flow_run_id = prefect.context.flow_run_id
url = Client().get_cloud_url("flow-run", flow_run_id)
I have not found anything similar in prefect 2.0. How can I do that in prefect 2.0?
f

Filip Panovski

03/23/2023, 3:31 PM
You should either be able to create a Slack Webhook notification in the UI (Notifications -> Create Notification -> Slack Webhook), or you can create a Slack Webhook Block and use that programmatically.
I'm currently using the block approach for a MS Teams webhook which looks like follows:
def _notify(status: PipelineStatus, pipeline: str = None, params: dict = None):
    pipeline = pipeline if pipeline else '<unnamed pipeline>'
    params = params if params else {}
    teams_webhook_block = MicrosoftTeamsWebhook.load("block-name")
    teams_webhook_block.notify(f'[{pipeline}] {status.name} with parameters: {params}')
Using the Slack block for notifying should be fairly similar.
Oh, I misread. You're asking something completely different 🤦
l

Luis Cebrián

03/23/2023, 3:34 PM
The problem is not sending the slack notification. The problem is sending the url of the flow run that just run in that notification.
f

Filip Panovski

03/23/2023, 3:37 PM
Perhaps
prefect.context.get_run_context().task_run.dict()
prefect.context.get_run_context().flow_run.dict()
might have the information you require. Specifically,
flow_run
should contain a
flow_id
. Have you tried looking at that?
l

Luis Cebrián

03/23/2023, 3:47 PM
Yes, I can get the flow_run_id from that, but there is nothing about the url.
We are also using the
on_completion
and
on_failure
callbacks outlined here: https://discourse.prefect.io/t/prefect-2-8-3-is-here-with-completion-and-failure-hooks-for-flows-and-tasks/2437
l

Luis Cebrián

03/27/2023, 2:18 PM
Thanks a lot @jack That thread and discourse post were very helpful. I got notifications working.
👍 1