Skip to content

Commit 9f0ecef

Browse files
committed
enable setting the chatid in the config. always log chatid if it's unset and we have a message from the target username.
1 parent 419999a commit 9f0ecef

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

checkrr.yaml.example

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ notifications:
8282
telegram:
8383
apitoken: ""
8484
username: "@username" # This must start with an @ to send to a user, otherwise, list the channel name
85+
chatid: 0 # Start checkrr, DM the bot, and then trigger a run. Checkrr will log the chatid to the console. Place the chatid value here.
8586
notificationtypes:
8687
- reacquire
8788
- unknowndetected

notifications/telegram.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)