statping/notifiers/discord.go

101 lines
3.3 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-09-12 04:14:22 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2020-03-09 18:17:55 +00:00
// https://github.com/statping/statping
2018-09-12 04:14:22 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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"
2018-09-12 04:14:22 +00:00
"fmt"
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-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
2020-03-14 03:13:20 +00:00
func (u *discord) sendRequest(msg string) error {
_, _, 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
}
2020-03-14 03:13:20 +00:00
func (u *discord) Select() *notifications.Notification {
2018-09-12 04:14:22 +00:00
return u.Notification
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
2020-03-14 03:13:20 +00:00
func (u *discord) OnFailure(s *services.Service, f *failures.Failure) error {
2018-09-12 04:14:22 +00:00
msg := fmt.Sprintf(`{"content": "Your service '%v' is currently failing! Reason: %v"}`, s.Name, f.Issue)
2020-03-14 03:13:20 +00:00
return u.sendRequest(msg)
2018-09-12 04:14:22 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
2020-03-14 03:13:20 +00:00
func (u *discord) OnSuccess(s *services.Service) error {
msg := fmt.Sprintf(`{"content": "Your service '%s' is currently online!"}`, s.Name)
return u.sendRequest(msg)
2018-09-12 04:14:22 +00:00
}
2018-09-27 00:41:51 +00:00
// OnSave triggers when this notifier has been saved
2018-10-06 05:00:40 +00:00
func (u *discord) OnTest() error {
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
}
var d discordTestJson
err = json.Unmarshal(contents, &d)
if err != nil {
return outError
}
if d.Code == 0 {
return outError
}
fmt.Println("discord: ", string(contents))
return nil
}
type discordTestJson struct {
Code int `json:"code"`
Message string `json:"message"`
}