Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/cli/guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.

Expand Down
22 changes: 22 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions internal/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?",
Expand Down Expand Up @@ -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 = 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.
for _, m := range msgs {
Expand Down
25 changes: 25 additions & 0 deletions internal/mtp/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"image"
"time"
"unicode/utf16"

"github.com/gotd/td/tg"

Expand All @@ -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
}
Expand All @@ -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(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
}

// 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 {
Expand All @@ -58,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
}
Expand Down
21 changes: 21 additions & 0 deletions internal/mtp/mtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mtp

import (
"path/filepath"
"slices"
"testing"

"github.com/gotd/td/tg"
Expand Down Expand Up @@ -157,3 +158,23 @@ 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"},
&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"},
{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)
}
}
Loading