Skip to content

Commit

Permalink
feat: add slack webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Feb 15, 2024
1 parent dd24d90 commit 8fa90ab
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 51 deletions.
188 changes: 140 additions & 48 deletions gen/go/v1/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/hook/discordhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (h *Hook) doDiscord(cmd *v1.Hook_ActionDiscord, vars HookVars, output io.Wr

requestBytes, _ := json.Marshal(request)

fmt.Fprintf(output, "Sending Discord message to %s\n---- payload ----", cmd.ActionDiscord.GetWebhookUrl())
fmt.Fprintf(output, "Sending Discord message to %s\n---- payload ----\n", cmd.ActionDiscord.GetWebhookUrl())
output.Write(requestBytes)

_, err = post(cmd.ActionDiscord.GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes))
Expand Down
2 changes: 2 additions & 0 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func (h *Hook) Do(event v1.Hook_Condition, vars HookVars, output io.Writer) erro
return h.doDiscord(action, vars, output)
case *v1.Hook_ActionGotify:
return h.doGotify(action, vars, output)
case *v1.Hook_ActionSlack:
return h.doSlack(action, vars, output)
default:
return fmt.Errorf("unknown hook action: %v", action)
}
Expand Down
33 changes: 33 additions & 0 deletions internal/hook/slackhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hook

import (
"bytes"
"encoding/json"
"fmt"
"io"

v1 "github.com/garethgeorge/backrest/gen/go/v1"
)

func (h *Hook) doSlack(cmd *v1.Hook_ActionSlack, vars HookVars, output io.Writer) error {
payload, err := h.renderTemplateOrDefault(cmd.ActionSlack.GetTemplate(), defaultTemplate, vars)
if err != nil {
return fmt.Errorf("template rendering: %w", err)
}

type Message struct {
Text string `json:"text"`
}

request := Message{
Text: "Backrest Notification\n" + payload, // leading newline looks better in discord.
}

requestBytes, _ := json.Marshal(request)

fmt.Fprintf(output, "Sending Slack message to %s\n---- payload ----\n", cmd.ActionSlack.GetWebhookUrl())
output.Write(requestBytes)

_, err = post(cmd.ActionSlack.GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes))
return err
}
6 changes: 6 additions & 0 deletions proto/v1/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ message Hook {
Webhook action_webhook = 101 [json_name="actionWebhook"];
Discord action_discord = 102 [json_name="actionDiscord"];
Gotify action_gotify = 103 [json_name="actionGotify"];
Slack action_slack = 104 [json_name="actionSlack"];
}

message Command {
Expand All @@ -93,6 +94,11 @@ message Hook {
string template = 100 [json_name="template"]; // template for the webhook payload.
string title_template = 101 [json_name="titleTemplate"]; // template for the webhook title.
}

message Slack {
string webhook_url = 1 [json_name="webhookUrl"];
string template = 2 [json_name="template"]; // template for the webhook payload.
}
}

message Auth {
Expand Down
Loading

0 comments on commit 8fa90ab

Please sign in to comment.