telegram

package
v0.0.0-...-4f76211 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CommandStart = "/start"
	CommandStop  = "/stop"
	CommandHelp  = "/help"
	CommandChats = "/chats"
	CommandID    = "/id"

	CommandStatus       = "/status"
	CommandAlerts       = "/alerts"
	CommandSilences     = "/silences"
	CommandMute         = "/mute"
	CommandMuteDel      = "/mute_del"
	CommandEnvironments = "/environments"
	CommandProjects     = "/projects"
	CommandMutedEnvs    = "/muted_envs"
	CommandMutedPrs     = "/muted_prs"

	ProjectAndEnvironmentMuteRegexp   = `/mute environment\[(\w+(\s*,\s*\w+)*)\],[ ]?project\[(\w+(\s*,\s*\w+)*)\]`
	MuteProjectRegexp                 = `/mute project\[(\w+(\s*,\s*\w+)*)\]`
	MuteEnvironmentRegexp             = `/mute environment\[(\w+(\s*,\s*\w+)*)\]`
	ProjectAndEnvironmentUnmuteRegexp = `/mute_del environment\[(\w+(\s*,\s*\w+)*)\],[ ]?project\[(\w+(\s*,\s*\w+)*)\]`
	UnmuteProjectRegexp               = `/mute_del project\[(\w+(\s*,\s*\w+)*)\]`
	UnmuteEnvironmentRegexp           = `/mute_del environment\[(\w+(\s*,\s*\w+)*)\]`
	EnvironmentValuesRegexp           = `environment\[(.*?)\]`
	ProjectValuesRegexp               = `project\[(.*?)\]`

	ResponseHelp = `
I'm a Prometheus AlertManager Bot for Telegram. I will notify you about alerts.
You can also ask me about my ` + CommandStatus + `, ` + CommandAlerts + ` & ` + CommandSilences + `

Available commands:
` + CommandStart + ` - Subscribe for alerts.
` + CommandStop + ` - Unsubscribe for alerts.
` + CommandStatus + ` - Print the current status.
` + CommandAlerts + ` - List all alerts.
` + CommandSilences + ` - List all silences.
` + CommandChats + ` - List all users and group chats that subscribed.
` + CommandID + ` - Send the senders Telegram ID (works for all Telegram users).
` + CommandMute + ` - Mute environments and/or projects.
` + CommandMuteDel + ` - Delete mute.
` + CommandEnvironments + ` - List all environments for alerts.
` + CommandProjects + ` - List all projects for alerts.
` + CommandMutedEnvs + ` - List all muted environments.
` + CommandMutedPrs + ` - List all muted projects.
`
)

Variables

View Source
var ChatNotFoundErr = errors.New("chat not found in store")

ChatNotFoundErr returned by the store if a chat isn't found.

Functions

This section is empty.

Types

type Alertmanager

type Alertmanager interface {
	ListAlerts(context.Context, string, bool) ([]*types.Alert, error)
	ListSilences(context.Context) ([]*types.Silence, error)
	Status(context.Context) (*models.AlertmanagerStatus, error)
}

type Bot

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

Bot runs the alertmanager telegram.

func NewBot

func NewBot(chats BotChatStore, token string, admin int, opts ...BotOption) (*Bot, error)

NewBot creates a Bot with the UserStore and telegram telegram.

func NewBotWithTelegram

func NewBotWithTelegram(chats BotChatStore, bot Telebot, admin int, opts ...BotOption) (*Bot, error)

func (*Bot) Run

func (b *Bot) Run(ctx context.Context, webhooks <-chan alertmanager.TelegramWebhook) error

Run the telegram and listen to messages send to the telegram.

func (*Bot) SendAdminMessage

func (b *Bot) SendAdminMessage(adminID int, message string)

SendAdminMessage to the admin's ID with a message.

type BotChatStore

type BotChatStore interface {
	List() ([]ChatInfo, error)
	Get(telebot.ChatID) (*telebot.Chat, error, *store.KVPair)
	AddChat(*telebot.Chat, []string, []string) error
	RemoveChat(*telebot.Chat) error
	MuteEnvironments(*telebot.Chat, []string, []string) error
	MuteProjects(*telebot.Chat, []string, []string) error
	UnmuteEnvironment(*telebot.Chat, string, []string) error
	UnmuteProject(*telebot.Chat, string, []string) error
	MutedEnvironments(*telebot.Chat) ([]string, error)
	MutedProjects(*telebot.Chat) ([]string, error)
}

BotChatStore is all the Bot needs to store and read.

type BotOption

