Categorygithub.com/obalunenko/notifier
modulepackage
0.2.0
Repository: https://github.com/obalunenko/notifier.git
Documentation: pkg.go.dev

# README

GitHub go.mod Go version GoDoc Latest release artifacts Go Report Card Go [lint, test] CodeQL coverbadger-tag-do-not-edit

notifier

Package notifier provides functionality to send notifications.

Examples

Notifier

ExampleNotifier shows how to create a new notifier with a list of notifiers and send a message.

package main

import (
	"bytes"
	"context"
	"fmt"
	"os"
	"time"

	"github.com/obalunenko/notifier"
)

const (
	// Test telegram credentials.
	testTGTokenEnv  = "TEST_TELEGRAM_TOKEN"
	testTGChatIDEnv = "TEST_TELEGRAM_CHAT_ID"
)

// ExampleNotifier shows how to create a new notifier with a list of notifiers and send a message.
func main() {
	ctx := context.Background()

	var buf bytes.Buffer

	// Declare list of notifiers.
	var notifiers []notifier.Notifier

	// Create a new io.Writer notifier to visualize alerts.
	wn, err := notifier.NewIOWriterNotifier(&buf)
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	notifiers = append(notifiers, wn)

	// Create a new telegram notifier if token and chatID env set.
	if token, chatID := os.Getenv(testTGTokenEnv), os.Getenv(testTGChatIDEnv); token != "" && chatID != "" {
		tgn, err := notifier.NewTelegram(token, chatID)
		if err != nil {
			// Handle error in your way.
			panic(err)
		}

		notifiers = append(notifiers, tgn)
	}

	// Add the notifier to the list of notifiers.
	n, err := notifier.NewMultiNotifier(notifiers...)
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	// Add notifier metadata to context.
	ctx = notifier.ContextWithMetadata(ctx, notifier.Metadata{
		AppName:      "test_app",
		InstanceName: "test_instance",
		Commit:       "test_commit",
		BuildDate:    time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.DateTime),
	})

	// Send alert.
	err = n.Alert(ctx, notifier.SeverityWarning, "[NOTIFIER_TEST]: example message")
	if err != nil {
		// Handle error in your way.
		panic(err)
	}

	fmt.Println(buf.String())
}

# Functions

ContextWithMetadata returns a new context with the given metadata.
MetadataFromContext returns the metadata stored in ctx, if any.
NewIOWriterNotifier creates a new notifier that writes messages to io.Writer.
NewMultiNotifier returns a new multiNotifier notifier.
NewTelegram returns a new telegram notifier.

# Constants

# Variables

ErrEmptyMessage is returned when the message is empty.
ErrEmptyNotifiers is returned when the notifiers' list is empty.
ErrEmptyTelegramChatID is returned when the telegram chat id is empty.
ErrEmptyTelegramToken is returned when the telegram token is empty.
ErrInvalidSeverity is returned when the severity is invalid.
ErrInvalidToken is returned when the telegram token is invalid.

# Structs

Metadata contains information about the application.

# Interfaces

Notifier declares notifier contract.

# Type aliases

Severity represents the severity of an alert.