Skip to content

Notifier Example

Hunter Long edited this page May 1, 2020 · 12 revisions

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

package notifiers

import (
	"fmt"
	"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"
)

var _ notifier.Notifier = (*example)(nil)

type example struct {
	*notifications.Notification
}

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",
	Icon:        "fa dot-circle",
	Delay:       time.Duration(10 * time.Second),
	Limits:      60,
	Form: []notifications.NotificationForm{{
		Type:        "text",
		Title:       "User Token",
		Placeholder: "Insert your Token",
		DbField:     "api_key",
		Required:    true,
	}, {
		Type:        "text",
		Title:       "Application API Key",
		Placeholder: "Insert something secret here",
		DbField:     "api_secret",
		Required:    true,
	},
	}},
}

// 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
}

// 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
}

// 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
}