-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(discord): format update, preparations for rich message support (#…
…108) * feat: add rich message API * feat: add field base tag and default field helper * feat(discord): update default message formatting also prepares for rich message sending * fix(types): integrate rich sender into default sender interface * format: fix naming and missing comments * feat(config): allow numeric bases to be detected this includes the web color format #xxxxxx * feat(discord): implement the rich send interface * feat: implement simple rich send wrapper for all services * test(discord): add black box tests * fix: split out rich API support Revert "feat: implement simple rich send wrapper for all services" This reverts commit fe5d43f
- Loading branch information
Showing
10 changed files
with
610 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,71 @@ | ||
package discord | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/containrrr/shoutrrr/pkg/types" | ||
"github.com/containrrr/shoutrrr/pkg/util" | ||
"time" | ||
) | ||
|
||
// WebhookPayload is the webhook endpoint payload | ||
type WebhookPayload struct { | ||
Embeds []embedItem `json:"content"` | ||
} | ||
|
||
// JSON is the actual notification payload | ||
type JSON struct { | ||
Text string `json:"content"` | ||
type embedItem struct { | ||
Title string `json:"title,omitempty"` | ||
Content string `json:"description,omitempty"` | ||
URL string `json:"url,omitempty"` | ||
Timestamp string `json:"timestamp,omitempty"` | ||
Color int `json:"color,omitempty"` | ||
Footer *embedFooter `json:"footer,omitempty"` | ||
} | ||
|
||
// CreateJSONToSend creates a JSON payload to be sent to the discord webhook API | ||
func CreateJSONToSend(message string, isJSON bool) ([]byte, error) { | ||
if message == "" { | ||
return nil, errors.New("message was empty") | ||
} | ||
if len(message) > maxlength { | ||
return nil, errors.New("the supplied message exceeds the max length for discord") | ||
type embedFooter struct { | ||
Text string `json:"text"` | ||
IconURL string `json:"icon_url,omitempty"` | ||
} | ||
|
||
// CreatePayloadFromItems creates a JSON payload to be sent to the discord webhook API | ||
func CreatePayloadFromItems(items []types.MessageItem, title string, colors [types.MessageLevelCount]int, omitted int) (WebhookPayload, error) { | ||
|
||
itemCount := util.Min(9, len(items)) | ||
embeds := make([]embedItem, 1, itemCount+1) | ||
|
||
for _, item := range items { | ||
|
||
color := 0 | ||
if item.Level >= types.Unknown && int(item.Level) < len(colors) { | ||
color = colors[item.Level] | ||
} | ||
|
||
ei := embedItem{ | ||
Content: item.Text, | ||
Color: color, | ||
} | ||
|
||
if item.Level != types.Unknown { | ||
ei.Footer = &embedFooter{ | ||
Text: item.Level.String(), | ||
} | ||
} | ||
|
||
if !item.Timestamp.IsZero() { | ||
ei.Timestamp = item.Timestamp.UTC().Format(time.RFC3339) | ||
} | ||
|
||
embeds = append(embeds, ei) | ||
} | ||
if isJSON { | ||
return []byte(message), nil | ||
|
||
embeds[0].Title = title | ||
if omitted > 0 { | ||
embeds[0].Footer = &embedFooter{ | ||
Text: fmt.Sprintf("... (%v character(s) where omitted)", omitted), | ||
} | ||
} | ||
return json.Marshal(JSON{ | ||
Text: message, | ||
}) | ||
|
||
return WebhookPayload{ | ||
Embeds: embeds, | ||
}, nil | ||
} |
Oops, something went wrong.