Files
worker/internal/sender/telegram.go
Gleb Tv 2c884c5612
Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
refactor: adopt worker module path
2026-07-13 17:56:12 +03:00

54 строки
1.9 KiB
Go

package sender
import (
"context"
"errors"
"time"
"rocketgit.ru/rsmon/worker/app/models"
"rocketgit.ru/rsmon/worker/internal/tg"
)
// RunTelegram provides functionality.
func RunTelegram(message *models.Message) error {
subject, _, _, _ := GetContent(message, time.Now())
return tg.SendMessage(message.Contact.Value, subject.String())
}
// SendTelegramWithCredential delivers a Telegram message using the given bot
// credential. Phase 1 of docs/plans/worker-notifier-mvp.md: the worker holds
// the credential in memory after the init/config push and passes it here.
func SendTelegramWithCredential(chatID, subject, body string, cred *models.NotificationCredential) error {
return SendTelegramWithCredentialContext(context.Background(), chatID, subject, body, cred)
}
// SendTelegramWithCredentialContext prevents handoff after cancellation. The
// tgbotapi client used by the legacy sender does not expose a context-aware
// Send method, so an already handed-off Telegram request cannot be interrupted.
func SendTelegramWithCredentialContext(ctx context.Context, chatID, subject, body string, cred *models.NotificationCredential) error {
if err := ctx.Err(); err != nil {
return err
}
if cred == nil {
return errors.New("telegram credential is nil")
}
text := subject
if body != "" {
text = subject + "\n\n" + body
}
err := sendTelegramWithBot(chatID, text, cred)
if ctx.Err() != nil {
return ctx.Err()
}
return err
}
// sendTelegramWithBot is the low-level helper that the legacy RunTelegram
// (legacy secrets path) and SendTelegramWithCredential (worker wire path)
// both call. It derives an http API URL from the credential's APIURL.
func sendTelegramWithBot(chatID, text string, cred *models.NotificationCredential) error {
// Use a credential-scoped helper so each Telegram credential can map to a
// separate bot token/API URL.
return tg.SendMessageWithToken(chatID, text, cred)
}