telegram: align chatIDs and chatIDsFile format handling

This commit is contained in:
CrazyMax
2024-12-20 18:25:34 +01:00
parent 8678b1944a
commit a27d3f5019
2 changed files with 32 additions and 56 deletions

View File

@@ -49,10 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier")
}
var cids []interface{}
for _, cid := range c.cfg.ChatIDs {
cids = append(cids, cid)
}
cids := c.cfg.ChatIDs
cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier")
@@ -124,18 +121,12 @@ func (c *Client) Send(entry model.NotifEntry) error {
return nil
}
func parseChatIDs(entries []interface{}) ([]chatID, error) {
func parseChatIDs(entries []string) ([]chatID, error) {
var chatIDs []chatID
for _, entry := range entries {
switch v := entry.(type) {
case int:
chatIDs = append(chatIDs, chatID{id: int64(v)})
case int64:
chatIDs = append(chatIDs, chatID{id: v})
case string:
parts := strings.Split(v, ":")
parts := strings.Split(entry, ":")
if len(parts) < 1 || len(parts) > 2 {
return nil, errors.Errorf("invalid chat ID %q", v)
return nil, errors.Errorf("invalid chat ID %q", entry)
}
id, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
@@ -156,9 +147,6 @@ func parseChatIDs(entries []interface{}) ([]chatID, error) {
id: id,
topics: topics,
})
default:
return nil, errors.Errorf("invalid chat ID %v (type=%T)", entry, entry)
}
}
return chatIDs, nil
}

View File

@@ -10,13 +10,13 @@ import (
func TestParseChatIDs(t *testing.T) {
tests := []struct {
name string
entries []interface{}
entries []string
expected []chatID
err error
}{
{
name: "valid integers",
entries: []interface{}{8547439, 1234567},
name: "valid chat IDS",
entries: []string{"8547439", "1234567"},
expected: []chatID{
{id: 8547439},
{id: 1234567},
@@ -25,7 +25,7 @@ func TestParseChatIDs(t *testing.T) {
},
{
name: "valid strings with topics",
entries: []interface{}{"567891234:25", "891256734:25;12"},
entries: []string{"567891234:25", "891256734:25;12"},
expected: []chatID{
{id: 567891234, topics: []int64{25}},
{id: 891256734, topics: []int64{25, 12}},
@@ -34,37 +34,25 @@ func TestParseChatIDs(t *testing.T) {
},
{
name: "invalid format",
entries: []interface{}{"invalid_format"},
entries: []string{"invalid_format"},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
},
{
name: "invalid type",
entries: []interface{}{true},
expected: nil,
err: errors.New("invalid chat ID true (type=bool)"),
},
{
name: "empty string",
entries: []interface{}{""},
entries: []string{""},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "": invalid syntax`),
},
{
name: "string with invalid topic",
entries: []interface{}{"567891234:invalid"},
entries: []string{"567891234:invalid"},
expected: nil,
err: errors.New(`invalid topic "invalid" for chat ID 567891234: strconv.ParseInt: parsing "invalid": invalid syntax`),
},
{
name: "mixed valid and invalid entries",
entries: []interface{}{8547439, "567891234:25", "invalid_format", true},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
},
{
name: "invalid format with too many parts",
entries: []interface{}{"567891234:25:extra"},
entries: []string{"567891234:25:extra"},
expected: nil,
err: errors.New(`invalid chat ID "567891234:25:extra"`),
},