Files
anon3anon/pkg/infrastructure/telegram/middleware/loggingmiddleware.go
T
nightnoryu 60746f8824
Check Go code / Build and test (push) Has been cancelled
Check Go code / Lint (push) Has been cancelled
Release / Automatic release (push) Has been cancelled
Fix linter warnings
2025-10-24 13:31:04 +03:00

40 lines
825 B
Go

package middleware
import (
"context"
"fmt"
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
const (
chatIDField = "chat_id"
usernameField = "username"
)
func NewLoggingMiddleware(logger jsonlog.Logger) bot.Middleware {
return func(next bot.HandlerFunc) bot.HandlerFunc {
return func(ctx context.Context, bot *bot.Bot, update *models.Update) {
if update.Message == nil {
return
}
chatLogger := logger.
WithField(chatIDField, update.Message.Chat.ID).
WithField(usernameField, update.Message.From.Username)
text := update.Message.Text
if update.Message.Caption != "" {
text = update.Message.Caption
}
chatLogger.Info(fmt.Sprintf("new message: %s", text))
next(ctx, bot, update)
}
}
}