feat: add support for notification action

This commit is contained in:
Bastien Riviere 2023-09-03 16:38:03 +02:00
parent ea22fa5569
commit ac2940ccfe
Signed by: babariviere
GPG key ID: 4E5F0839249F162E

View file

@ -23,11 +23,37 @@ type NotificationError struct {
Error string `json:"error"`
}
type NotificationAction struct {
Action string
Label string
Params []string
}
func NewViewAction(label, url string, params ...string) NotificationAction {
return NotificationAction{
Action: "view",
Label: label,
Params: append([]string{url}, params...),
}
}
func (n NotificationAction) Format() string {
var sb strings.Builder
sb.WriteString(n.Action + ", " + n.Label)
if len(n.Params) > 0 {
sb.WriteString(", " + strings.Join(n.Params, ", "))
}
return sb.String()
}
type Notification struct {
Title string
Body string
Priority int
Tags []string
Actions []NotificationAction
IsMarkdown bool
topic string
@ -70,6 +96,15 @@ func (n Notification) Send(base string) error {
req.Header.Set("Authorization", n.auth.bearer())
}
if len(n.Actions) > 0 {
actions := make([]string, len(n.Actions))
for i, act := range n.Actions {
actions[i] = act.Format()
}
req.Header.Set("Actions", strings.Join(actions, "; "))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err