mirror of https://github.com/statping/statping
Updated Notifier Example (markdown)
parent
fa8f878ef9
commit
6c3ec918f1
|
@ -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!
|
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
|
```go
|
||||||
package notifiers
|
package notifiers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/statping/statping/types"
|
"github.com/statping/statping/types/failures"
|
||||||
"github.com/statping/statping/core/notifier"
|
"github.com/statping/statping/types/notifications"
|
||||||
|
"github.com/statping/statping/types/notifier"
|
||||||
|
"github.com/statping/statping/types/services"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Example struct {
|
var _ notifier.Notifier = (*example)(nil)
|
||||||
*notifier.Notification
|
|
||||||
|
type example struct {
|
||||||
|
*notifications.Notification
|
||||||
}
|
}
|
||||||
|
|
||||||
var example = &Example{¬ifier.Notification{
|
func (t *example) Select() *notifications.Notification {
|
||||||
Method: METHOD,
|
return t.Notification
|
||||||
Title: "Example",
|
}
|
||||||
Description: "Example Notifier",
|
|
||||||
|
var Example = &example{¬ifications.Notification{
|
||||||
|
Method: "example",
|
||||||
|
Title: "Example Notifier",
|
||||||
|
Description: "This notifier only prints messages in the console.",
|
||||||
Author: "Hunter Long",
|
Author: "Hunter Long",
|
||||||
AuthorUrl: "https://github.com/hunterlong",
|
AuthorUrl: "https://github.com/hunterlong",
|
||||||
Delay: time.Duration(5 * time.Second),
|
Icon: "fa dot-circle",
|
||||||
Form: []notifier.NotificationForm{{
|
Delay: time.Duration(10 * time.Second),
|
||||||
|
Limits: 60,
|
||||||
|
Form: []notifications.NotificationForm{{
|
||||||
Type: "text",
|
Type: "text",
|
||||||
Title: "Host",
|
Title: "User Token",
|
||||||
Placeholder: "Insert your Host here.",
|
Placeholder: "Insert your Token",
|
||||||
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",
|
|
||||||
DbField: "api_key",
|
DbField: "api_key",
|
||||||
|
Required: true,
|
||||||
}, {
|
}, {
|
||||||
Type: "text",
|
Type: "text",
|
||||||
Title: "API Secret",
|
Title: "Application API Key",
|
||||||
Placeholder: "Insert your API Secret here",
|
Placeholder: "Insert something secret here",
|
||||||
DbField: "api_secret",
|
DbField: "api_secret",
|
||||||
}, {
|
Required: true,
|
||||||
Type: "text",
|
},
|
||||||
Title: "Var 1",
|
|
||||||
Placeholder: "Insert your Var1 here",
|
|
||||||
DbField: "var1",
|
|
||||||
}, {
|
|
||||||
Type: "text",
|
|
||||||
Title: "Var2",
|
|
||||||
Placeholder: "Var2 goes here",
|
|
||||||
DbField: "var2",
|
|
||||||
}},
|
}},
|
||||||
}}
|
|
||||||
|
|
||||||
// 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
|
// OnFailure will trigger failing service
|
||||||
func (n *Example) Send(msg interface{}) error {
|
func (t *example) OnFailure(s *services.Service, f *failures.Failure) error {
|
||||||
message := msg.(string)
|
msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name)
|
||||||
fmt.Printf("i received this string: %v\n", message)
|
fmt.Println(msg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// REQUIRED
|
// OnSuccess will trigger successful service
|
||||||
func (n *Example) Select() *notifier.Notification {
|
func (t *example) OnSuccess(s *services.Service) error {
|
||||||
return n.Notification
|
msg := fmt.Sprintf("Your service '%v' is currently online!", s.Name)
|
||||||
}
|
fmt.Println(msg)
|
||||||
|
|
||||||
// REQUIRED
|
|
||||||
func (n *Example) OnSave() error {
|
|
||||||
msg := fmt.Sprintf("received on save trigger")
|
|
||||||
n.AddQueue(msg)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// REQUIRED
|
// OnTest will test the example notifier
|
||||||
func (n *Example) Test() error {
|
func (t *example) OnTest() (string, error) {
|
||||||
msg := fmt.Sprintf("received a test trigger\n")
|
msg := fmt.Sprintf("Testing the Pushover Notifier")
|
||||||
n.AddQueue(msg)
|
fmt.Println(msg)
|
||||||
return nil
|
return msg, 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)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue