2020-03-04 10:29:00 +00:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2020-05-02 09:51:47 +00:00
|
|
|
"bytes"
|
2020-10-13 00:31:51 +00:00
|
|
|
"html/template"
|
|
|
|
"time"
|
|
|
|
|
2020-05-02 09:51:47 +00:00
|
|
|
"github.com/statping/statping/types/core"
|
2020-03-23 02:50:30 +00:00
|
|
|
"github.com/statping/statping/types/failures"
|
2020-03-12 04:58:56 +00:00
|
|
|
"github.com/statping/statping/types/services"
|
2020-03-23 02:50:30 +00:00
|
|
|
"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-08-06 04:17:14 +00:00
|
|
|
|
2020-04-15 02:04:50 +00:00
|
|
|
var log = utils.Log.WithField("type", "notifier")
|
|
|
|
|
2020-05-02 09:51:47 +00:00
|
|
|
type replacer struct {
|
2020-06-26 04:08:12 +00:00
|
|
|
Core core.Core
|
|
|
|
Service services.Service
|
|
|
|
Failure failures.Failure
|
2020-10-13 00:31:51 +00:00
|
|
|
Email string
|
2020-06-19 09:14:40 +00:00
|
|
|
Custom map[string]string
|
2020-05-02 09:51:47 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2020-08-20 07:21:19 +00:00
|
|
|
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
|
|
|
|
2020-05-02 09:51:47 +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 {
|
2020-03-23 02:50:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-03-23 02:50:30 +00:00
|
|
|
|
2020-06-26 04:08:12 +00:00
|
|
|
func ReplaceVars(input string, s services.Service, f failures.Failure) string {
|
|
|
|
return ReplaceTemplate(input, replacer{Service: s, Failure: f, Core: *core.App})
|
2020-03-23 02:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|