type BotOption func(b *Bot) error

BotOption passed to NewBot to change the default instance.

func WithAddr

func WithAddr(addr string) BotOption

WithAddr sets the internal listening addr of the bot's web server receiving webhooks.

func WithAlertmanager

func WithAlertmanager(alertmanager Alertmanager) BotOption

func WithCommandEvent

func WithCommandEvent(callback func(command string)) BotOption

WithCommandEvent sets a func to call whenever commands are handled.

func WithDeletePeriod

func WithDeletePeriod(deletePeriod float64) BotOption

WithDeletePeriod allows to define period of deleting messages

func WithEnvironments

func WithEnvironments(environmentsToUse string) BotOption

WithEnvironments allows to define environments that are monitored by Prometheus

func WithExtraAdmins

func WithExtraAdmins(ids ...int) BotOption

WithExtraAdmins allows the specified additional user IDs to issue admin commands to the bot.

func WithFetchPeriod

func WithFetchPeriod(fetchPeriod float64) BotOption

WithFetchPeriod allows to define scheduler period for fetching messages from store

func WithLogger

func WithLogger(l log.Logger) BotOption

WithLogger sets the logger for the Bot as an option.

func WithProjects

func WithProjects(projectsToUse string) BotOption

WithProjects allows to define projects that are monitored by Prometheus

func WithRevision

func WithRevision(r string) BotOption

WithRevision is setting the Bot's revision for status commands.

func WithStartTime

func WithStartTime(st time.Time) BotOption

WithStartTime is setting the Bot's start time for status commands.

func WithTemplates

func WithTemplates(alertmanager *url.URL, templatePaths ...string) BotOption

WithTemplates uses Alertmanager template to render messages for Telegram.

type ChatInfo

type ChatInfo struct {
	Chat              *telebot.Chat
	AlertEnvironments []string
	AlertProjects     []string
	MutedEnvironments []string
	MutedProjects     []string
}

func (*ChatInfo) MuteEnvironments

func (ch *ChatInfo) MuteEnvironments(envsToMute []string, allEnvs []string)

func (*ChatInfo) MuteProjects

func (ch *ChatInfo) MuteProjects(prsToMute []string, allPrs []string)

func (*ChatInfo) UnmuteEnvironment

func (ch *ChatInfo) UnmuteEnvironment(env string, allEnvs []string)

func (*ChatInfo) UnmuteProject

func (ch *ChatInfo) UnmuteProject(pr string, allPrs []string)

type ChatStore

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

ChatStore writes the users to a libkv store backend.

func NewChatStore

func NewChatStore(kv store.Store, storeKeyPrefix string) (*ChatStore, error)

NewChatStore stores telegram chats in the provided kv backend.

func (*ChatStore) AddChat

func (s *ChatStore) AddChat(c *telebot.Chat, allEnvs []string, allPrs []string) error

AddChat Add a telegram chat to the kv backend.

func (*ChatStore) Get

func (s *ChatStore) Get(id telebot.ChatID) (*telebot.Chat, error, *store.KVPair)

func (*ChatStore) List

func (s *ChatStore) List() ([]ChatInfo, error)

List all chats saved in the kv backend.

func (*ChatStore) MuteEnvironments

func (s *ChatStore) MuteEnvironments(c *telebot.Chat, envsToMute []string, allEnvs []string) error

func (*ChatStore) MuteProjects

func (s *ChatStore) MuteProjects(c *telebot.Chat, prsToMute []string, allPrs []string) error

func (*ChatStore) MutedEnvironments

func (s *ChatStore) MutedEnvironments(c *telebot.Chat) ([]string, error)

func (*ChatStore) MutedProjects

func (s *ChatStore) MutedProjects(c *telebot.Chat) ([]string, error)

func (*ChatStore) RemoveChat

func (s *ChatStore) RemoveChat(c *telebot.Chat) error

RemoveChat Remove a telegram chat from the kv backend.

func (*ChatStore) UnmuteEnvironment

func (s *ChatStore) UnmuteEnvironment(c *telebot.Chat, envToUnmute string, allEnvs []string) error

func (*ChatStore) UnmuteProject

func (s *ChatStore) UnmuteProject(c *telebot.Chat, prToUnmute string, allPrs []string) error

type Telebot

type Telebot interface {
	Start()
	Stop()
	Send(to telebot.Recipient, what interface{}, options ...interface{}) (*telebot.Message, error)
	Notify(to telebot.Recipient, action telebot.ChatAction) error
	Handle(endpoint interface{}, handler interface{})
}

Jump to

Keyboard shortcuts

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