Luke He
06/28/2024, 6:17 PMMarvin
06/28/2024, 6:17 PMCreate Notification
or use the +
button.
2. Setting up the Notification:
- Choose which run states should trigger a notification.
- Optionally, add tags to filter which flow runs are covered by the notification.
- Choose to send a Slack message and provide the necessary webhook credentials for your Slack workspace.
3. Customizing the Message:
- You can customize the Slack message to include the user tag. In Slack, tagging a user involves including their handle in the message. For example, to tag a user named @username
, your message would include Hello <@username>
.
### Example Code Snippet
Below is an example Prefect automation script that sends a Slack notification and tags a specific user:
python
from prefect import task, flow
from prefect.notifications import SlackWebhook
@task
def example_task():
return "The flow run is complete."
@flow
def example_flow():
task_result = example_task()
webhook = SlackWebhook(url="YOUR_SLACK_WEBHOOK_URL")
webhook.notify(message=f"Task result: {task_result}\n\nHello <@username>, please check the task status.")
if __name__ == "__main__":
example_flow()
### References and Useful Links
- Prefect Documentation: Notifications configuration
- GitHub Issue on Notifications Metadata: Better management of Notifications in Cloud 2.0 UI
That should cover the basics. For more specific configurations, please refer directly to the Prefect documentation.Nate
06/28/2024, 6:38 PMHello @usernamei dont think this actually works you need the user id im pretty sure
Nate
06/28/2024, 6:39 PMwebhook.notify(message=f"Task result: {task_result}\n\nHello <@{user_id}>, please check the task status.")
Luke He
06/28/2024, 6:42 PMLuke He
06/28/2024, 6:43 PMNate
06/28/2024, 7:04 PMprefect_slack
it works
In [7]: from prefect_slack import SlackWebhook
...: slack_webhook_block = SlackWebhook.load("pager")
In [8]: slack_webhook_block.notify("Hello from Prefect! <@U03RX2A8LK0>")
Nate
06/28/2024, 7:04 PMprefecthq/prefect
is using apprise and they dont seem to pass it through how slack needs itLuke He
06/28/2024, 7:15 PMNate
06/28/2024, 7:19 PMSlackWebhook
is from prefect_slack
• a different SlackWebhook
is from prefect
(the one you tried)
https://github.com/search?q=repo%3APrefectHQ%2Fprefect%20SlackWebhook&type=code
are currently 2 different block types, both of which you can find / create in the UI. the one from prefect_slack
was implemented using the slack sdk, so it supports the user mentioning syntax above, whereas the apprise implementation in core doesnt
going forward, i am going to remove the one from core in prefect 3.x so that its less confusing and because the one from prefect_slack
is betterNate
06/28/2024, 7:20 PMNotificationBlock
types, so they can be used by automations to send notification actionsLuke He
06/28/2024, 7:53 PMLuke He
06/28/2024, 7:55 PMNate
06/28/2024, 8:10 PMthe "Incoming Webhook" doesn't seem to successfully send notifications via an automationcan you share what went wrong?
Luke He
06/28/2024, 8:16 PMLuke He
06/28/2024, 8:17 PMNate
06/28/2024, 8:21 PMprefect-cloud.automation.action.failed
in your Event Feed?Nate
06/28/2024, 8:22 PMprefect_slack
version of the block serversideLuke He
06/28/2024, 8:24 PM{
"id": "6f45b44e-435f-4263-9441-f8ce94f8ae81",
"account": "97028b81-bea9-458e-a561-24b4031e349c",
"event": "prefect-cloud.automation.action.failed",
"occurred": "2024-06-28T20:07:59.767Z",
"payload": {
"action_index": 0,
"action_type": "send-notification",
"invocation": "1719fb87-2a08-4de1-9ca3-94532f363ba0",
"reason": "The notification block was invalid: KeyError(\"No class found for dispatch key 'slack-incoming-webhook' in registry for type 'Block'.\")"
},
"received": "2024-06-28T20:07:59.768Z",
"related": [],
"resource": {
"prefect.resource.id": "prefect-cloud.automation.c2ec2959-fd53-49e9-90f0-28e73418bbb5",
"prefect.resource.name": "Failure/Paused Notifications",
"prefect-cloud.trigger-type": "event",
"prefect-cloud.posture": "Reactive"
},
"workspace": "afa27e96-6a02-402f-9ad7-bffbcf577974"
}
Nate
06/28/2024, 8:27 PMSlackWebhook
from core registered (which is why the message goes through in that case)
so right now, it might not be directly possible to tag a user in your automation action with the SlackWebhook
from prefect
, because apprise
is mutating that message somewhere on the way to the slack API, sorry about that
I will make an issue to install the one from prefect_slack
(that works as desired) server side so it can be used in automation actionsLuis Diaz
06/28/2024, 8:29 PMLuke He
06/28/2024, 8:31 PMNate
06/28/2024, 8:31 PM