<@ULVA73B9P> how do you tag users when using autom...
# ask-marvin
l
@Marvin how do you tag users when using automations to send Slack notifications?
m
To tag users in Slack notifications when using Prefect automations, you can follow these general steps: ### 1. Setting Up Slack Webhook Firstly, ensure you have your Slack webhook URL. This can be set up in the Slack app settings and is required for sending messages to specific Slack channels. ### 2. Configuring Notification in Prefect 1. Navigating to Notifications: - Go to the Notification page on your Prefect Cloud or Prefect Server. - Select
Create 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:
Copy code
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.
n
Hello @username
i dont think this actually works you need the user id im pretty sure
i think its like
Copy code
webhook.notify(message=f"Task result: {task_result}\n\nHello <@{user_id}>, please check the task status.")
l
I was trying to do it by triggering slack notifications via Prefect Automations but it seems that it converts Slack tags to plain text
n
aha okay if you use the one from
prefect_slack
it works
Copy code
In [7]: from prefect_slack import SlackWebhook
   ...: slack_webhook_block = SlackWebhook.load("pager")

In [8]: slack_webhook_block.notify("Hello from Prefect! <@U03RX2A8LK0>")
the one in
prefecthq/prefect
is using apprise and they dont seem to pass it through how slack needs it
l
Sorry, are you saying that I'd need to do tags in Slack Pythonically rather than through the Prefect Cloud automation?
n
sorry, no. im saying that: •
SlackWebhook
is from
prefect_slack
• a different
SlackWebhook
is from
prefect
(the one you tried) https://github.com/search?q=repo%3APrefectHQ%2Fprefect%20SlackWebhook&amp;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 better
currently, both block types are
NotificationBlock
types, so they can be used by automations to send notification actions
l
we should be using the "Slack Incoming Webhook"? the one that prints tags as plaintext was the "Slack Webhook" block type
because the "Incoming Webhook" doesn't seem to successfully send notifications via an automation
n
the "Incoming Webhook" doesn't seem to successfully send notifications via an automation
can you share what went wrong?
l
I made a new webhook and created a Slack Incoming Webhook block with it; the automation itself is just when a flow run enters a fail state:
When I replaced our the webhook within our old Slack webhook block with this newly created webhook, and shifted the auomation to send a notification to the old block again, the message did go through again
n
hmm do you have an
prefect-cloud.automation.action.failed
in your Event Feed?
there might be a bug to fix when using the
prefect_slack
version of the block serverside
l
oh interesting yes there is, these correspond with the times of my testing; here's the raw JSON of the failed automation event:
Copy code
{
  "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"
}
n
> "The notification block was invalid: KeyError(\"No class found for dispatch key 'slack-incoming-webhook' in registry for type 'Block'.\")" okay, this looks like a bug. to me this says "Prefect Cloud doesnt have this block type registered", but we do have the
SlackWebhook
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 actions
l
Thanks for this bringing this up Luke. Put your name in for the bug bounty haha
l
lol thanks @Nate it's not a big deal for us right now; I was just worried I was missing something obvious, but good to know!
n
thanks for the report and additional info!