Skip to content

Commit

Permalink
Add slack attachment template
Browse files Browse the repository at this point in the history
  • Loading branch information
rchapin committed Mar 14, 2024
1 parent d554e34 commit 4d7b06c
Showing 1 changed file with 259 additions and 0 deletions.
259 changes: 259 additions & 0 deletions templates/slack-legacy-colorbar.json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
{{- /* This slack template renders colored bars on the left-hand side of messages by "hacking" the
messages to add an attachment for each message that is to have a different colored bar. The
attachment element has been deprectated by Slack and could be removed at any time so use this
template at your own risk.

See Slack docs:
https://api.slack.com/messaging/webhooks
https://api.slack.com/block-kit

Documentation regarding this hack:
https://api.slack.com/messaging/attachments-to-blocks#direct_equivalents Search for "color" on that page.
https://api.slack.com/reference/messaging/attachments Search for "color" on this page too.
*/ -}}

{{- /*
Slack:
0) if explicit to do digest, do that
1) if one custom insight, display with old style (like alarms)
2) if only insights, display as a digest
*/ -}}
{{- if or .Config.IsChannelDigest .IsMultipleEvents -}}
{
"blocks": [
{
"type": "divider"
},
{
"type": "header",
"text": {
"type": "plain_text",
"emoji": true,
"text": "{{ $.Headline }}: {{ $.Summary }}"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "{{ $.Headline }} for *{{ $.CompanyName }}* sent on {{ $.NowDatetime }}"
}
]
},

{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": ":gear: Manage Notifications",
"emoji": true
},
"url": {{$.NotificationsSettingsURL | toJSON}}
}
]
}
],
"attachments": [
{{ range $index, $importanceGroups := slice .EventsGroupedByImportance 0 (min (len .EventsGroupedByImportance) 4) }}
{{ if $index }},{{ end }}
{
"color": "{{- importanceToColor .Importance -}}",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{{importanceToEmoji .Importance}} *{{importanceLabel .Importance}}* ({{.Count}})"
}
},
{{ range $index, $group := slice $importanceGroups.Groups 0 (min (len $importanceGroups.Groups) 3) }}
{{ if $index }},{{ end }}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*{{ $group.Name }}*"
}
},
{{ range $index, $event := slice .Events 0 (min (len .Events) 2) }}
{{ if $index }},{{ end }}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "
{{- if .IsInsight -}}
{{ $event.Details.GetValue "InsightPlainDescription" }}
{{- else -}}
{{- $event.Description -}}\n*State:* {{ .PreviousState }} → *{{ .CurrentState }}*\n*Timeframe:* {{ .StartTime }} (start) → *{{ .EndTime }}*
{{- end -}}
"
}
},
{
"type": "actions",
"elements": [
{{- range $index, $url := $event.Details.WithTag "url" -}}
{{- join $index -}}
{
"type": "button",
"action_id": "{{- $url.Name -}}",
"text": {
"type": "plain_text",
"text": "{{- $url.LabelOrName -}}"
},
"url": "{{ $url.Value }}"
}
{{- end -}}
]
},
{
"type": "divider"
}
{{ end }}
{{ end }}
]
}
{{ end }}
]
}
{{- else if .IsSingleEvent -}}
{{- with .Event -}}
{
"attachments": [
{
"color": "{{- importanceToColor .Importance -}}",
"blocks": [
{
"type": "divider"
},
{
"type": "header",
"text": {
"type": "plain_text",
"emoji": true,
"text": "
{{- importanceToEmoji .Importance }} {{ importanceLabel .Importance -}}\n{{- $.Summary -}}
{{- range $index, $label := (.Details.WithTag "label").Values -}}
{{ " " }}[{{- $label.Name -}}]
{{- end -}}
"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "{{ $.Headline }} for *{{ $.CompanyName }}* sent on {{ $.NowDatetime }}"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "
{{- if not .IsInsight -}}
*State:* {{ .PreviousState }} → *{{ .CurrentState }}*\n*Timeframe:* {{ .StartTime }} (start) → *{{ .EndTime }}*\n
{{- end -}}
{{- range $index, $detail := .Details.General -}}
*{{ $detail.LabelOrName }}*: {{ $detail.Value }}\n
{{- end -}}
{{- with $dimensions := (.Details.WithTag "dimension").FormatDimensions -}}
{{- if gt (len $dimensions) 0 -}}
*Dimensions*:\n
{{- range $index, $detail := $dimensions -}}
{{- if (and (eq "i_device_id" $detail.Name) ($.Event.Details.Has "DeviceName")) -}}
- *Device*: {{ $.Event.Details.GetValue "DeviceName" -}}
{{- if $.Event.Details.Has "DeviceType" }} ({{ $.Event.Details.GetValue "DeviceType" }}) {{ end -}}
{{- range $index, $label := ($.Event.Details.WithTag "device_label").Values -}}
[{{- $label.Name -}}]
{{- end -}}
{{- "\\n" -}}
{{- else -}}
- *{{ $detail.LabelOrName }}*: {{ $detail.Value }}\n
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- with $metrics := (.Details.WithTag "metric").FormatMetrics -}}
{{- if gt (len $metrics) 0 -}}
*Metrics*:\n
{{- range $index, $metric := $metrics -}}
- {{ $metric.Value }} {{ $metric.LabelOrName }}\n
{{- end -}}
{{- end -}}
{{- end -}}
{{- with $stats := .Details.WithTag "statistic" -}}
{{- if gt (len $stats) 0 -}}
*Summary*:\n
{{- range $index, $detail := $stats -}}
- *{{ $detail.LabelOrName }}*: {{ $detail.Value }}\n
{{- end -}}
{{- end -}}
{{- end -}}
{{- with $issues := (.Details.WithTag "issue").Values -}}
{{- if gt (len $issues) 0 -}}
*Issues*:\n
{{- range $index, $issue := $issues -}}
- {{ range $index, $label := $issue.Labels -}}
[{{- $label.Name -}}]{{ " " }}
{{- end -}}
*{{ $issue.Description }}*
{{- if $issue.DetailedInfo }}: {{ end -}}
{{- range $index, $detailedItem := $issue.DetailedInfo -}}
{{- joinWith $index ", " -}}
{{- $detailedItem -}}
{{- end -}}

{{- if $issue.Url -}}
{{- " " }}- <{{ $issue.Url }}|{{ if $issue.UrlLabel }}{{ $issue.UrlLabel }}{{ else }}More details{{ end }} »>
{{- end -}}
\n
{{- end -}}
{{- end -}}
{{- end -}}
"
}
},
{
"type": "actions",
"elements": [
{{- range $index, $url := .Details.WithTag "url" -}}
{{- join $index -}}
{
"type": "button",
"action_id": "{{- $url.Name -}}",
"text": {
"type": "plain_text",
"text": "{{- $url.LabelOrName -}}"
},
"url": "{{ $url.Value }}"
}
{{- end -}}
]
}
]
}
]








}
{{- end -}}
{{- end -}}

{{- /*
Note: In this v1 channel, only one event is expected to be sent at a time,
so there is no case for multiple events.
*/ -}}

0 comments on commit 4d7b06c

Please sign in to comment.