Skip to content

Commit

Permalink
fix(telegram): support only one topicID per chatID
Browse files Browse the repository at this point in the history
  • Loading branch information
jon4hz committed Mar 24, 2024
1 parent b840e97 commit f20bde8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/notif/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Multiple chat IDs can be provided in order to deliver notifications to multiple
| `tokenFile` | | Use content of secret file as Telegram bot token if `token` not defined |
| `chatIDs` | | List of chat IDs to send notifications to |
| `chatIDsFile` | | Use content of secret file as chat IDs if `chatIDs` not defined |
| `chatTopics` | | Nested List of chat topic IDs to send notifications to. |
| `chatTopics` | | List of chat topic IDs to send notifications to. |
| `chatTopicsFile` | | Use content of secret file as chat topic IDs if `chatTopics` not defined |
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |

Expand All @@ -42,7 +42,7 @@ Multiple chat IDs can be provided in order to deliver notifications to multiple
Chat IDs secret file must be a valid JSON array like: `[123456789,987654321]`

!!! example "chat topic IDS secret file"
Chat topics is a nested array, so you can specify multiple topics per chat ID: `[[10,15][10,20]]`
Chat topics is also an array, so you can specify a topic ID per chat ID: `[10,20]`

### Default `templateBody`

Expand Down
14 changes: 7 additions & 7 deletions internal/model/notif_telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const NotifTelegramDefaultTemplateBody = `Docker tag {{ if .Entry.Image.HubLink

// NotifTelegram holds Telegram notification configuration details
type NotifTelegram struct {
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
ChatIDs []int64 `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"`
ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"`
ChatTopics [][]int64 `yaml:"chatTopics,omitempty" json:"chatTopics,omitempty" validate:"omitempty"`
ChatTopicsFile string `yaml:"chatTopicsFile,omitempty" json:"chatTopicsFile,omitempty" validate:"omitempty,file"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
ChatIDs []int64 `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"`
ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"`
ChatTopics []int64 `yaml:"chatTopics,omitempty" json:"chatTopics,omitempty" validate:"omitempty"`
ChatTopicsFile string `yaml:"chatTopicsFile,omitempty" json:"chatTopicsFile,omitempty" validate:"omitempty,file"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
}

// GetDefaults gets the default values
Expand Down
19 changes: 7 additions & 12 deletions internal/notif/telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,13 @@ func (c *Client) Send(entry model.NotifEntry) error {
}

for i, chatID := range chatIDs {
if len(chatTopics) > i && len(chatTopics[i]) > 0 {
for _, topic := range chatTopics[i] {
err = sendTelegramMessage(bot, chatID, topic, string(body))
if err != nil {
return err
}
}
} else {
err = sendTelegramMessage(bot, chatID, 0, string(body))
if err != nil {
return err
}
var chatTopic int64
if len(chatTopics) > i {
chatTopic = chatTopics[i]
}
err = sendTelegramMessage(bot, chatID, chatTopic, string(body))
if err != nil {
return err
}
}

Expand Down

0 comments on commit f20bde8

Please sign in to comment.