statping/notifiers/discord.go

85 lines
2.8 KiB
Go
Raw Normal View History

2020-03-12 08:09:25 +00:00
package notifiers
2018-09-12 04:14:22 +00:00
import (
"bytes"
2018-09-27 00:41:51 +00:00
"encoding/json"
"errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2018-11-25 03:56:09 +00:00
"strings"
"time"
2018-09-12 04:14:22 +00:00
)
2020-03-14 03:13:20 +00:00
var _ notifier.Notifier = (*discord)(nil)
2020-03-09 15:15:15 +00:00
2018-10-06 05:00:40 +00:00
type discord struct {
2020-03-14 03:13:20 +00:00
*notifications.Notification
2018-09-12 04:14:22 +00:00
}
2020-03-14 03:13:20 +00:00
var Discorder = &discord{&notifications.Notification{
Method: "discord",
2018-10-06 05:00:40 +00:00
Title: "discord",
2019-10-25 15:13:32 +00:00
Description: "Send notifications to your discord channel using discord webhooks. Insert your discord channel Webhook URL to receive notifications. Based on the <a href=\"https://discordapp.com/developers/docs/resources/Webhook\">discord webhooker API</a>.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Delay: time.Duration(5 * time.Second),
Host: "https://discordapp.com/api/webhooks/****/*****",
2018-10-21 16:22:26 +00:00
Icon: "fab fa-discord",
2020-03-17 03:13:07 +00:00
Limits: 60,
2020-03-14 03:13:20 +00:00
Form: []notifications.NotificationForm{{
2018-09-12 04:14:22 +00:00
Type: "text",
2018-10-06 05:00:40 +00:00
Title: "discord webhooker URL",
2019-10-25 15:13:32 +00:00
Placeholder: "Insert your Webhook URL here",
2018-09-12 04:14:22 +00:00
DbField: "host",
}}},
}
2018-10-06 05:00:40 +00:00
// Send will send a HTTP Post to the discord API. It accepts type: []byte
func (d *discord) sendRequest(msg string) error {
2020-03-14 03:13:20 +00:00
_, _, err := utils.HttpRequest(Discorder.GetValue("host"), "POST", "application/json", nil, strings.NewReader(msg), time.Duration(10*time.Second), true)
2018-11-25 03:56:09 +00:00
return err
2018-09-12 04:14:22 +00:00
}
func (d *discord) Select() *notifications.Notification {
return d.Notification
2018-09-12 04:14:22 +00:00
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
func (d *discord) OnFailure(s *services.Service, f *failures.Failure) error {
msg := `{"content": "Your service '{{.Service.Name}}' is currently failing! Reason: {{.Failure.Issue}}"}`
return d.sendRequest(ReplaceVars(msg, s, f))
2018-09-12 04:14:22 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
func (d *discord) OnSuccess(s *services.Service) error {
msg := `{"content": "Your service '{{.Service.Name}}' is currently online!"}`
return d.sendRequest(ReplaceVars(msg, s, nil))
2018-09-12 04:14:22 +00:00
}
2018-09-27 00:41:51 +00:00
// OnSave triggers when this notifier has been saved
func (d *discord) OnTest() (string, error) {
2018-10-06 05:00:40 +00:00
outError := errors.New("Incorrect discord URL, please confirm URL is correct")
message := `{"content": "Testing the discord notifier"}`
2019-10-25 15:13:32 +00:00
contents, _, err := utils.HttpRequest(Discorder.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(message)), time.Duration(10*time.Second), true)
2018-09-27 00:41:51 +00:00
if string(contents) == "" {
return "", nil
2018-09-27 00:41:51 +00:00
}
var dtt discordTestJson
err = json.Unmarshal(contents, &dtt)
2018-09-27 00:41:51 +00:00
if err != nil {
return string(contents), outError
2018-09-27 00:41:51 +00:00
}
if dtt.Code == 0 {
return string(contents), outError
2018-09-27 00:41:51 +00:00
}
return string(contents), nil
2018-09-27 00:41:51 +00:00
}
type discordTestJson struct {
Code int `json:"code"`
Message string `json:"message"`
}