From d9982a24b5ed737bd1c05f22d984cd6dab1b69d4 Mon Sep 17 00:00:00 2001 From: Phillipp Engelke Date: Sun, 4 Apr 2021 17:52:33 +0200 Subject: [PATCH] Add Telegram Notification --- go.mod | 2 ++ notifiers/telegram.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 notifiers/telegram.go diff --git a/go.mod b/go.mod index 39cc6a2..a6c0b13 100644 --- a/go.mod +++ b/go.mod @@ -7,12 +7,14 @@ require ( github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 github.com/fsnotify/fsnotify v1.4.7 github.com/gin-gonic/gin v1.5.0 + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect github.com/golang/protobuf v1.3.3 // indirect github.com/hashicorp/hcl/v2 v2.2.0 github.com/keymon/scheduler v0.0.0-20180613200400-c8fecd8965b9 github.com/markbates/pkger v0.14.0 github.com/matcornic/hermes/v2 v2.0.2 github.com/rs/zerolog v1.18.0 + github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/teris-io/cli v1.0.1 golang.org/x/crypto v0.0.0-20200214034016-1d94cc7ab1c6 // indirect golang.org/x/net v0.0.0-20200202094626-16171245cfb2 // indirect diff --git a/notifiers/telegram.go b/notifiers/telegram.go new file mode 100644 index 0000000..c34496f --- /dev/null +++ b/notifiers/telegram.go @@ -0,0 +1,35 @@ +package notifiers + +import ( + "codeberg.org/momar/chihuahua" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/rs/zerolog/log" +) + +type TelegramNotifier struct { + Token string `hcl:"token"` + ChatID int64 `hcl:"chat"` +} + +func init() { + chihuahua.Notifiers["telegram"] = &TelegramNotifier{} +} + +func (c *TelegramNotifier) Notify(cfg *chihuahua.Config, check chihuahua.Check, previous chihuahua.CheckResult) { + bot, err := tgbotapi.NewBotAPI(c.Token) + if err != nil { + log.Error().Err(err).Msg("Failed to connect to Bot API") + return + } + + msg := tgbotapi.NewMessage(c.ChatID, Content(cfg, check, previous)) + + _, err = bot.Send(msg) + if err != nil { + log.Error().Err(err).Msg("Failed to send Message to Chat") + return + } + + log.Trace().Msg("Notification send") +} + -- 2.30.2