alertmanager/notify/telegram/telegram.go

130 lines
3.5 KiB
Go
Raw Normal View History

Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
// Copyright 2022 Prometheus Team
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package telegram
import (
"context"
"fmt"
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
"net/http"
"os"
"strings"
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
"github.com/go-kit/log"
"github.com/go-kit/log/level"
commoncfg "github.com/prometheus/common/config"
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
"gopkg.in/telebot.v3"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/notify"
"github.com/prometheus/alertmanager/template"
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
"github.com/prometheus/alertmanager/types"
)
// Telegram supports 4096 chars max - from https://limits.tginfo.me/en.
const maxMessageLenRunes = 4096
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
// Notifier implements a Notifier for telegram notifications.
type Notifier struct {
conf *config.TelegramConfig
tmpl *template.Template
logger log.Logger
client *telebot.Bot
retrier *notify.Retrier
}
// New returns a new Telegram notification handler.
func New(conf *config.TelegramConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
httpclient, err := commoncfg.NewClientFromConfig(*conf.HTTPConfig, "telegram", httpOpts...)
if err != nil {
return nil, err
}
client, err := createTelegramClient(conf.APIUrl.String(), conf.ParseMode, httpclient)
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
if err != nil {
return nil, err
}
return &Notifier{
conf: conf,
tmpl: t,
logger: l,
client: client,
retrier: &notify.Retrier{},
}, nil
}
func (n *Notifier) Notify(ctx context.Context, alert ...*types.Alert) (bool, error) {
var (
err error
data = notify.GetTemplateData(ctx, n.tmpl, alert, n.logger)
tmpl = notify.TmplText(n.tmpl, data, &err)
)
if n.conf.ParseMode == "HTML" {
tmpl = notify.TmplHTML(n.tmpl, data, &err)
}
key, ok := notify.GroupKey(ctx)
if !ok {
return false, fmt.Errorf("group key missing")
}
messageText, truncated := notify.TruncateInRunes(tmpl(n.conf.Message), maxMessageLenRunes)
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
if truncated {
level.Warn(n.logger).Log("msg", "Truncated message", "alert", key, "max_runes", maxMessageLenRunes)
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
}
n.client.Token, err = n.getBotToken()
if err != nil {
return true, err
}
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
message, err := n.client.Send(telebot.ChatID(n.conf.ChatID), messageText, &telebot.SendOptions{
DisableNotification: n.conf.DisableNotifications,
DisableWebPagePreview: true,
ThreadID: n.conf.MessageThreadID,
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
})
if err != nil {
return true, err
}
level.Debug(n.logger).Log("msg", "Telegram message successfully published", "message_id", message.ID, "chat_id", message.Chat.ID)
return false, nil
}
func createTelegramClient(apiURL, parseMode string, httpClient *http.Client) (*telebot.Bot, error) {
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
bot, err := telebot.NewBot(telebot.Settings{
URL: apiURL,
Adding telegram support (#2827) * added telegram config in notifiers Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram config to config.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added package telegram to notify and initialization in cmd Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram.default.message Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added telegram template to notifiers.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added message rendering from template to telegram integration Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * documentation for telegram_configs Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added tests for telegram Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed config_test.go Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added valid parse_mode list to the error log Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * made use of httpconfig, moved telegram client creation to New func, changed message truncation to 4096 chars Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed supported values for telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added mock url for telegram tests to pass Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * added api_url check to telegram config Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * changed bot_token type to secret Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * removed extra emptyline Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * convert bot_token to string in telegram client creation Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * updated configuration.md Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * fixed mixed up errors Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com> * check telegram api url without conversion to string Signed-off-by: timmilesdw <tuktamyshev.t0308@gmail.com>
2022-02-22 16:51:02 +00:00
ParseMode: parseMode,
Client: httpClient,
Offline: true,
})
if err != nil {
return nil, err
}
return bot, nil
}
func (n *Notifier) getBotToken() (string, error) {
if len(n.conf.BotTokenFile) > 0 {
content, err := os.ReadFile(n.conf.BotTokenFile)
if err != nil {
return "", fmt.Errorf("could not read %s: %w", n.conf.BotTokenFile, err)
}
return strings.TrimSpace(string(content)), nil
}
return string(n.conf.BotToken), nil
}