samber / slog-telegram

🚨 slog: Telegram handler
https://pkg.go.dev/github.com/samber/slog-telegram/v2
MIT License
7 stars 5 forks source link

Example code execution failed #3

Open yl opened 3 weeks ago

yl commented 3 weeks ago

Telegram bot api return error: Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 70

yl commented 3 weeks ago

example.go

package main

import (
    "fmt"
    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
    "time"

    "log/slog"

    slogtelegram "github.com/samber/slog-telegram/v2"
)

func main() {
    token := "7294492055:AAEHDKzd1Yg8p3HuNBzOfaKV18v0D6YYwJ0"
    username := "@slog_test"

    logger := slog.New(slogtelegram.Option{Level: slog.LevelDebug, Token: token, Username: username, MessageConfigurator: Configurator}.NewTelegramHandler())
    logger = logger.With("release", "v1.0.0")

    logger.
        With(
            slog.Group("user",
                slog.String("id", "user-123"),
                slog.Time("created_at", time.Now().AddDate(0, 0, -1)),
            ),
        ).
        With("environment", "dev").
        With("error", fmt.Errorf("an error")).
        Error("A message")

    time.Sleep(10 * time.Second)
}

// Configurator Make the message support markdown
func Configurator(config tgbotapi.MessageConfig, attr []slog.Attr) tgbotapi.MessageConfig {
    config.ParseMode = tgbotapi.ModeMarkdown
    return config
}

handler.go

...
func (h *TelegramHandler) Handle(ctx context.Context, record slog.Record) error {
    message := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, h.attrs, h.groups, &record)
    msg := tgbotapi.NewMessageToChannel(h.option.Username, message)
    if h.option.MessageConfigurator != nil {
        msg = h.option.MessageConfigurator(msg, h.attrs)
    }

    // non-blocking
    go func() {
        _, err := h.client.Send(msg)
        fmt.Println(err)
    }()

    return nil
}
...

output

Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 70
samber commented 3 weeks ago

Did you check the syntax of your message ?

-> https://stackoverflow.com/questions/61224362/telegram-bot-cant-find-end-of-the-entity-starting-at-truncated

yl commented 3 weeks ago

I'm running the example code of this repository.

https://github.com/samber/slog-telegram/blob/main/example%2Fexample.go

samber commented 3 weeks ago

I made some tests and I think this is due to slog.Attr of kind Time.

Can you confirm ?