<@ULVA73B9P> is this valid template for slack noti...
# ask-marvin
s
@Marvin is this valid template for slack notification payload { “blocks”: [ { “type”: “divider” } ], “attachments”: [ { “color”: “{% if flow_run.state.name == ‘Completed’ %}good{% else %}danger{% endif %}“, “blocks”: [ { “type”: “header”, “text”: { “type”: “plain_text”, “text”: “{{ flow.name|upper }} - {{ flow_run.state.name|upper }}” } }, { “type”: “section”, “text”: { “type”: “mrkdwn”, “text”: “{{ flow_run.state.message }}” } } ] } ] }
m
thought for 201.4 seconds
Short answer: almost — but a few important caveats. What’s okay: - Slack supports both top-level
blocks
and
attachments
(and
attachments
may contain their own
blocks
). Using
color
with values like `good`/`danger` is valid for attachments. - Your Block Kit structure (divider, header, section) looks fine. What needs fixing: - The quotes in your snippet are “smart quotes” (curly). Slack expects valid JSON with straight quotes. Replace all “ ” and ‘ ’ with " and '. Potential issue depending on how you send it from Prefect: - If you’re using Prefect’s built-in Slack Notification block (SlackWebhook), it sends simple messages (subject/body), not arbitrary JSON payloads. To post raw Block Kit JSON, use a Custom Webhook pointing at a Slack Incoming Webhook, or the prefect-slack integration. - The Jinja variables you’re using (
flow_run
,
flow
) will only render if the context is provided by the sender. In Prefect 3.x, Automations for flow-run events generally provide
flow_run
,
flow
, and
deployment
in the template context; a standalone block call may not. Please confirm how you’re sending this (Automation vs. a block in code) so I can tailor the exact template. Here’s your payload corrected to valid JSON with straight quotes (structure-wise valid for Slack Incoming Webhooks), keeping your Jinja logic intact:
Copy code
{
  "blocks": [
    {
      "type": "divider"
    }
  ],
  "attachments": [
    {
      "color": "{% if flow_run.state.name == 'Completed' %}good{% else %}danger{% endif %}",
      "blocks": [
        {
          "type": "header",
          "text": {
            "type": "plain_text",
            "text": "{{ flow.name|upper }} - {{ flow_run.state.name|upper }}"
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "{{ flow_run.state.message }}"
          }
        }
      ]
    }
  ]
}
Follow-ups to make sure this works for you: - How are you sending this from Prefect? (Automation action, Custom Webhook block to an Incoming Webhook URL, or the Slack Notification block?) - If it’s an Automation, which event are you triggering on? I can confirm the exact context variables available. References: - Slack Incoming Webhooks and message payloads: Slack webhooks - Block Kit reference: Block types and Attachments - Prefect 3.x notification blocks: Prefect notification blocks - Prefect Slack integration (for richer Slack interactions): prefect-slack integration
s
@Marvin sending it as automation action on flow run completion or failure
m
thought for 169.8 seconds
Got it — since you’re sending this from a Prefect Cloud Automation on flow-run completion/failure: - Your JSON is valid Slack Block Kit payload (once you replace the curly quotes with straight quotes), including mixing top-level blocks with attachments->blocks. - However, it will only work if the automation posts raw JSON to a Slack Incoming Webhook. The “Send Slack notification” action (Slack Notification block) sends plain text (subject/body), not arbitrary JSON. Two ways to make this work: 1) Use a Custom Webhook/Call Webhook action to post JSON to Slack - Point it at your Slack Incoming Webhook URL. - Paste your JSON payload as the request body. - Jinja templating in Automations supports
flow_run
,
flow
, and
deployment
when the trigger is a flow-run event, so your
{{ flow.* }}
and
{{ flow_run.* }}
references should render. - This is the recommended path for Block Kit. 2) If you use the “Send Slack notification” action - It only supports a text message (no Block Kit/attachments). You’d need to convert to plain text. Here’s your payload corrected to valid JSON with straight quotes (works for a webhook action):
Copy code
{
  "blocks": [
    {
      "type": "divider"
    }
  ],
  "attachments": [
    {
      "color": "{% if flow_run.state.name == 'Completed' %}good{% else %}danger{% endif %}",
      "blocks": [
        {
          "type": "header",
          "text": {
            "type": "plain_text",
            "text": "{{ flow.name|upper }} - {{ flow_run.state.name|upper }}"
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "{{ flow_run.state.message }}"
          }
        }
      ]
    }
  ]
}
Quick check: - Variables available for flow-run triggers typically include
flow_run
(name, parameters, state, tags, id, etc.),
flow
, and
deployment
. - Mixing top-level
blocks
with
attachments
containing their own
blocks
is allowed by Slack. Can you confirm which automation action you’re using: “Send Slack notification” or a webhook-based action (e.g., “Call a webhook” / Custom Webhook)? I can give you exact UI steps for that path. References: - Prefect Automations overview: Automations in Prefect 3 - Notification blocks: Notification blocks API - Slack Incoming Webhooks: Slack webhooks - Block Kit reference: Block Kit and Attachments
s
@Marvin I’m using call a webhook action
m
thought for 27.7 seconds
Perfect — using Call a Webhook is the right choice for Block Kit. What to configure in the automation action: - URL: your Slack Incoming Webhook URL - Method: POST - Headers: Content-Type: application/json - Body: your JSON payload with straight quotes and Jinja variables Your template will work with flow-run triggers;
flow_run
,
flow
, and
deployment
are available in the template context. Minor improvements: - Include a top-level
text
as a fallback so mobile/notifications show something even if blocks don’t render. - Default the state message to empty to avoid “null” if there’s no message. Drop-in payload you can use:
Copy code
{
  "text": "{{ flow.name }} - {{ flow_run.state.name }}",
  "blocks": [
    {
      "type": "divider"
    }
  ],
  "attachments": [
    {
      "color": "{% if flow_run.state.name == 'Completed' %}good{% else %}danger{% endif %}",
      "blocks": [
        {
          "type": "header",
          "text": {
            "type": "plain_text",
            "text": "{{ flow.name|upper }} - {{ flow_run.state.name|upper }}"
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "{{ flow_run.state.message | default('') }}"
          }
        }
      ]
    }
  ]
}
Notes: - Mixing top-level
blocks
and
attachments
with their own
blocks
is supported by Slack. - Make sure all quotes are straight quotes. Docs for reference: - Prefect Automations: Automations in Prefect 3 - Slack Incoming Webhooks: Slack webhooks - Block Kit reference: Block types and Attachments