statping/notifiers/notifiers.go

83 lines
1.5 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package notifiers
import (
"bytes"
"html/template"
"time"
"github.com/statping/statping/types/core"
"github.com/statping/statping/types/failures"
2020-03-12 04:58:56 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2020-03-04 10:29:00 +00:00
)
2020-08-06 07:58:00 +00:00
//go:generate go run generate.go
2020-04-15 02:04:50 +00:00
var log = utils.Log.WithField("type", "notifier")
type replacer struct {
Core core.Core
Service services.Service
Failure failures.Failure
Email string
Custom map[string]string
}
2020-03-14 03:13:20 +00:00
func InitNotifiers() {
Add(
slacker,
2020-03-12 08:09:25 +00:00
Command,
Discorder,
2020-03-14 03:13:20 +00:00
email,
2020-03-12 08:09:25 +00:00
LineNotify,
Telegram,
Twilio,
Webhook,
2020-03-14 03:13:20 +00:00
Mobile,
2020-04-09 01:47:27 +00:00
Pushover,
2020-06-20 00:57:34 +00:00
statpingMailer,
2020-07-07 20:58:46 +00:00
Gotify,
AmazonSNS,
2020-03-04 10:29:00 +00:00
)
2020-08-30 02:02:23 +00:00
services.UpdateNotifiers()
2020-03-04 10:29:00 +00:00
}
2020-03-14 03:13:20 +00:00
func ReplaceTemplate(tmpl string, data replacer) string {
buf := new(bytes.Buffer)
tmp, err := template.New("replacement").Parse(tmpl)
if err != nil {
log.Error(err)
return err.Error()
}
err = tmp.Execute(buf, data)
if err != nil {
log.Error(err)
return err.Error()
}
return buf.String()
}
2020-03-14 03:13:20 +00:00
func Add(notifs ...services.ServiceNotifier) {
for _, n := range notifs {
2020-08-26 10:48:05 +00:00
notif := n.Select()
if err := notif.Create(); err != nil {
log.Error(err)
2020-03-14 03:13:20 +00:00
}
2020-08-26 10:48:05 +00:00
services.AddNotifier(n)
2020-03-14 03:13:20 +00:00
}
}
func ReplaceVars(input string, s services.Service, f failures.Failure) string {
return ReplaceTemplate(input, replacer{Service: s, Failure: f, Core: *core.App})
}
var exampleFailure = &failures.Failure{
Id: 1,
Issue: "HTTP returned a 500 status code",
ErrorCode: 500,
Service: 1,
PingTime: 43203,
CreatedAt: utils.Now().Add(-10 * time.Minute),
}