mirror of
https://github.com/containrrr/watchtower.git
synced 2026-03-12 23:46:14 +01:00
Added hangouts chat notification support
This commit is contained in:
parent
f317f9fbc8
commit
d75f3e018c
4 changed files with 109 additions and 1 deletions
86
pkg/notifications/hangouts.go
Normal file
86
pkg/notifications/hangouts.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package notifications
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
hangoutsType = "hangouts"
|
||||
)
|
||||
|
||||
type hangoutsTypeNotifier struct {
|
||||
hangoutsURL string
|
||||
logLevels []log.Level
|
||||
}
|
||||
|
||||
func newHangoutsNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Notifier {
|
||||
flags := c.PersistentFlags()
|
||||
|
||||
hangoutsURL, _ := flags.GetString("notification-hangouts-url")
|
||||
if len(hangoutsURL) < 1 {
|
||||
log.Fatal("Required argument --notification-hangouts-url(cli) or WATCHTOWER_NOTIFICATION_HANGOUTS_CHAT_WEBHOOK_URL(env) is empty.")
|
||||
} else if !(strings.HasPrefix(hangoutsURL, "http://") || strings.HasPrefix(hangoutsURL, "https://")) {
|
||||
log.Fatal("Hangouts URL must start with \"http://\" or \"https://\"")
|
||||
} else if strings.HasPrefix(hangoutsURL, "http://") {
|
||||
log.Warn("Using an HTTP url for Hangouts is insecure")
|
||||
}
|
||||
|
||||
n := &hangoutsTypeNotifier{
|
||||
hangoutsURL: hangoutsURL,
|
||||
logLevels: acceptedLogLevels,
|
||||
}
|
||||
|
||||
log.AddHook(n)
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *hangoutsTypeNotifier) StartNotification() {}
|
||||
|
||||
func (n *hangoutsTypeNotifier) SendNotification() {}
|
||||
|
||||
func (n *hangoutsTypeNotifier) Levels() []log.Level {
|
||||
return n.logLevels
|
||||
}
|
||||
|
||||
func (n *hangoutsTypeNotifier) getURL() string {
|
||||
return n.hangoutsURL
|
||||
}
|
||||
|
||||
func (n *hangoutsTypeNotifier) Fire(entry *log.Entry) error {
|
||||
|
||||
go func() {
|
||||
jsonBody, err := json.Marshal(hangoutsMessage{
|
||||
Text: "(" + entry.Level.String() + "): " + entry.Message,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("Failed to create JSON body for Hangouts notification: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
jsonBodyBuffer := bytes.NewBuffer([]byte(jsonBody))
|
||||
resp, err := http.Post(n.getURL(), "application/json", jsonBodyBuffer)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to send Hangouts notification: ", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
fmt.Printf("Hangouts notification returned %d HTTP status code", resp.StatusCode)
|
||||
}
|
||||
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
type hangoutsMessage struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
|
@ -40,6 +40,8 @@ func NewNotifier(c *cobra.Command) *Notifier {
|
|||
tn = newMsTeamsNotifier(c, acceptedLogLevels)
|
||||
case gotifyType:
|
||||
tn = newGotifyNotifier(c, acceptedLogLevels)
|
||||
case hangoutsType:
|
||||
tn = newHangoutsNotifier(c, acceptedLogLevels)
|
||||
default:
|
||||
log.Fatalf("Unknown notification type %q", t)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue