mirror of
https://github.com/crazy-max/diun.git
synced 2025-12-21 13:23:09 +01:00
telegram: align chatIDs and chatIDsFile format handling
This commit is contained in:
@@ -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,41 +121,32 @@ 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, ":")
|
||||
if len(parts) < 1 || len(parts) > 2 {
|
||||
return nil, errors.Errorf("invalid chat ID %q", v)
|
||||
}
|
||||
id, err := strconv.ParseInt(parts[0], 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid chat ID")
|
||||
}
|
||||
var topics []int64
|
||||
if len(parts) == 2 {
|
||||
topicParts := strings.Split(parts[1], ";")
|
||||
for _, topicPart := range topicParts {
|
||||
topic, err := strconv.ParseInt(topicPart, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
|
||||
}
|
||||
topics = append(topics, topic)
|
||||
}
|
||||
}
|
||||
chatIDs = append(chatIDs, chatID{
|
||||
id: id,
|
||||
topics: topics,
|
||||
})
|
||||
default:
|
||||
return nil, errors.Errorf("invalid chat ID %v (type=%T)", entry, entry)
|
||||
parts := strings.Split(entry, ":")
|
||||
if len(parts) < 1 || len(parts) > 2 {
|
||||
return nil, errors.Errorf("invalid chat ID %q", entry)
|
||||
}
|
||||
id, err := strconv.ParseInt(parts[0], 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "invalid chat ID")
|
||||
}
|
||||
var topics []int64
|
||||
if len(parts) == 2 {
|
||||
topicParts := strings.Split(parts[1], ";")
|
||||
for _, topicPart := range topicParts {
|
||||
topic, err := strconv.ParseInt(topicPart, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
|
||||
}
|
||||
topics = append(topics, topic)
|
||||
}
|
||||
}
|
||||
chatIDs = append(chatIDs, chatID{
|
||||
id: id,
|
||||
topics: topics,
|
||||
})
|
||||
}
|
||||
return chatIDs, nil
|
||||
}
|
||||
|
||||
@@ -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"`),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user