Skip to content

Conversation

@tennisleng
Copy link

This PR adds support for reading chat_id from a file, similar to bot_token_file. This allows users to securely pass chat_id using secrets or volumes in docker-compose without exposing it in cleartext in the config file.

Changes

  • Added ChatIDFile field to TelegramConfig struct
  • Updated validation to require either chat_id or chat_id_file (but not both)
  • Added getChatID() method to read chat_id from file
  • Updated Notify() method to use the new getChatID() method

Testing

  • Follows the same pattern as bot_token_file
  • Validates that either chat_id or chat_id_file is provided
  • Validates that both are not provided simultaneously
  • Reads and parses chat_id from file as int64

Fixes #4672


func (n *Notifier) getChatID() (int64, error) {
if len(n.conf.ChatIDFile) > 0 {
content, err := os.ReadFile(n.conf.ChatIDFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect the contents to change as we go? Would we consider saving the chatID to n.conf.ChatID after we read it, and then in this function return that one first, and only read the file if ChatID is still 0?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the caching you suggested - getChatID() now returns the cached value first and only reads from file if ChatID is still 0, then caches it for future calls. This avoids the file I/O on every notification

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you removed this improvement when you removed the unrelated changes from the branch... You could cherry-pick just this one patch on top, or push to a version that has just those.

@tennisleng tennisleng force-pushed the fix/telegram-chat-id-file branch from 08960ca to ff16850 Compare November 11, 2025 02:10
Copy link
Contributor

@ultrotter ultrotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you remove the unrelated changes from this branch? Otherwise I don't think it can be merged, as is...

Thanks!

G

This adds support for reading chat_id from a file, similar to bot_token_file.
This allows users to securely pass chat_id using secrets or volumes in
docker-compose without exposing it in cleartext in the config file.

Fixes prometheus#4672

Signed-off-by: tennisleng <tennisleng@users.noreply.github.com>
@tennisleng tennisleng force-pushed the fix/telegram-chat-id-file branch from 7585500 to 29885e2 Compare November 11, 2025 20:06
@TheMeier
Copy link
Contributor

If chat_id is considered a secret it also get mapped to a secret type. Right now it is mapeed to int and is visible unmasked in /#/status

@TheMeier
Copy link
Contributor

related #2498

@tennisleng tennisleng closed this Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add chat_id_file configuration parameter for Telegram

4 participants