From c172635ca0ce6c51f29f90b6d0808f045fba5453 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Sat, 21 Nov 2026 05:56:21 +0100 Subject: [PATCH 1/2] feat(cli): show hidden links in message json Messages in --json carry links: the URLs behind words of the text (text link entities, sliced in UTF-16 code units) and the link buttons of the keyboard. Agents lost both before. Co-Authored-By: Claude Opus 5.5 --- internal/cli/guide.txt | 2 ++ internal/core/core.go | 22 ++++++++++++++++++++++ internal/demo/demo.go | 11 +++++++++-- internal/mtp/convert.go | 22 ++++++++++++++++++++++ internal/mtp/mtp_test.go | 16 ++++++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) diff --git a/internal/cli/guide.txt b/internal/cli/guide.txt index 19be893..afbcc60 100644 --- a/internal/cli/guide.txt +++ b/internal/cli/guide.txt @@ -54,6 +54,8 @@ OUTPUT them. Text that continues on further lines is indented by two spaces. Media shows as [photo], [file: name], ... Add --json to a command that prints data for one JSON object per line. + A JSON message also has "links": [{"text", "url"}] for the URLs the text + does not show, behind its words or its buttons. Exit codes: 0 done, 1 failed, 2 wrong usage, 3 refused: by the account's limits, or because the command is for a person. Errors go to stderr. diff --git a/internal/core/core.go b/internal/core/core.go index fe8fd02..00f5718 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -61,10 +61,32 @@ type Message struct { // PictureSize is the media's size in pixels, zero when unknown. The TUI // sets room aside for the picture with it, so nothing moves when it loads. PictureSize image.Point `json:"-"` + // Links are the URLs the text does not show: links behind words of the + // text, then the link buttons of the keyboard. + Links []Link `json:"links,omitempty"` // Buttons are a bot's inline keyboard, row by row. Buttons [][]Button `json:"-"` } +// Link is a URL behind Text, which is what the message shows instead. +type Link struct { + Text string `json:"text"` + URL string `json:"url"` +} + +// ButtonLinks lists the link buttons of a keyboard, for Message.Links. +func ButtonLinks(rows [][]Button) []Link { + var out []Link + for _, row := range rows { + for _, b := range row { + if b.Kind == ButtonURL { + out = append(out, Link{Text: b.Text, URL: b.Value}) + } + } + } + return out +} + // ButtonKind tells what pressing a button of a bot's keyboard does. type ButtonKind uint8 diff --git a/internal/demo/demo.go b/internal/demo/demo.go index c16de18..9d29305 100644 --- a/internal/demo/demo.go +++ b/internal/demo/demo.go @@ -59,6 +59,11 @@ var keyboards = map[int64][][]core.Button{ }, } +// textLinks are the links behind words of the last message of chats. +var textLinks = map[int64][]core.Link{ + -1001000000202: {{Text: "Issue 572", URL: "https://goweekly.example/issues/572"}}, +} + var fixtures = []fixture{ {core.Chat{ID: 101, Kind: core.KindUser, Title: "Alice Rossi", Username: "alice", Unread: 2, Pinned: true, PhotoID: 1}, []string{ "190|Alice Rossi|hey, are we still on for tomorrow?", @@ -151,8 +156,10 @@ func New() *Backend { } chat := f.chat msgs := b.messages[chat.ID] - msgs[len(msgs)-1].Buttons = keyboards[chat.ID] - chat.Last = &msgs[len(msgs)-1] + last := &msgs[len(msgs)-1] + last.Buttons = keyboards[chat.ID] + last.Links = append(textLinks[chat.ID], core.ButtonLinks(last.Buttons)...) + chat.Last = last // They read everything up to their last answer, so a message of // the account after it shows as not read yet. for _, m := range msgs { diff --git a/internal/mtp/convert.go b/internal/mtp/convert.go index fc02455..3ec912e 100644 --- a/internal/mtp/convert.go +++ b/internal/mtp/convert.go @@ -4,6 +4,7 @@ import ( "fmt" "image" "time" + "unicode/utf16" "github.com/gotd/td/tg" @@ -29,6 +30,7 @@ func (b *backend) message(raw tg.MessageClass, e tg.Entities) (core.Message, boo out.Buttons = append(out.Buttons, buttons) } } + out.Links = append(textLinks(m.Message, m.Entities), core.ButtonLinks(out.Buttons)...) if reply, ok := m.ReplyTo.(*tg.MessageReplyHeader); ok && !reply.ForumTopic { out.ReplyTo = reply.ReplyToMsgID } @@ -47,6 +49,26 @@ func (b *backend) message(raw tg.MessageClass, e tg.Entities) (core.Message, boo return core.Message{}, false } +// textLinks lists the links hidden behind words of a message's text. Telegram +// places entities in UTF-16 code units, so the text is sliced in those. +func textLinks(text string, entities []tg.MessageEntityClass) []core.Link { + var out []core.Link + var units []uint16 + for _, entity := range entities { + link, ok := entity.(*tg.MessageEntityTextURL) + if !ok { + continue + } + if units == nil { + units = utf16.Encode([]rune(text)) + } + start := min(max(link.Offset, 0), len(units)) + end := min(start+max(link.Length, 0), len(units)) + out = append(out, core.Link{Text: string(utf16.Decode(units[start:end])), URL: link.URL}) + } + return out +} + // buttonOf converts a button of an inline keyboard. A callback button that // asks for the account's password is left to Telegram's own apps. func buttonOf(button tg.KeyboardInlineButton) core.Button { diff --git a/internal/mtp/mtp_test.go b/internal/mtp/mtp_test.go index b2fdba4..be00105 100644 --- a/internal/mtp/mtp_test.go +++ b/internal/mtp/mtp_test.go @@ -2,6 +2,7 @@ package mtp import ( "path/filepath" + "slices" "testing" "github.com/gotd/td/tg" @@ -157,3 +158,18 @@ func TestMediaSummary(t *testing.T) { } } } + +// Telegram places entities in UTF-16 code units: an emoji before a link +// takes two of them, and a range past the text must not panic. +func TestTextLinks(t *testing.T) { + text := "🔥 -40% ➡ BUY HERE" + links := textLinks(text, []tg.MessageEntityClass{ + &tg.MessageEntityBold{Offset: 0, Length: 2}, + &tg.MessageEntityTextURL{Offset: 10, Length: 8, URL: "https://amzn.example/x"}, + &tg.MessageEntityTextURL{Offset: 15, Length: 50, URL: "https://past.example"}, + }) + want := []core.Link{{Text: "BUY HERE", URL: "https://amzn.example/x"}, {Text: "ERE", URL: "https://past.example"}} + if !slices.Equal(links, want) { + t.Fatalf("textLinks = %+v, want %+v", links, want) + } +} From cd6aa8516bac6b5dc613d995416c5f3dad05e916 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Sat, 21 Nov 2026 06:07:49 +0100 Subject: [PATCH 2/2] fix(cli): link login buttons, clamp link ranges to the text Co-Authored-By: Claude Opus 5.5 --- internal/cli/guide.txt | 4 ++-- internal/demo/demo.go | 2 +- internal/mtp/convert.go | 5 ++++- internal/mtp/mtp_test.go | 7 ++++++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/cli/guide.txt b/internal/cli/guide.txt index afbcc60..f7c21e8 100644 --- a/internal/cli/guide.txt +++ b/internal/cli/guide.txt @@ -54,8 +54,8 @@ OUTPUT them. Text that continues on further lines is indented by two spaces. Media shows as [photo], [file: name], ... Add --json to a command that prints data for one JSON object per line. - A JSON message also has "links": [{"text", "url"}] for the URLs the text - does not show, behind its words or its buttons. + A JSON message with URLs its text does not show, behind its words or its + buttons, lists them in "links": [{"text", "url"}]. Exit codes: 0 done, 1 failed, 2 wrong usage, 3 refused: by the account's limits, or because the command is for a person. Errors go to stderr. diff --git a/internal/demo/demo.go b/internal/demo/demo.go index 9d29305..8cd522d 100644 --- a/internal/demo/demo.go +++ b/internal/demo/demo.go @@ -158,7 +158,7 @@ func New() *Backend { msgs := b.messages[chat.ID] last := &msgs[len(msgs)-1] last.Buttons = keyboards[chat.ID] - last.Links = append(textLinks[chat.ID], core.ButtonLinks(last.Buttons)...) + last.Links = slices.Concat(textLinks[chat.ID], core.ButtonLinks(last.Buttons)) chat.Last = last // They read everything up to their last answer, so a message of // the account after it shows as not read yet. diff --git a/internal/mtp/convert.go b/internal/mtp/convert.go index 3ec912e..fe46a78 100644 --- a/internal/mtp/convert.go +++ b/internal/mtp/convert.go @@ -63,7 +63,7 @@ func textLinks(text string, entities []tg.MessageEntityClass) []core.Link { units = utf16.Encode([]rune(text)) } start := min(max(link.Offset, 0), len(units)) - end := min(start+max(link.Length, 0), len(units)) + end := min(max(link.Offset+link.Length, start), len(units)) out = append(out, core.Link{Text: string(utf16.Decode(units[start:end])), URL: link.URL}) } return out @@ -80,6 +80,9 @@ func buttonOf(button tg.KeyboardInlineButton) core.Button { } case *tg.InlineButtonTypeURL: out.Kind, out.Value = core.ButtonURL, t.URL + case *tg.InlineButtonTypeURLAuth: + // A link that can log the account in to the site: opened as a plain link. + out.Kind, out.Value = core.ButtonURL, t.URL case *tg.InlineButtonTypeCopy: out.Kind, out.Value = core.ButtonCopy, t.CopyText } diff --git a/internal/mtp/mtp_test.go b/internal/mtp/mtp_test.go index be00105..87592c8 100644 --- a/internal/mtp/mtp_test.go +++ b/internal/mtp/mtp_test.go @@ -167,8 +167,13 @@ func TestTextLinks(t *testing.T) { &tg.MessageEntityBold{Offset: 0, Length: 2}, &tg.MessageEntityTextURL{Offset: 10, Length: 8, URL: "https://amzn.example/x"}, &tg.MessageEntityTextURL{Offset: 15, Length: 50, URL: "https://past.example"}, + &tg.MessageEntityTextURL{Offset: -1, Length: 3, URL: "https://before.example"}, }) - want := []core.Link{{Text: "BUY HERE", URL: "https://amzn.example/x"}, {Text: "ERE", URL: "https://past.example"}} + want := []core.Link{ + {Text: "BUY HERE", URL: "https://amzn.example/x"}, + {Text: "ERE", URL: "https://past.example"}, + {Text: "🔥", URL: "https://before.example"}, // the two units left of a range starting before the text + } if !slices.Equal(links, want) { t.Fatalf("textLinks = %+v, want %+v", links, want) }