mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-14 18:19:22 +00:00
feat(alias): log the shape of a capture at debug level
The capture failures worth debugging are all about what Telegram did not deliver: a reply stripped of its content, a caption where text was expected, or a kind that falls through the switch. None of that is visible from the user-facing refusal, which only says "unsupported". One alias_capture line per /alias reports field names, lengths and counts — never message text, so aliased messages cannot travel with the logs.
This commit is contained in:
1 parent
2a86e028f6
commit
cb86ec4cf3
4 files changed
+219
-1
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package alias
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/go-telegram/bot/models"
|
||||
)
|
||||
|
||||
// replyShape describes what /alias was handed, for the debug log.
|
||||
//
|
||||
// Enable with LOG_LEVEL=debug; silent otherwise. It exists because the failures
|
||||
// worth debugging here are all about what Telegram *did not* deliver: a reply
|
||||
// stripped of its content (another bot's message), a caption where text was
|
||||
// expected, or a kind that falls through capture's switch. Those are invisible
|
||||
// from the user-facing refusal, which only says the message was unsupported.
|
||||
//
|
||||
// Deliberately reports shape, never content. Lengths and field names are
|
||||
// enough to explain a capture failure, and message text in a log line would
|
||||
// mean every aliased message ending up in stdout and whatever ships it.
|
||||
func replyShape(replied *models.Message, entry Alias, ok bool) []any {
|
||||
if replied == nil {
|
||||
// The case that motivated this: Telegram can deliver /alias with no
|
||||
// reply attached at all, which is indistinguishable from the caller
|
||||
// forgetting to reply.
|
||||
return []any{"reply", "absent"}
|
||||
}
|
||||
|
||||
attrs := []any{
|
||||
"reply", "present",
|
||||
"reply_id", replied.ID,
|
||||
"fields", strings.Join(populatedFields(replied), ","),
|
||||
"text_len", len(replied.Text),
|
||||
"caption_len", len(replied.Caption),
|
||||
"entities", len(replied.Entities) + len(replied.CaptionEntities),
|
||||
"captured", ok,
|
||||
}
|
||||
if ok {
|
||||
attrs = append(attrs, "kind", entry.Kind)
|
||||
}
|
||||
if replied.From != nil {
|
||||
attrs = append(attrs, "from_id", replied.From.ID, "from_bot", replied.From.IsBot)
|
||||
} else {
|
||||
// No sender at all — an anonymous channel post or a stripped reply.
|
||||
attrs = append(attrs, "from", "absent")
|
||||
}
|
||||
if replied.SenderChat != nil {
|
||||
attrs = append(attrs, "sender_chat", replied.SenderChat.ID)
|
||||
}
|
||||
return attrs
|
||||
}
|
||||
|
||||
// populatedFields names the content fields the replied message actually has.
|
||||
//
|
||||
// Covers more than capture handles on purpose: the point is to show what
|
||||
// arrived, including kinds this module refuses, so "unsupported" can be told
|
||||
// apart from "empty".
|
||||
func populatedFields(m *models.Message) []string {
|
||||
var out []string
|
||||
add := func(present bool, name string) {
|
||||
if present {
|
||||
out = append(out, name)
|
||||
}
|
||||
}
|
||||
add(m.Text != "", "text")
|
||||
add(m.Caption != "", "caption")
|
||||
add(m.Sticker != nil, "sticker")
|
||||
add(len(m.Photo) > 0, "photo")
|
||||
add(m.Animation != nil, "animation")
|
||||
add(m.Video != nil, "video")
|
||||
add(m.VideoNote != nil, "video_note")
|
||||
add(m.Audio != nil, "audio")
|
||||
add(m.Voice != nil, "voice")
|
||||
add(m.Document != nil, "document")
|
||||
add(m.Location != nil, "location")
|
||||
add(m.Contact != nil, "contact")
|
||||
add(m.Poll != nil, "poll")
|
||||
add(m.Dice != nil, "dice")
|
||||
add(m.Venue != nil, "venue")
|
||||
add(m.Game != nil, "game")
|
||||
add(m.ViaBot != nil, "via_bot")
|
||||
add(m.ForwardOrigin != nil, "forward_origin")
|
||||
if len(out) == 0 {
|
||||
// The signature of a reply Telegram delivered but emptied.
|
||||
out = append(out, "none")
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user