Add config to send messages to a specific Telegram topic

This commit is contained in:
Rithas K 2024-06-02 00:12:55 +05:30
parent 7cb1ec1bba
commit 4910d2ca55
No known key found for this signature in database
2 changed files with 7 additions and 1 deletions

View file

@ -85,7 +85,12 @@ func NotifyTelegram(msg string) {
return return
} }
client := &http.Client{} client := &http.Client{}
var data = strings.NewReader("chat_id=" + Cfg.TelegramChat + "&text=" + msg) var data *strings.Reader
if Cfg.TelegramToken != "" {
data = strings.NewReader("chat_id=" + Cfg.TelegramChat + "&message_thread_id=" + Cfg.TelegramTopic + "&text=" + msg)
} else {
data = strings.NewReader("chat_id=" + Cfg.TelegramChat + "&text=" + msg)
}
req, err := http.NewRequest("POST", "https://api.telegram.org/bot"+Cfg.TelegramToken+"/sendMessage", data) req, err := http.NewRequest("POST", "https://api.telegram.org/bot"+Cfg.TelegramToken+"/sendMessage", data)
if err != nil { if err != nil {
slog.Error("failed telegram request", "error", err) slog.Error("failed telegram request", "error", err)

View file

@ -30,6 +30,7 @@ type Config struct {
TelegramToken string `config:"tg_token" type:"string"` TelegramToken string `config:"tg_token" type:"string"`
TelegramChat string `config:"tg_chat" type:"string"` TelegramChat string `config:"tg_chat" type:"string"`
TelegramTopic string `config:"tg_topic" type:"string"`
} }
var DefaultConfig = Config{ var DefaultConfig = Config{