diff --git a/api/notification/internal/server/notificationimp/telegram/client.go b/api/notification/internal/server/notificationimp/telegram/client.go index 030264c..07356bc 100644 --- a/api/notification/internal/server/notificationimp/telegram/client.go +++ b/api/notification/internal/server/notificationimp/telegram/client.go @@ -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))) }