statping/notifiers/discord.go

130 lines
3.7 KiB
Go
Raw Normal View History

2018-09-12 04:14:22 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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/>.
package notifiers
import (
"bytes"
2018-09-27 00:41:51 +00:00
"encoding/json"
"errors"
2018-09-12 04:14:22 +00:00
"fmt"
"github.com/hunterlong/statup/core/notifier"
"github.com/hunterlong/statup/types"
2018-09-27 00:41:51 +00:00
"io/ioutil"
2018-09-12 04:14:22 +00:00
"net/http"
"time"
2018-09-12 04:14:22 +00:00
)
2018-10-06 05:00:40 +00:00
type discord struct {
2018-09-12 04:14:22 +00:00
*notifier.Notification
}
2018-10-06 05:00:40 +00:00
var discorder = &discord{&notifier.Notification{
Method: "discord",
2018-10-06 05:00:40 +00:00
Title: "discord",
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-09-12 04:14:22 +00:00
Form: []notifier.NotificationForm{{
Type: "text",
2018-10-06 05:00:40 +00:00
Title: "discord webhooker URL",
2018-09-12 04:14:22 +00:00
Placeholder: "Insert your webhook URL here",
DbField: "host",
}}},
}
2018-10-06 05:00:40 +00:00
// init the discord notifier
2018-09-12 04:14:22 +00:00
func init() {
err := notifier.AddNotifier(discorder)
if err != nil {
panic(err)
}
}
2018-10-06 05:00:40 +00:00
// Send will send a HTTP Post to the discord API. It accepts type: []byte
func (u *discord) Send(msg interface{}) error {
message := msg.(string)
req, _ := http.NewRequest("POST", discorder.GetValue("host"), bytes.NewBuffer([]byte(message)))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
return resp.Body.Close()
2018-09-12 04:14:22 +00:00
}
2018-10-06 05:00:40 +00:00
func (u *discord) Select() *notifier.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
2018-10-06 05:00:40 +00:00
func (u *discord) OnFailure(s *types.Service, f *types.Failure) {
2018-09-12 04:14:22 +00:00
msg := fmt.Sprintf(`{"content": "Your service '%v' is currently failing! Reason: %v"}`, s.Name, f.Issue)
u.AddQueue(msg)
u.Online = false
2018-09-12 04:14:22 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
2018-10-06 05:00:40 +00:00
func (u *discord) OnSuccess(s *types.Service) {
if !u.Online {
msg := fmt.Sprintf(`{"content": "Your service '%v' is back online!"}`, s.Name)
u.AddQueue(msg)
}
u.Online = true
2018-09-12 04:14:22 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSave triggers when this notifier has been saved
2018-10-06 05:00:40 +00:00
func (u *discord) OnSave() error {
msg := fmt.Sprintf(`{"content": "The discord notifier on Statup was just updated."}`)
u.AddQueue(msg)
2018-09-12 04:14:22 +00:00
return nil
}
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"}`
2018-09-27 00:41:51 +00:00
req, _ := http.NewRequest("POST", discorder.Host, bytes.NewBuffer([]byte(message)))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
contents, _ := ioutil.ReadAll(resp.Body)
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"`
}