feat: add support for block kit slack body (#774)
Build Snapshot Release / build (push) Has been cancelled
Docs / build (push) Has been cancelled
Release Please / release-please (push) Has been cancelled
Test / test-nix (push) Has been cancelled
Test / test-win (push) Has been cancelled
Docs / deploy (push) Has been cancelled
Update Restic / update-restic-version (push) Has been cancelled

This commit is contained in:
Justin Rich
2025-05-29 02:19:46 -07:00
committed by GitHub
parent 1a9bed21e2
commit 9a4e0c5c74
5 changed files with 88 additions and 10 deletions
@@ -0,0 +1,73 @@
# Slack Hook Build Kit Examples
## Overview
When using the Slack Hook you can provide a simple message or a [Slack Block Kit](https://api.slack.com/block-kit) message. You can leverage the [Block Kit Builder](https://app.slack.com/block-kit-builder) to Preview the message.
## Clean Job Summary
<div style="text-align: center;">
<img src="/screenshots/slack-clean-job.png" alt="Settings View" style="width: 700px; height: auto;" />
</div>
```
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Backup: *{{ .Plan.Id }}* ➝ *{{ .Repo.Id }}* at *{{ .FormatTime .CurTime }}*"
}
},
{
"type": "divider"
},
{{ if .Error }}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":x: *Snapshot failed:*\n```{{ .Error }}```"
}
}
{{ else }}
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":white_check_mark: *Snapshot `{{ .SnapshotId }}` created*"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Total Added:*\n`{{ .FormatSizeBytes .SnapshotStats.DataAdded }}`"
},
{
"type": "mrkdwn",
"text": "*Files Processed:*\n`{{ .SnapshotStats.TotalFilesProcessed }}`"
},
{
"type": "mrkdwn",
"text": "*Bytes Processed:*\n`{{ .FormatSizeBytes .SnapshotStats.TotalBytesProcessed }}`"
},
{
"type": "mrkdwn",
"text": "*Time:*\n`{{ .SnapshotStats.TotalDuration }}s`"
}
]
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Backup Stats:*\n• *Files:* `{{ .SnapshotStats.FilesNew }} new`, `{{ .SnapshotStats.FilesChanged }} changed`, `{{ .SnapshotStats.FilesUnmodified }} unmodified`\n• *Dirs:* `{{ .SnapshotStats.DirsNew }} new`, `{{ .SnapshotStats.DirsChanged }} changed`, `{{ .SnapshotStats.DirsUnmodified }} unmodified`"
}
}
{{ end }}
]
}
```
Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

+15 -10
View File
@@ -20,25 +20,30 @@ func (slackHandler) Name() string {
}
func (slackHandler) Execute(ctx context.Context, cmd *v1.Hook, vars interface{}, runner tasks.TaskRunner, event v1.Hook_Condition) error {
payload, err := hookutil.RenderTemplateOrDefault(cmd.GetActionSlack().GetTemplate(), hookutil.DefaultTemplate, vars)
if err != nil {
return fmt.Errorf("template rendering: %w", err)
}
l := runner.Logger(ctx)
l.Sugar().Infof("Sending slack message to %s", cmd.GetActionSlack().GetWebhookUrl())
l.Debug("Sending slack message", zap.String("payload", payload))
type Message struct {
Text string `json:"text"`
var requestBytes []byte
if json.Valid([]byte(payload)) {
l.Sugar().Infof("Sending advanced slack message to %s", cmd.GetActionSlack().GetWebhookUrl())
l.Sugar().Debugf("Sending advanced slack message: %s", payload)
requestBytes = []byte(payload)
} else {
l.Sugar().Infof("Sending slack message to %s", cmd.GetActionSlack().GetWebhookUrl())
l.Debug("Sending slack message", zap.String("payload", payload))
request := struct {
Text string `json:"text"`
}{
Text: "Backrest Notification\n" + payload,
}
requestBytes, _ = json.Marshal(request)
}
request := Message{
Text: "Backrest Notification\n" + payload, // leading newline looks better in discord.
}
requestBytes, _ := json.Marshal(request)
body, err := hookutil.PostRequest(cmd.GetActionSlack().GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes))
if err != nil {
return fmt.Errorf("sending slack message to %q: %w", cmd.GetActionSlack().GetWebhookUrl(), err)