telegram_notifier

package module
v0.0.0-...-bf41ca6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2023 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Telegram Notifier

Ubuntu-latest Macos-latest Windows-latest Ubuntu-coverage

The telegram_notifier package TODO.

Code example:

// TODO

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultSendTimeoutSec is the default timeout in seconds
	// to send a telegram message.
	DefaultSendTimeoutSec = 5

	// DefaultMsgBufSize is the default message buffer size for
	// TelegramMessage channel.
	DefaultMsgBufSize = 50
)
View Source
var (
	ErrUnitNotAvailable = errors.New("unit not available")

	ErrBadLogLevel = errors.New("bad log level")

	ErrBadTelegramBotToken = errors.New("bad telegram bot token")

	ErrBadTelegramChatId = errors.New("bad telegram chat ID")

	ErrLogTelegramConfigIsNil = errors.New("log telegram config is nil")
)

Errors

Functions

This section is empty.

Types

type Config

type Config struct {
	// BotToken specifies the Telegram bot secret token.
	BotToken string `yaml:"bot_token" json:"bot_token"`

	// BotTokenEnvVar specifies the name of the environment variable
	// that contains BotToken for current telegram notifier.
	// This allows for more versatile and secure configuration.
	// The environment variable has precedence over the BotToken value.
	BotTokenEnvVar string `yaml:"bot_token_env_var" json:"bot_token_env_var"`

	// ChatIds specifies the receivers of notifications.
	ChatIds []int64 `yaml:"chat_ids" json:"chat_ids"`

	// ChatIdsEnvVar specifies the name of the environment variable
	// that contains comma-separated ChatIds for current telegram notifier.
	ChatIdsEnvVar string `yaml:"chat_ids_env_var" json:"chat_ids_env_var"`

	// LogLevels define the log levels the messages must have to be send to Telegram
	// when integrated with `igulib/app_logger`.
	// If none specified, no messages will be sent via Telegram.
	LogLevels []string `yaml:"log_levels" json:"log_levels"`

	// LogOnlyWithPrefixes defines the prefixes that a log message must start with
	// in order to be sent to Telegram chats.
	// If a message starts with any of these prefixes, it will be sent via Telegram.
	// If prefixes not specified, all messages with the appropriate log level
	// will be sent.
	// This setting only has effect for log messages from `igulib/app_logger`
	// when integrated with it via zerolog.Hook.
	LogOnlyWithPrefixes []string `yaml:"log_only_with_prefixes" json:"log_only_with_prefixes"`

	// LogDateTime enables appending date and time to the log message.
	LogDateTime bool `yaml:"log_date_time" json:"log_date_time"`

	// LogUseUTC enables UTC time instead of local if LogDateTime is true.
	LogUseUTC bool `yaml:"log_use_utc" json:"log_use_utc"`
}

func ParseYamlConfig

func ParseYamlConfig(data []byte) (*Config, error)

type TelegramMessage

type TelegramMessage struct {
	Title string
	Text  string
}

type TelegramNotifier

type TelegramNotifier struct {
	// contains filtered or unexported fields
}

TelegramNotifier unit. Do not instantiate TelegramNotifier directly, use the New function instead.

func AddNew

func AddNew(unitName string, c *Config) (*TelegramNotifier, error)

AddNew creates a new TelegramNotifier unit and adds it into the default app unit manager (app.M). This function should be used instead of direct construction of TelegramNotifier.

func New

func New(unitName string, c *Config) (*TelegramNotifier, error)

New creates a new TelegramNotifier unit. This function should be used instead of direct construction of TelegramNotifier.

func (*TelegramNotifier) Run

func (u *TelegramNotifier) Run(
	e *zerolog.Event,
	level zerolog.Level,
	message string,
)

Run implements zerolog.Hook.

func (*TelegramNotifier) SendAsync

func (u *TelegramNotifier) SendAsync(title, text string) error

SendAsync asynchronously sends the message via Telegram, it is thread-safe.

func (*TelegramNotifier) SetLogMessageTitleSuffix

func (u *TelegramNotifier) SetLogMessageTitleSuffix(appName string)

SetLogMessageTitleSuffix sets an optional suffix to the log message title when log messages are forwarded from `igulib/app_logger`. This method has no effect if current `TelegramNotifier` not used as a hook at `igulib/app_logger`. Message title example without suffix: `ERROR`, with suffix: `ERROR | my-suffix`.

func (*TelegramNotifier) UnitAvailability

func (u *TelegramNotifier) UnitAvailability() app.UnitAvailability

UnitAvailability implements app.IUnit.

func (*TelegramNotifier) UnitPause

func (u *TelegramNotifier) UnitPause() app.UnitOperationResult

UnitPause implements app.IUnit.

func (*TelegramNotifier) UnitQuit

UnitQuit implements app.IUnit.

func (*TelegramNotifier) UnitRunner

func (u *TelegramNotifier) UnitRunner() *app.UnitLifecycleRunner

UnitRunner implements app.IUnit.

func (*TelegramNotifier) UnitStart

func (u *TelegramNotifier) UnitStart() app.UnitOperationResult

UnitStart implements app.IUnit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址