better logging
Some checks failed
ci/woodpecker/push/fx_oracle Pipeline is pending
ci/woodpecker/push/ledger Pipeline is pending
ci/woodpecker/push/nats Pipeline is pending
ci/woodpecker/push/notification Pipeline is pending
ci/woodpecker/push/payments_orchestrator Pipeline is pending
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline failed
ci/woodpecker/push/bump_version unknown status
ci/woodpecker/push/frontend Pipeline failed

This commit is contained in:
Stephan D
2025-11-19 13:32:40 +01:00
parent 8710d7da53
commit 62956b06ca

View File

@@ -16,6 +16,7 @@ import (
"github.com/tech/sendico/pkg/merrors"
"github.com/tech/sendico/pkg/mlogger"
"github.com/tech/sendico/pkg/model"
"go.uber.org/zap"
)
const defaultAPIURL = "https://api.telegram.org"
@@ -110,24 +111,45 @@ func (c *client) SendDemoRequest(ctx context.Context, request *model.DemoRequest
func (c *client) sendMessage(ctx context.Context, payload sendMessagePayload) error {
body, err := json.Marshal(&payload)
if err != nil {
c.logger.Warn("Failed to marshal telegram payload", zap.Error(err))
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint(), bytes.NewReader(body))
if err != nil {
c.logger.Warn("Failed to create telegram request", zap.Error(err))
return err
}
req.Header.Set("Content-Type", "application/json")
fields := []zap.Field{
zap.String("chat_id", payload.ChatID),
zap.Int("payload_size_bytes", len(body)),
zap.Bool("disable_preview", payload.DisablePreview),
zap.Bool("disable_notification", payload.DisableNotify),
zap.Bool("protect_content", payload.ProtectContent),
}
if payload.ThreadID != nil {
fields = append(fields, zap.Int64("thread_id", *payload.ThreadID))
}
c.logger.Debug("Sending Telegram message", fields...)
start := time.Now()
resp, err := c.httpClient.Do(req)
if err != nil {
c.logger.Warn("Telegram request failed", zap.Error(err))
return err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
c.logger.Debug("Telegram message sent", zap.Int("status_code", resp.StatusCode), zap.Duration("latency", time.Since(start)))
return nil
}
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10))
c.logger.Warn("Telegram API returned non-success status",
zap.Int("status_code", resp.StatusCode),
zap.ByteString("response_body", respBody),
zap.String("chat_id", c.chatID))
return merrors.Internal(fmt.Sprintf("telegram sendMessage failed with status %d: %s", resp.StatusCode, string(respBody)))
}