Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for Telegram to use a thread id when posting notification #265

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/services/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ metadata:
annotations:
notifications.argoproj.io/subscribe.on-sync-succeeded.telegram: -1000000000000
```

If your private chat contains threads, you can optionally specify a thread id by seperating it with a `|`:
```yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
annotations:
notifications.argoproj.io/subscribe.on-sync-succeeded.telegram: -1000000000000|2
```
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,5 @@ require (
)

replace github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.14.0

replace github.com/go-telegram-bot-api/telegram-bot-api/v5 => github.com/OvyFlash/telegram-bot-api/v5 v5.0.0-20240108230938-63e5c59035bf
Techwolf12 marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYr
github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA=
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/OvyFlash/telegram-bot-api/v5 v5.0.0-20240108230938-63e5c59035bf h1:a7VKhbjKYPO8twGy/1AxMpM2Fp0qT7bf25fmCVMVu4s=
github.com/OvyFlash/telegram-bot-api/v5 v5.0.0-20240108230938-63e5c59035bf/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/PagerDuty/go-pagerduty v1.7.0 h1:S1NcMKECxT5hJwV4VT+QzeSsSiv4oWl1s2821dUqG/8=
github.com/PagerDuty/go-pagerduty v1.7.0/go.mod h1:PuFyJKRz1liIAH4h5KVXVD18Obpp1ZXRdxHvmGXooro=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
Expand Down Expand Up @@ -174,8 +176,6 @@ github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gobuffalo/envy v1.7.0/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
Expand Down
47 changes: 31 additions & 16 deletions pkg/services/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,50 @@
opts TelegramOptions
}

func (s telegramService) Send(notification Notification, dest Destination) error {
bot, err := tgbotapi.NewBotAPI(s.opts.Token)
if err != nil {
return err
}
func buildTelegramMessageOptions(notification Notification, dest Destination) (*tgbotapi.MessageConfig, error) {
msg := tgbotapi.MessageConfig{}

if strings.HasPrefix(dest.Recipient, "-") {
chatID, err := strconv.ParseInt(dest.Recipient, 10, 64)
chatChannel := strings.Split(dest.Recipient, "|")

chatID, err := strconv.ParseInt(chatChannel[0], 10, 64)
if err != nil {
return err
return nil, err

Check warning on line 30 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L30

Added line #L30 was not covered by tests
}

// Init message with ParseMode is 'Markdown'
msg := tgbotapi.NewMessage(chatID, notification.Message)
msg = tgbotapi.NewMessage(chatID, notification.Message)
msg.ParseMode = "Markdown"

_, err = bot.Send(msg)
if err != nil {
return err
if len(chatChannel) > 1 {
Techwolf12 marked this conversation as resolved.
Show resolved Hide resolved
threadID, err := strconv.Atoi(chatChannel[1])
if err != nil {
return nil, err
}

Check warning on line 41 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L40-L41

Added lines #L40 - L41 were not covered by tests
msg.MessageThreadID = threadID
}
} else {
// Init message with ParseMode is 'Markdown'
msg := tgbotapi.NewMessageToChannel("@"+dest.Recipient, notification.Message)
msg = tgbotapi.NewMessageToChannel("@"+dest.Recipient, notification.Message)
msg.ParseMode = "Markdown"
}
return &msg, nil
}

_, err := bot.Send(msg)
if err != nil {
return err
}
func (s telegramService) Send(notification Notification, dest Destination) error {
bot, err := tgbotapi.NewBotAPI(s.opts.Token)
if err != nil {
return err
}

Check warning on line 56 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L52-L56

Added lines #L52 - L56 were not covered by tests

msg, err := buildTelegramMessageOptions(notification, dest)
if err != nil {
return err
}

Check warning on line 61 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L58-L61

Added lines #L58 - L61 were not covered by tests

_, err = bot.Send(msg)
if err != nil {
return err

Check warning on line 65 in pkg/services/telegram.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/telegram.go#L63-L65

Added lines #L63 - L65 were not covered by tests
}

return nil
Expand Down
53 changes: 53 additions & 0 deletions pkg/services/telegram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package services

import (
"reflect"
"testing"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func TestBuildTelegramMessageOptions(t *testing.T) {
tests := []struct {
name string
notification Notification
dest Destination
want *tgbotapi.MessageConfig
wantErr bool
}{
{
name: "Message to chat",
notification: Notification{Message: "Test message"},
dest: Destination{Recipient: "-123456"},
want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChatID: -123456}, MessageThreadID: 0}, ParseMode: "Markdown"},
wantErr: false,
},
{
name: "Message to chat with thread",
notification: Notification{Message: "Test message"},
dest: Destination{Recipient: "-123456|1"},
want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChatID: -123456}, MessageThreadID: 1}, ParseMode: "Markdown"},
wantErr: false,
},
{
name: "Message to channel",
notification: Notification{Message: "Test message"},
dest: Destination{Recipient: "123456"},
want: &tgbotapi.MessageConfig{Text: "Test message", BaseChat: tgbotapi.BaseChat{ChatConfig: tgbotapi.ChatConfig{ChannelUsername: "@123456"}}, ParseMode: "Markdown"},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildTelegramMessageOptions(tt.notification, tt.dest)
if (err != nil) != tt.wantErr {
t.Errorf("buildTelegramMessageOptions() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildTelegramMessageOptions() = %v, want %v", got, tt.want)
}
})
}
}
Loading