@@ -13,6 +13,7 @@ type Telegram struct {
1313 AllowedNotifs []string
1414 apiToken string
1515 username string
16+ chatid int64
1617 bot * tgbotapi.BotAPI
1718 Log log.Logger
1819}
@@ -25,20 +26,7 @@ func (t Telegram) Notify(title string, description string, notifType string, pat
2526 }
2627 }
2728 if allowed {
28- var chatid int64
29- updates , err := t .bot .GetUpdates (tgbotapi.UpdateConfig {})
30- if err != nil {
31- return false
32- }
33- for _ , update := range updates {
34- t .Log .Debug (fmt .Sprintf ("User: %s" , update .SentFrom ().UserName ))
35- if fmt .Sprintf ("@%s" , update .SentFrom ().UserName ) == t .username {
36- chatid = update .FromChat ().ID
37- t .Log .Debug (fmt .Sprintf ("chatid: %v" , chatid ))
38- break
39- }
40- }
41- message := tgbotapi .NewMessage (chatid , description )
29+ message := tgbotapi .NewMessage (t .chatid , description )
4230 t .bot .Send (message )
4331 return true
4432 }
@@ -54,6 +42,20 @@ func (t *Telegram) Connect() bool {
5442 t .Log .WithFields (log.Fields {"Error" : err , "Username" : t .username , "Token" : t .apiToken }).Warn ("Error connecting to Telegram" )
5543 return false
5644 }
45+ updates , err := t .bot .GetUpdates (tgbotapi.UpdateConfig {})
46+ if err != nil {
47+ return false
48+ }
49+ if t .chatid == 0 {
50+ for _ , update := range updates {
51+ t .Log .Debug (fmt .Sprintf ("User: %s" , update .SentFrom ().UserName ))
52+ if fmt .Sprintf ("@%s" , update .SentFrom ().UserName ) == t .username {
53+ t .chatid = update .FromChat ().ID
54+ t .Log .Infof ("Telegram chatid: %v" , t .chatid )
55+ break
56+ }
57+ }
58+ }
5759 t .Log .Info ("Connected to Telegram" )
5860 return true
5961}
@@ -62,5 +64,6 @@ func (t *Telegram) FromConfig(config viper.Viper) {
6264 t .config = config
6365 t .apiToken = config .GetString ("apitoken" )
6466 t .username = config .GetString ("username" )
67+ t .chatid = config .GetInt64 ("chatid" )
6568 t .AllowedNotifs = config .GetStringSlice ("notificationtypes" )
6669}
0 commit comments