Updated Notifier Example (markdown)

master
Hunter Long 2020-05-01 05:42:35 -07:00
parent fa8f878ef9
commit 6c3ec918f1
1 changed files with 41 additions and 135 deletions

@ -1,165 +1,71 @@
Below is a full example of a Statping notifier which will give you a good example of how to create your own. Insert your new notifier inside the `/notifiers` folder once your ready!
### `notifiers/example.go`
```go
package notifiers
import (
"errors"
"fmt"
"github.com/statping/statping/types"
"github.com/statping/statping/core/notifier"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
"github.com/statping/statping/types/services"
"time"
)
type Example struct {
*notifier.Notification
var _ notifier.Notifier = (*example)(nil)
type example struct {
*notifications.Notification
}
var example = &Example{&notifier.Notification{
Method: METHOD,
Title: "Example",
Description: "Example Notifier",
func (t *example) Select() *notifications.Notification {
return t.Notification
}
var Example = &example{&notifications.Notification{
Method: "example",
Title: "Example Notifier",
Description: "This notifier only prints messages in the console.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Delay: time.Duration(5 * time.Second),
Form: []notifier.NotificationForm{{
Icon: "fa dot-circle",
Delay: time.Duration(10 * time.Second),
Limits: 60,
Form: []notifications.NotificationForm{{
Type: "text",
Title: "Host",
Placeholder: "Insert your Host here.",
DbField: "host",
SmallText: "this is where you would put the host",
}, {
Type: "text",
Title: "Username",
Placeholder: "Insert your Username here.",
DbField: "username",
}, {
Type: "password",
Title: "Password",
Placeholder: "Insert your Password here.",
DbField: "password",
}, {
Type: "number",
Title: "Port",
Placeholder: "Insert your Port here.",
DbField: "port",
}, {
Type: "text",
Title: "API Key",
Placeholder: "Insert your API Key here",
Title: "User Token",
Placeholder: "Insert your Token",
DbField: "api_key",
Required: true,
}, {
Type: "text",
Title: "API Secret",
Placeholder: "Insert your API Secret here",
Title: "Application API Key",
Placeholder: "Insert something secret here",
DbField: "api_secret",
}, {
Type: "text",
Title: "Var 1",
Placeholder: "Insert your Var1 here",
DbField: "var1",
}, {
Type: "text",
Title: "Var2",
Placeholder: "Var2 goes here",
DbField: "var2",
Required: true,
},
}},
}}
// REQUIRED init() will install/load the notifier
func init() {
notifier.AddNotifier(example)
}
// REQUIRED - Send is where you would put the action's of your notifier
func (n *Example) Send(msg interface{}) error {
message := msg.(string)
fmt.Printf("i received this string: %v\n", message)
// OnFailure will trigger failing service
func (t *example) OnFailure(s *services.Service, f *failures.Failure) error {
msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name)
fmt.Println(msg)
return nil
}
// REQUIRED
func (n *Example) Select() *notifier.Notification {
return n.Notification
}
// REQUIRED
func (n *Example) OnSave() error {
msg := fmt.Sprintf("received on save trigger")
n.AddQueue(msg)
// OnSuccess will trigger successful service
func (t *example) OnSuccess(s *services.Service) error {
msg := fmt.Sprintf("Your service '%v' is currently online!", s.Name)
fmt.Println(msg)
return nil
}
// REQUIRED
func (n *Example) Test() error {
msg := fmt.Sprintf("received a test trigger\n")
n.AddQueue(msg)
return nil
}
// REQUIRED - BASIC EVENT
func (n *Example) OnSuccess(s *types.Service) {
msg := fmt.Sprintf("received a count trigger for service: %v\n", s.Name)
n.AddQueue(msg)
}
// REQUIRED - BASIC EVENT
func (n *Example) OnFailure(s *types.Service, f *types.Failure) {
msg := fmt.Sprintf("received a failure trigger for service: %v\n", s.Name)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnNewService(s *types.Service) {
msg := fmt.Sprintf("received a new service trigger for service: %v\n", s.Name)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnUpdatedService(s *types.Service) {
msg := fmt.Sprintf("received a update service trigger for service: %v\n", s.Name)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnDeletedService(s *types.Service) {
msg := fmt.Sprintf("received a delete service trigger for service: %v\n", s.Name)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnNewUser(s *types.User) {
msg := fmt.Sprintf("received a new user trigger for user: %v\n", s.Username)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnUpdatedUser(s *types.User) {
msg := fmt.Sprintf("received a updated user trigger for user: %v\n", s.Username)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnDeletedUser(s *types.User) {
msg := fmt.Sprintf("received a deleted user trigger for user: %v\n", s.Username)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnUpdatedCore(s *types.Core) {
msg := fmt.Sprintf("received a updated core trigger for core: %v\n", s.Name)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnNewNotifier(s *Notification) {
msg := fmt.Sprintf("received a new notifier trigger for notifier: %v\n", s.Method)
n.AddQueue(msg)
}
// OPTIONAL
func (n *Example) OnUpdatedNotifier(s *Notification) {
msg := fmt.Sprintf("received a update notifier trigger for notifier: %v\n", s.Method)
n.AddQueue(msg)
// OnTest will test the example notifier
func (t *example) OnTest() (string, error) {
msg := fmt.Sprintf("Testing the Pushover Notifier")
fmt.Println(msg)
return msg, nil
}
```