Skip to content

Commit 36bfdbd

Browse files
Kasamagithub-actions[bot]
authored andcommitted
add support for telegram message_thread_id
Signed-off-by: Kasama <[email protected]> (cherry picked from commit 2da2adf)
1 parent 78b3f65 commit 36bfdbd

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

internal/notifier/telegram.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ type Telegram struct {
2525
// TelegramPayload represents the payload sent to Telegram Bot API
2626
// Reference: https://core.telegram.org/bots/api#sendmessage
2727
type TelegramPayload struct {
28-
ChatID string `json:"chat_id"` // Unique identifier for the target chat
29-
Text string `json:"text"` // Text of the message to be sent
30-
ParseMode string `json:"parse_mode"` // Mode for parsing entities in the message text
28+
ChatID string `json:"chat_id"` // Unique identifier for the target chat
29+
MessageThreadID string `json:"message_thread_id,omitempty"` // Unique identifier for the target message thread (topic) of the forum; for forum supergroups only
30+
Text string `json:"text"` // Text of the message to be sent
31+
ParseMode string `json:"parse_mode"` // Mode for parsing entities in the message text
3132
}
3233

3334
func NewTelegram(proxyURL, channel, token string) (*Telegram, error) {
@@ -66,12 +67,18 @@ func (t *Telegram) Post(ctx context.Context, event eventv1.Event) error {
6667
}
6768
message := fmt.Sprintf("*%s*\n%s\n%s", escapeString(heading), escapeString(event.Message), metadata)
6869

70+
chatID, thread, channelHasThreadID := strings.Cut(t.Channel, ":")
71+
6972
payload := TelegramPayload{
70-
ChatID: t.Channel,
73+
ChatID: chatID,
7174
Text: message,
7275
ParseMode: "MarkdownV2", // https://core.telegram.org/bots/api#markdownv2-style
7376
}
7477

78+
if channelHasThreadID {
79+
payload.MessageThreadID = thread
80+
}
81+
7582
apiURL, err := url.JoinPath(t.url, sendMessageMethodName)
7683
if err != nil {
7784
return fmt.Errorf("failed to construct API URL: %w", err)

internal/notifier/telegram_test.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ import (
2929
. "github.com/onsi/gomega"
3030
)
3131

32-
func TestTelegram_Post(t *testing.T) {
33-
g := NewWithT(t)
34-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32+
func telegramMockServer(g *WithT, expected *TelegramPayload) *httptest.Server {
33+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3534
g.Expect(r.Method).To(Equal(http.MethodPost))
3635
g.Expect(r.URL.Path).To(Equal("/sendMessage"))
3736
g.Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
@@ -43,8 +42,9 @@ func TestTelegram_Post(t *testing.T) {
4342
err = json.Unmarshal(b, &payload)
4443
g.Expect(err).ToNot(HaveOccurred())
4544

46-
g.Expect(payload.ChatID).To(Equal("channel"))
47-
g.Expect(payload.ParseMode).To(Equal("MarkdownV2"))
45+
g.Expect(payload.ChatID).To(Equal(expected.ChatID))
46+
g.Expect(payload.MessageThreadID).To(Equal(expected.MessageThreadID))
47+
g.Expect(payload.ParseMode).To(Equal(expected.ParseMode))
4848

4949
lines := strings.Split(payload.Text, "\n")
5050
g.Expect(lines).To(HaveLen(5))
@@ -59,6 +59,15 @@ func TestTelegram_Post(t *testing.T) {
5959

6060
w.WriteHeader(http.StatusOK)
6161
}))
62+
}
63+
64+
func TestTelegram_Post(t *testing.T) {
65+
g := NewWithT(t)
66+
ts := telegramMockServer(g, &TelegramPayload{
67+
ChatID: "channel",
68+
MessageThreadID: "",
69+
ParseMode: "MarkdownV2",
70+
})
6271
defer ts.Close()
6372

6473
telegram, err := NewTelegram("", "channel", "token")
@@ -71,3 +80,23 @@ func TestTelegram_Post(t *testing.T) {
7180
err = telegram.Post(context.TODO(), ev)
7281
g.Expect(err).ToNot(HaveOccurred())
7382
}
83+
84+
func TestTelegram_PostWithMessageThreadID(t *testing.T) {
85+
g := NewWithT(t)
86+
ts := telegramMockServer(g, &TelegramPayload{
87+
ChatID: "channel",
88+
MessageThreadID: "thread",
89+
ParseMode: "MarkdownV2",
90+
})
91+
defer ts.Close()
92+
93+
telegram, err := NewTelegram("", "channel:thread", "token")
94+
g.Expect(err).ToNot(HaveOccurred())
95+
96+
telegram.url = ts.URL
97+
98+
ev := testEvent()
99+
ev.Metadata["kubernetes.io/somekey"] = "some.value"
100+
err = telegram.Post(context.TODO(), ev)
101+
g.Expect(err).ToNot(HaveOccurred())
102+
}

0 commit comments

Comments
 (0)