fix(telegram): support only one topicID per chatID

This commit is contained in:
jon4hz
2024-03-17 17:52:46 +01:00
committed by CrazyMax
parent 45893c2ed9
commit d673b3c9dc
3 changed files with 16 additions and 21 deletions

View File

@@ -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 |
@@ -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`

View File

@@ -9,7 +9,7 @@ type NotifTelegram struct {
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"`
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"`
}

View File

@@ -102,20 +102,15 @@ 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))
var chatTopic int64
if len(chatTopics) > i {
chatTopic = chatTopics[i]
}
err = sendTelegramMessage(bot, chatID, chatTopic, string(body))
if err != nil {
return err
}
}
} else {
err = sendTelegramMessage(bot, chatID, 0, string(body))
if err != nil {
return err
}
}
}
return nil
}