@@ -13,6 +13,7 @@ type Telegram struct {
13
13
AllowedNotifs []string
14
14
apiToken string
15
15
username string
16
+ chatid int64
16
17
bot * tgbotapi.BotAPI
17
18
Log log.Logger
18
19
}
@@ -25,20 +26,7 @@ func (t Telegram) Notify(title string, description string, notifType string, pat
25
26
}
26
27
}
27
28
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 )
42
30
t .bot .Send (message )
43
31
return true
44
32
}
@@ -54,6 +42,20 @@ func (t *Telegram) Connect() bool {
54
42
t .Log .WithFields (log.Fields {"Error" : err , "Username" : t .username , "Token" : t .apiToken }).Warn ("Error connecting to Telegram" )
55
43
return false
56
44
}
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
+ }
57
59
t .Log .Info ("Connected to Telegram" )
58
60
return true
59
61
}
@@ -62,5 +64,6 @@ func (t *Telegram) FromConfig(config viper.Viper) {
62
64
t .config = config
63
65
t .apiToken = config .GetString ("apitoken" )
64
66
t .username = config .GetString ("username" )
67
+ t .chatid = config .GetInt64 ("chatid" )
65
68
t .AllowedNotifs = config .GetStringSlice ("notificationtypes" )
66
69
}
0 commit comments