Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example code execution failed #3

Open
yl opened this issue Aug 19, 2024 · 4 comments
Open

Example code execution failed #3

yl opened this issue Aug 19, 2024 · 4 comments

Comments

@yl
Copy link

yl commented Aug 19, 2024

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

@yl
Copy link
Author

yl commented Aug 19, 2024

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
Copy link
Owner

samber commented Aug 19, 2024

@yl
Copy link
Author

yl commented Aug 20, 2024

I'm running the example code of this repository.

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

@samber
Copy link
Owner

samber commented Aug 20, 2024

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

Can you confirm ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants