statping/notifiers/webhook.go

165 lines
4.6 KiB
Go
Raw Permalink Normal View History

2020-03-12 08:09:25 +00:00
package notifiers
2018-10-03 08:17:25 +00:00
import (
"bytes"
"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"
"github.com/statping/statping/types/null"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2018-10-03 08:17:25 +00:00
"io/ioutil"
"net/http"
"strings"
"time"
)
2020-03-14 03:13:20 +00:00
var _ notifier.Notifier = (*webhooker)(nil)
2020-03-09 15:15:15 +00:00
2018-10-03 08:17:25 +00:00
const (
2020-03-09 15:15:15 +00:00
webhookMethod = "webhook"
2018-10-03 08:17:25 +00:00
)
2018-10-06 05:00:40 +00:00
type webhooker struct {
2020-03-14 03:13:20 +00:00
*notifications.Notification
2018-10-03 08:17:25 +00:00
}
2020-03-14 03:13:20 +00:00
var Webhook = &webhooker{&notifications.Notification{
2018-10-06 05:05:50 +00:00
Method: webhookMethod,
Title: "Webhook",
2019-01-17 22:19:18 +00:00
Description: "Send a custom HTTP request to a specific URL with your own body, headers, and parameters.",
2018-10-03 08:17:25 +00:00
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
2018-10-21 16:22:26 +00:00
Icon: "fas fa-code-branch",
Delay: time.Duration(3 * time.Second),
SuccessData: null.NewNullString(`{"id": "{{.Service.Id}}", "online": true}`),
FailureData: null.NewNullString(`{"id": "{{.Service.Id}}", "online": false}`),
DataType: "json",
2020-03-17 03:13:07 +00:00
Limits: 180,
2020-03-14 03:13:20 +00:00
Form: []notifications.NotificationForm{{
2018-10-03 08:17:25 +00:00
Type: "text",
Title: "HTTP Endpoint",
Placeholder: "http://webhookurl.com/JW2MCP4SKQP",
2019-01-17 22:19:18 +00:00
SmallText: "Insert the URL for your HTTP Requests.",
2018-10-03 08:17:25 +00:00
DbField: "Host",
Required: true,
}, {
Type: "list",
2018-10-03 08:17:25 +00:00
Title: "HTTP Method",
Placeholder: "POST",
2019-01-17 22:19:18 +00:00
SmallText: "Choose a HTTP method for example: GET, POST, DELETE, or PATCH.",
2018-10-03 08:17:25 +00:00
DbField: "Var1",
Required: true,
ListOptions: []string{"GET", "POST", "PATCH", "DELETE"},
2018-10-03 08:17:25 +00:00
}, {
Type: "text",
Title: "Content Type",
Placeholder: `application/json`,
SmallText: "Optional content type for example: application/json or text/plain",
DbField: "api_key",
}, {
Type: "text",
Title: "Header",
Placeholder: "Authorization=Token12345",
SmallText: "Optional Headers for request use format: KEY=Value,Key=Value",
DbField: "api_secret",
},
}}}
2018-10-06 05:00:40 +00:00
// Send will send a HTTP Post to the webhooker API. It accepts type: string
func (w *webhooker) Send(msg interface{}) error {
2018-11-10 03:42:32 +00:00
resp, err := w.sendHttpWebhook(msg.(string))
2018-10-03 10:47:32 +00:00
if err == nil {
resp.Body.Close()
}
2018-10-03 08:17:25 +00:00
return err
}
2020-03-14 03:13:20 +00:00
func (w *webhooker) Select() *notifications.Notification {
2018-10-03 08:17:25 +00:00
return w.Notification
}
func (w *webhooker) Valid(values notifications.Values) error {
return nil
}
2018-11-10 03:42:32 +00:00
func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) {
2020-07-24 01:36:30 +00:00
utils.Log.Infoln(fmt.Sprintf("sending body: '%v' to %v as a %v request", body, w.Host.String, w.Var1.String))
2018-10-03 08:17:25 +00:00
client := new(http.Client)
2020-07-07 21:15:47 +00:00
client.Timeout = 10 * time.Second
req, err := http.NewRequest(w.Var1.String, w.Host.String, bytes.NewBufferString(body))
2018-10-03 08:17:25 +00:00
if err != nil {
return nil, err
}
if w.ApiSecret.String != "" {
keyVal := strings.SplitN(w.ApiSecret.String, "=", 2)
if len(keyVal) == 2 {
if keyVal[0] != "" && keyVal[1] != "" {
if strings.ToLower(keyVal[0]) == "host" {
req.Host = strings.TrimSpace(keyVal[1])
} else {
req.Header.Set(keyVal[0], keyVal[1])
}
}
2018-10-03 08:17:25 +00:00
}
}
if w.ApiKey.String != "" {
req.Header.Add("Content-Type", w.ApiKey.String)
2020-07-07 21:15:47 +00:00
} else {
req.Header.Add("Content-Type", "application/json")
2018-10-03 08:17:25 +00:00
}
2018-12-04 05:57:11 +00:00
req.Header.Set("User-Agent", "Statping")
req.Header.Set("Statping-Version", utils.Params.GetString("VERSION"))
2018-10-03 08:17:25 +00:00
resp, err := client.Do(req)
if err != nil {
return nil, err
}
2018-10-03 08:17:25 +00:00
return resp, err
}
func (w *webhooker) OnTest() (string, error) {
f := failures.Example()
s := services.Example(false)
body := ReplaceVars(w.SuccessData.String, s, f)
2018-11-10 03:42:32 +00:00
resp, err := w.sendHttpWebhook(body)
2018-10-03 08:17:25 +00:00
if err != nil {
return "", err
2018-10-03 08:17:25 +00:00
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
out := fmt.Sprintf("Webhook notifier received: '%v'", string(content))
utils.Log.Infoln(out)
return out, err
2018-10-03 08:17:25 +00:00
}
// OnFailure will trigger failing service
func (w *webhooker) OnFailure(s services.Service, f failures.Failure) (string, error) {
msg := ReplaceVars(w.FailureData.String, s, f)
2020-03-14 03:13:20 +00:00
resp, err := w.sendHttpWebhook(msg)
2020-03-25 19:23:50 +00:00
if err != nil {
return "", err
2020-03-25 19:23:50 +00:00
}
2020-03-14 03:13:20 +00:00
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
return string(content), err
2018-10-03 08:17:25 +00:00
}
// OnSuccess will trigger successful service
func (w *webhooker) OnSuccess(s services.Service) (string, error) {
msg := ReplaceVars(w.SuccessData.String, s, failures.Failure{})
2020-03-14 03:13:20 +00:00
resp, err := w.sendHttpWebhook(msg)
2020-03-25 19:23:50 +00:00
if err != nil {
return "", err
2020-03-25 19:23:50 +00:00
}
2020-03-14 03:13:20 +00:00
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
return string(content), err
2018-10-03 08:17:25 +00:00
}
2020-06-21 06:52:07 +00:00
// OnSave will trigger when this notifier is saved
func (w *webhooker) OnSave() (string, error) {
return "", nil
}