feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
53
internal/sender/telegram.go
Обычный файл
53
internal/sender/telegram.go
Обычный файл
@@ -0,0 +1,53 @@
|
||||
package sender
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/app/models"
|
||||
"rsgit.ru/rsmon/rsmon/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)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user