mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
parent
3bbe1bd109
commit
35490c853d
9 changed files with 375 additions and 295 deletions
|
@ -1,15 +1,12 @@
|
|||
package notifications
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
shoutrrrTeams "github.com/containrrr/shoutrrr/pkg/services/teams"
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -22,7 +19,12 @@ type msTeamsTypeNotifier struct {
|
|||
data bool
|
||||
}
|
||||
|
||||
func newMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) t.Notifier {
|
||||
// NewMsTeamsNotifier is a factory method creating a new teams notifier instance
|
||||
func NewMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) t.ConvertableNotifier {
|
||||
return newMsTeamsNotifier(cmd, acceptedLogLevels)
|
||||
}
|
||||
|
||||
func newMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) t.ConvertableNotifier {
|
||||
|
||||
flags := cmd.PersistentFlags()
|
||||
|
||||
|
@ -38,103 +40,29 @@ func newMsTeamsNotifier(cmd *cobra.Command, acceptedLogLevels []log.Level) t.Not
|
|||
data: withData,
|
||||
}
|
||||
|
||||
log.AddHook(n)
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *msTeamsTypeNotifier) StartNotification() {}
|
||||
func (n *msTeamsTypeNotifier) GetURL() string {
|
||||
|
||||
func (n *msTeamsTypeNotifier) SendNotification() {}
|
||||
baseURL := "https://outlook.office.com/webhook/"
|
||||
|
||||
func (n *msTeamsTypeNotifier) Close() {}
|
||||
path := strings.Replace(n.webHookURL, baseURL, "", 1)
|
||||
rawToken := strings.Replace(path, "/IncomingWebhook", "", 1)
|
||||
token := strings.Split(rawToken, "/")
|
||||
config := &shoutrrrTeams.Config{
|
||||
Token: shoutrrrTeams.Token{
|
||||
A: token[0],
|
||||
B: token[1],
|
||||
C: token[2],
|
||||
},
|
||||
}
|
||||
|
||||
func (n *msTeamsTypeNotifier) Levels() []log.Level {
|
||||
return n.levels
|
||||
return config.GetURL().String()
|
||||
}
|
||||
|
||||
func (n *msTeamsTypeNotifier) Fire(entry *log.Entry) error {
|
||||
|
||||
message := "(" + entry.Level.String() + "): " + entry.Message
|
||||
|
||||
go func() {
|
||||
webHookBody := messageCard{
|
||||
CardType: "MessageCard",
|
||||
Context: "http://schema.org/extensions",
|
||||
Markdown: true,
|
||||
Text: message,
|
||||
}
|
||||
|
||||
if n.data && entry.Data != nil && len(entry.Data) > 0 {
|
||||
section := messageCardSection{
|
||||
Facts: make([]messageCardSectionFact, len(entry.Data)),
|
||||
Text: "",
|
||||
}
|
||||
|
||||
index := 0
|
||||
for k, v := range entry.Data {
|
||||
section.Facts[index] = messageCardSectionFact{
|
||||
Name: k,
|
||||
Value: fmt.Sprint(v),
|
||||
}
|
||||
index++
|
||||
}
|
||||
|
||||
webHookBody.Sections = []messageCardSection{section}
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(webHookBody)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to build JSON body for MSTeams notificattion: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := http.Post(n.webHookURL, "application/json", bytes.NewBuffer([]byte(jsonBody)))
|
||||
if err != nil {
|
||||
fmt.Println("Failed to send MSTeams notificattion: ", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
fmt.Println("Failed to send MSTeams notificattion. HTTP RESPONSE STATUS: ", resp.StatusCode)
|
||||
if resp.Body != nil {
|
||||
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err == nil {
|
||||
bodyString := string(bodyBytes)
|
||||
fmt.Println(bodyString)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type messageCard struct {
|
||||
CardType string `json:"@type"`
|
||||
Context string `json:"@context"`
|
||||
CorrelationID string `json:"correlationId,omitempty"`
|
||||
ThemeColor string `json:"themeColor,omitempty"`
|
||||
Summary string `json:"summary,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Markdown bool `json:"markdown,bool"`
|
||||
Sections []messageCardSection `json:"sections,omitempty"`
|
||||
}
|
||||
|
||||
type messageCardSection struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
ActivityTitle string `json:"activityTitle,omitempty"`
|
||||
ActivitySubtitle string `json:"activitySubtitle,omitempty"`
|
||||
ActivityImage string `json:"activityImage,omitempty"`
|
||||
ActivityText string `json:"activityText,omitempty"`
|
||||
HeroImage string `json:"heroImage,omitempty"`
|
||||
Facts []messageCardSectionFact `json:"facts,omitempty"`
|
||||
}
|
||||
|
||||
type messageCardSectionFact struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Value string `json:"value,omitempty"`
|
||||
}
|
||||
func (n *msTeamsTypeNotifier) StartNotification() {}
|
||||
func (n *msTeamsTypeNotifier) SendNotification() {}
|
||||
func (n *msTeamsTypeNotifier) Close() {}
|
||||
func (n *msTeamsTypeNotifier) Levels() []log.Level { return nil }
|
||||
func (n *msTeamsTypeNotifier) Fire(entry *log.Entry) error { return nil }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue