statping/notifiers/webhook.go

150 lines
4.0 KiB
Go
Raw 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"
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",
2018-10-03 08:17:25 +00:00
Delay: time.Duration(1 * time.Second),
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: "text",
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,
}, {
Type: "textarea",
Title: "HTTP Body",
Placeholder: `{"service_id": {{.Service.Id}}", "service_name": "{{.Service.Name}"}`,
SmallText: "Optional HTTP body for a POST request. You can insert variables into your body request.<br>{{.Service.Id}}, {{.Service.Name}}, {{.Service.Online}}<br>{{.Failure.Issue}}",
2018-10-03 08:17:25 +00:00
DbField: "Var2",
}, {
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
}
2018-11-10 03:42:32 +00:00
func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) {
utils.Log.Infoln(fmt.Sprintf("sending body: '%v' to %v as a %v request", body, w.Host, w.Var1))
2018-10-03 08:17:25 +00:00
client := new(http.Client)
client.Timeout = time.Duration(10 * time.Second)
var buf *bytes.Buffer
buf = bytes.NewBuffer(nil)
if w.Var2 != "" {
2018-11-10 03:42:32 +00:00
buf = bytes.NewBuffer([]byte(body))
2018-10-03 08:17:25 +00:00
}
req, err := http.NewRequest(w.Var1, w.Host, buf)
if err != nil {
return nil, err
}
if w.ApiSecret != "" {
splitArray := strings.Split(w.ApiSecret, ",")
for _, a := range splitArray {
split := strings.Split(a, "=")
req.Header.Add(split[0], split[1])
}
}
if w.ApiKey != "" {
req.Header.Add("Content-Type", w.ApiKey)
2018-10-03 08:17:25 +00:00
}
2018-12-04 05:57:11 +00:00
req.Header.Set("User-Agent", "Statping")
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) {
body := ReplaceVars(w.Var2, exampleService, exampleFailure)
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
2020-03-14 03:13:20 +00:00
func (w *webhooker) OnFailure(s *services.Service, f *failures.Failure) error {
msg := ReplaceVars(w.Var2, 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-14 03:13:20 +00:00
defer resp.Body.Close()
return err
2018-10-03 08:17:25 +00:00
}
// OnSuccess will trigger successful service
2020-03-14 03:13:20 +00:00
func (w *webhooker) OnSuccess(s *services.Service) error {
msg := ReplaceVars(w.Var2, s, nil)
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-14 03:13:20 +00:00
defer resp.Body.Close()
return err
2018-10-03 08:17:25 +00:00
}