Skip to content

Commit

Permalink
enable setting the chatid in the config. always log chatid if it's un…
Browse files Browse the repository at this point in the history
…set and we have a message from the target username.
  • Loading branch information
aetaric committed Dec 15, 2023
1 parent 419999a commit 9f0ecef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions checkrr.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ notifications:
telegram:
apitoken: ""
username: "@username" # This must start with an @ to send to a user, otherwise, list the channel name
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.
notificationtypes:
- reacquire
- unknowndetected
Expand Down
31 changes: 17 additions & 14 deletions notifications/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Telegram struct {
AllowedNotifs []string
apiToken string
username string
chatid int64
bot *tgbotapi.BotAPI
Log log.Logger
}
Expand All @@ -25,20 +26,7 @@ func (t Telegram) Notify(title string, description string, notifType string, pat
}
}
if allowed {
var chatid int64
updates, err := t.bot.GetUpdates(tgbotapi.UpdateConfig{})
if err != nil {
return false
}
for _, update := range updates {
t.Log.Debug(fmt.Sprintf("User: %s", update.SentFrom().UserName))
if fmt.Sprintf("@%s", update.SentFrom().UserName) == t.username {
chatid = update.FromChat().ID
t.Log.Debug(fmt.Sprintf("chatid: %v", chatid))
break
}
}
message := tgbotapi.NewMessage(chatid, description)
message := tgbotapi.NewMessage(t.chatid, description)
t.bot.Send(message)
return true
}
Expand All @@ -54,6 +42,20 @@ func (t *Telegram) Connect() bool {
t.Log.WithFields(log.Fields{"Error": err, "Username": t.username, "Token": t.apiToken}).Warn("Error connecting to Telegram")
return false
}
updates, err := t.bot.GetUpdates(tgbotapi.UpdateConfig{})
if err != nil {
return false
}
if t.chatid == 0 {
for _, update := range updates {
t.Log.Debug(fmt.Sprintf("User: %s", update.SentFrom().UserName))
if fmt.Sprintf("@%s", update.SentFrom().UserName) == t.username {
t.chatid = update.FromChat().ID
t.Log.Infof("Telegram chatid: %v", t.chatid)
break
}
}
}
t.Log.Info("Connected to Telegram")
return true
}
Expand All @@ -62,5 +64,6 @@ func (t *Telegram) FromConfig(config viper.Viper) {
t.config = config
t.apiToken = config.GetString("apitoken")
t.username = config.GetString("username")
t.chatid = config.GetInt64("chatid")
t.AllowedNotifs = config.GetStringSlice("notificationtypes")
}

0 comments on commit 9f0ecef

Please sign in to comment.