mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(notification): Telegram add support for topics in groups (#894)
* feat(notification): send Telegram messages to a specific topic of a group * Convert settings.Topic to integer once and reuse it as part of the telegramSender struct. * feat(notifications): add migrations for topic * fix(notifications): find null string * fix(notifications): form initial values --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
e5692fefc7
commit
fdc957c571
7 changed files with 60 additions and 16 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -19,29 +20,42 @@ import (
|
|||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// Reference: https://core.telegram.org/bots/api#sendmessage
|
||||
type TelegramMessage struct {
|
||||
ChatID string `json:"chat_id"`
|
||||
Text string `json:"text"`
|
||||
ParseMode string `json:"parse_mode"`
|
||||
ChatID string `json:"chat_id"`
|
||||
Text string `json:"text"`
|
||||
ParseMode string `json:"parse_mode"`
|
||||
MessageThreadID int `json:"message_thread_id,omitempty"`
|
||||
}
|
||||
|
||||
type telegramSender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
ThreadID int
|
||||
}
|
||||
|
||||
func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
threadID := 0
|
||||
if t := settings.Topic; t != "" {
|
||||
var err error
|
||||
threadID, err = strconv.Atoi(t)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("could not parse specified topic %q as an integer", t)
|
||||
}
|
||||
}
|
||||
return &telegramSender{
|
||||
log: log.With().Str("sender", "telegram").Logger(),
|
||||
Settings: settings,
|
||||
ThreadID: threadID,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *telegramSender) Send(event domain.NotificationEvent, payload domain.NotificationPayload) error {
|
||||
m := TelegramMessage{
|
||||
ChatID: s.Settings.Channel,
|
||||
Text: s.buildMessage(event, payload),
|
||||
ParseMode: "HTML",
|
||||
ChatID: s.Settings.Channel,
|
||||
Text: s.buildMessage(event, payload),
|
||||
MessageThreadID: s.ThreadID,
|
||||
ParseMode: "HTML",
|
||||
//ParseMode: "MarkdownV2",
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue