Skip to content

Commit

Permalink
Merge pull request #4 from rogerrum/addTimeStamp
Browse files Browse the repository at this point in the history
Add TimeStamp/Footer for every Embedded Alert
  • Loading branch information
rogerrum authored Nov 20, 2022
2 parents 03af00f + 02919a6 commit 4728dc5
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (kv KV) SortedPairs() Pairs {
// Alerts is a list of Alert objects.
type AlertManagerAlerts []AlertManagerAlert

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

type DiscordMessage struct {
Content string `json:"content"`
Username string `json:"username"`
Expand All @@ -95,11 +99,13 @@ type DiscordMessage struct {
type DiscordEmbeds []DiscordEmbed

type DiscordEmbed struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Color int `json:"color"`
Fields DiscordEmbedFields `json:"fields"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Color int `json:"color"`
Fields DiscordEmbedFields `json:"fields"`
Footer *DiscordEmbedFooter `json:"footer,omitempty"`
Timestamp *time.Time `json:"timestamp,omitempty"`
}

type DiscordEmbedFields []DiscordEmbedField
Expand Down Expand Up @@ -130,7 +136,7 @@ func checkWebhookURL(webhookURL string) {
log.Fatalf("The Discord WebHook URL doesn't seem to be a valid URL.")
}

re := regexp.MustCompile(`https://discord(?:app)?.com/api/webhooks/[0-9]{18}/[a-zA-Z0-9_-]+`)
re := regexp.MustCompile(`https://discord(?:app)?.com/api/webhooks/[0-9]{18,19}/[a-zA-Z0-9_-]+`)
if ok := re.Match([]byte(webhookURL)); !ok {
log.Printf("The Discord WebHook URL doesn't seem to be valid.")
}
Expand Down Expand Up @@ -177,6 +183,13 @@ func sendWebhook(alertManagerData *AlertManagerData) {
Name: "*Details:*",
Value: getFormattedLabels(alert.Labels),
})
if *username != "" {
footer := DiscordEmbedFooter{}
footer.Text = *username
embedAlertMessage.Footer = &footer
currentTime := time.Now()
embedAlertMessage.Timestamp = &currentTime
}
embeds = append(embeds, embedAlertMessage)

//Check if number of embeds are greater than discord limit and push to discord
Expand Down

0 comments on commit 4728dc5

Please sign in to comment.