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
16 changes: 9 additions & 7 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,24 @@ func inlineKeyboardFromTg(mkp *tg.ReplyInlineMarkup) *InlineKeyboardMarkup {
for i, row := range mkp.Rows {
buttons := make([]InlineKeyboardButton, len(row.Buttons))
for j, b := range row.Buttons {
btn := InlineKeyboardButton{Text: b.GetText()}
switch b := b.(type) {
case *tg.KeyboardButtonURL:
btn := InlineKeyboardButton{Text: b.Text}
switch b := b.Type.(type) {
case *tg.InlineButtonTypeURL:
btn.URL = b.URL
case *tg.KeyboardButtonCallback:
case *tg.InlineButtonTypeCallback:
btn.CallbackData = string(b.Data)
case *tg.KeyboardButtonBuy:
case *tg.InlineButtonTypeBuy:
btn.Pay = true
case *tg.KeyboardButtonSwitchInline:
case *tg.InlineButtonTypeWebView:
btn.WebApp = &WebAppInfo{URL: b.URL}
case *tg.InlineButtonTypeSwitchInline:
q := b.Query
if b.SamePeer {
btn.SwitchInlineQueryCurrentChat = &q
} else {
btn.SwitchInlineQuery = &q
}
case *tg.KeyboardButtonURLAuth:
case *tg.InlineButtonTypeURLAuth:
// login_url buttons are represented as ordinary url buttons.
btn.URL = b.URL
}
Expand Down
5 changes: 3 additions & 2 deletions convert_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/gotd/td/bin"
"github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/tg"
)

Expand Down Expand Up @@ -44,8 +45,8 @@ func TestConvertTextMessageFromUser(t *testing.T) {
m.SetFromID(&tg.PeerUser{UserID: 10})
m.SetEntities([]tg.MessageEntityClass{&tg.MessageEntityBold{Offset: 0, Length: 5}})
m.SetReplyTo(&tg.MessageReplyHeader{ReplyToMsgID: 3})
m.SetReplyMarkup(&tg.ReplyInlineMarkup{Rows: []tg.KeyboardButtonRow{{
Buttons: []tg.KeyboardButtonClass{&tg.KeyboardButtonCallback{Text: "ok", Data: []byte("d")}},
m.SetReplyMarkup(&tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{{
Buttons: []tg.KeyboardInlineButton{markup.Callback("ok", []byte("d"))},
}}})

r := convMsg(t, m)
Expand Down
9 changes: 5 additions & 4 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package botapi
import (
"testing"

"github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/tg"
)

Expand All @@ -19,10 +20,10 @@ func TestUserFromTgUser(t *testing.T) {
}

func TestInlineKeyboardFromTg(t *testing.T) {
mkp := &tg.ReplyInlineMarkup{Rows: []tg.KeyboardButtonRow{{Buttons: []tg.KeyboardButtonClass{
&tg.KeyboardButtonURL{Text: "site", URL: "https://example.com"},
&tg.KeyboardButtonCallback{Text: "ok", Data: []byte("ok:1")},
&tg.KeyboardButtonSwitchInline{Text: "here", Query: "q", SamePeer: true},
mkp := &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{{Buttons: []tg.KeyboardInlineButton{
markup.URL("site", "https://example.com"),
markup.Callback("ok", []byte("ok:1")),
markup.SwitchInline("here", "q", true),
}}}}

got := inlineKeyboardFromTg(mkp)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/gotd/contrib v0.25.0
github.com/gotd/log v0.1.0
github.com/gotd/log/logzap v0.1.1
github.com/gotd/td v0.161.0
github.com/gotd/td v0.163.0
github.com/gotd/teled v0.5.0
github.com/stretchr/testify v1.12.1
go.etcd.io/bbolt v1.5.0
Expand Down Expand Up @@ -54,12 +54,12 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.10.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/klauspost/compress v1.19.0 // indirect
github.com/klauspost/compress v1.19.1 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.10 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.22 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/go-archive v0.3.0 // indirect
Expand All @@ -82,7 +82,7 @@ require (
github.com/testcontainers/testcontainers-go v0.42.0 // indirect
github.com/tklauser/go-sysconf v0.3.16 // indirect
github.com/tklauser/numcpus v0.11.0 // indirect
github.com/yuin/goldmark v1.8.4 // indirect
github.com/yuin/goldmark v1.8.5 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ github.com/gotd/log/logzap v0.1.1 h1:O6l7d8HUbODe+UMcrM47eXYDwdJ6RNmpQejLjrlcEIQ
github.com/gotd/log/logzap v0.1.1/go.mod h1:5ObZkITbfhbsBOLzBkzmMk9QxXc0eNQpimau7zRL+Y8=
github.com/gotd/neo v0.1.5 h1:oj0iQfMbGClP8xI59x7fE/uHoTJD7NZH9oV1WNuPukQ=
github.com/gotd/neo v0.1.5/go.mod h1:9A2a4bn9zL6FADufBdt7tZt+WMhvZoc5gWXihOPoiBQ=
github.com/gotd/td v0.161.0 h1:krbzsb70cakdrqF+MUIo+W7BkQTVhyB1kNS7X/+BLcY=
github.com/gotd/td v0.161.0/go.mod h1:7HdCs+zeJugdgZAF5iG8f70eOJvuiH2QzjoyUcysXbY=
github.com/gotd/td v0.163.0 h1:A/QHpyMmYaQgyOY//vySoKa1wQP8NCAo3/5OcgfZVnU=
github.com/gotd/td v0.163.0/go.mod h1:ZsGbErIos7XHF7HL0VlsjL6mmX4b1JpinHfDb7gOdvs=
github.com/gotd/teled v0.5.0 h1:jcO5sMVPCKLMBvBOrauhPzGIaxv6bxvmkJ6V9cftKto=
github.com/gotd/teled v0.5.0/go.mod h1:7jbroRefOZTTvWZiQrBxyThCcIa1USrLfhEG8Gt7taU=
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 h1:Dj0L5fhJ9F82ZJyVOmBx6msDp/kfd1t9GRfny/mfJA0=
Expand All @@ -105,10 +105,10 @@ github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QII
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/k0kubun/pp v2.3.0+incompatible h1:EKhKbi34VQDWJtq+zpsKSEhkHHs9w2P8Izbq8IhLVSo=
github.com/k0kubun/pp/v3 v3.5.1 h1:fS8Xt0MWVVSiKwfXeIdE0WJlktdA87/gt0Hs0+j2R2s=
github.com/k0kubun/pp/v3 v3.5.1/go.mod h1:s7qPOSp65uuilpprLJs2yDi9DNd7JGyWJPtPvDFpG9w=
github.com/klauspost/compress v1.19.0 h1:sXLILfc9jV2QYWkzFOPWStmcUVH2RHEB1JCdY2oVvCQ=
github.com/klauspost/compress v1.19.0/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/k0kubun/pp/v3 v3.5.2 h1:oBEa3Nft1C/k4/xCtAgv+siTiHJHFXg2FDOdC+y/bWM=
github.com/k0kubun/pp/v3 v3.5.2/go.mod h1:lNnOiHoLg3XXNPt3mKuR1vx8dmw+RLKYJ1WfnfpNjgI=
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -123,8 +123,8 @@ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-colorable v0.1.15 h1:+u9SLTRGnXv73cEsnsmoZBom+dMU88B2M0aDcWy0/jY=
github.com/mattn/go-colorable v0.1.15/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4=
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
Expand Down Expand Up @@ -186,8 +186,8 @@ github.com/tklauser/numcpus v0.11.0 h1:nSTwhKH5e1dMNsCdVBukSZrURJRoHbSEQjdEbY+9R
github.com/tklauser/numcpus v0.11.0/go.mod h1:z+LwcLq54uWZTX0u/bGobaV34u6V7KNlTZejzM6/3MQ=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
github.com/yuin/goldmark v1.8.4 h1:oat/nd3U6NeQqFEL3xpEJq7d7c86NI+DbSNGAs4xnjA=
github.com/yuin/goldmark v1.8.4/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yuin/goldmark v1.8.5 h1:r6N5afV5qj/5S4UTch8agZHJ8UxNCMwX7WjkkJam2NA=
github.com/yuin/goldmark v1.8.5/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.etcd.io/bbolt v1.5.0 h1:S7GAl7Fxv12yohbwFfIbQCGDWbQbtDGPET4P/bD4lxU=
Expand Down
14 changes: 7 additions & 7 deletions markup_to_tg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
func replyMarkupToTg(m ReplyMarkup) (tg.ReplyMarkupClass, error) {
switch m := m.(type) {
case *InlineKeyboardMarkup:
rows := make([]tg.KeyboardButtonRow, 0, len(m.InlineKeyboard))
rows := make([]tg.KeyboardInlineButtonRow, 0, len(m.InlineKeyboard))
for _, row := range m.InlineKeyboard {
buttons := make([]tg.KeyboardButtonClass, len(row))
buttons := make([]tg.KeyboardInlineButton, len(row))
for i, btn := range row {
b, err := inlineButtonToTg(btn)
if err != nil {
Expand All @@ -25,14 +25,14 @@ func replyMarkupToTg(m ReplyMarkup) (tg.ReplyMarkupClass, error) {
buttons[i] = b
}

rows = append(rows, tg.KeyboardButtonRow{Buttons: buttons})
rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: buttons})
}

return &tg.ReplyInlineMarkup{Rows: rows}, nil
case *ReplyKeyboardMarkup:
rows := make([]tg.KeyboardButtonRow, 0, len(m.Keyboard))
for _, row := range m.Keyboard {
buttons := make([]tg.KeyboardButtonClass, len(row))
buttons := make([]tg.KeyboardButton, len(row))
for i, btn := range row {
buttons[i] = keyboardButtonToTg(btn)
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func replyMarkupToTg(m ReplyMarkup) (tg.ReplyMarkupClass, error) {
}
}

func inlineButtonToTg(btn InlineKeyboardButton) (tg.KeyboardButtonClass, error) {
func inlineButtonToTg(btn InlineKeyboardButton) (tg.KeyboardInlineButton, error) {
switch {
case btn.URL != "":
return markup.URL(btn.Text, btn.URL), nil
Expand All @@ -85,11 +85,11 @@ func inlineButtonToTg(btn InlineKeyboardButton) (tg.KeyboardButtonClass, error)
case btn.Pay:
return markup.Buy(btn.Text), nil
default:
return nil, &Error{Code: 400, Description: "Bad Request: text buttons are unallowed in the inline keyboard"}
return tg.KeyboardInlineButton{}, &Error{Code: 400, Description: "Bad Request: text buttons are unallowed in the inline keyboard"}
}
}

func keyboardButtonToTg(btn KeyboardButton) tg.KeyboardButtonClass {
func keyboardButtonToTg(btn KeyboardButton) tg.KeyboardButton {
switch {
case btn.RequestUsers != nil:
return requestUsersToTg(btn.Text, btn.RequestUsers)
Expand Down
16 changes: 8 additions & 8 deletions markup_to_tg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ func TestReplyMarkupToTg_Inline(t *testing.T) {
t.Fatalf("unexpected: %#v", got)
}

if _, isURL := inline.Rows[0].Buttons[0].(*tg.KeyboardButtonURL); !isURL {
t.Fatalf("button 0: want URL, got %T", inline.Rows[0].Buttons[0])
if _, isURL := inline.Rows[0].Buttons[0].Type.(*tg.InlineButtonTypeURL); !isURL {
t.Fatalf("button 0: want URL, got %T", inline.Rows[0].Buttons[0].Type)
}

cb, isCb := inline.Rows[0].Buttons[1].(*tg.KeyboardButtonCallback)
cb, isCb := inline.Rows[0].Buttons[1].Type.(*tg.InlineButtonTypeCallback)
if !isCb || string(cb.Data) != "ok:1" {
t.Fatalf("button 1: want callback ok:1, got %#v", inline.Rows[0].Buttons[1])
}

si, isSwitch := inline.Rows[0].Buttons[2].(*tg.KeyboardButtonSwitchInline)
si, isSwitch := inline.Rows[0].Buttons[2].Type.(*tg.InlineButtonTypeSwitchInline)
if !isSwitch || si.Query != "go" || si.SamePeer {
t.Fatalf("button 2: want switch-inline, got %#v", inline.Rows[0].Buttons[2])
}
Expand Down Expand Up @@ -64,12 +64,12 @@ func TestReplyMarkupToTg_Reply(t *testing.T) {
t.Fatalf("unexpected: %#v", got)
}

if _, ok := kb.Rows[0].Buttons[1].(*tg.KeyboardButtonRequestPhone); !ok {
t.Fatalf("want request-phone, got %T", kb.Rows[0].Buttons[1])
if _, ok := kb.Rows[0].Buttons[1].Type.(*tg.ButtonTypeRequestPhone); !ok {
t.Fatalf("want request-phone, got %T", kb.Rows[0].Buttons[1].Type)
}

if _, ok := kb.Rows[0].Buttons[2].(*tg.KeyboardButtonRequestGeoLocation); !ok {
t.Fatalf("want request-geo, got %T", kb.Rows[0].Buttons[2])
if _, ok := kb.Rows[0].Buttons[2].Type.(*tg.ButtonTypeRequestGeoLocation); !ok {
t.Fatalf("want request-geo, got %T", kb.Rows[0].Buttons[2].Type)
}
}

Expand Down
4 changes: 2 additions & 2 deletions prepared_keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestSavePreparedKeyboardButtonUsers(t *testing.T) {

inv.decode(t, tg.BotsRequestWebViewButtonRequestTypeID, &req)

rp, ok := req.Button.(*tg.KeyboardButtonRequestPeer)
rp, ok := req.Button.Type.(*tg.ButtonTypeRequestPeer)
if !ok || rp.ButtonID != 7 || rp.MaxQuantity != 3 {
t.Fatalf("button = %#v", req.Button)
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestSavePreparedKeyboardButtonChat(t *testing.T) {

inv.decode(t, tg.BotsRequestWebViewButtonRequestTypeID, &req)

rp := req.Button.(*tg.KeyboardButtonRequestPeer)
rp := req.Button.Type.(*tg.ButtonTypeRequestPeer)

bc, ok := rp.PeerType.(*tg.RequestPeerTypeBroadcast)
if !ok || !bc.Creator {
Expand Down
27 changes: 14 additions & 13 deletions request_peer_button.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package botapi

import "github.com/gotd/td/tg"
import (
"github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/tg"
)

// KeyboardButtonRequestUsers defines the criteria used to request suitable users
// when pressed. Maps to a request-peer keyboard button.
Expand Down Expand Up @@ -50,7 +53,7 @@ type KeyboardButtonRequestChat struct {
}

// requestUsersToTg builds the MTProto request-peer button for a user request.
func requestUsersToTg(text string, r *KeyboardButtonRequestUsers) *tg.KeyboardButtonRequestPeer {
func requestUsersToTg(text string, r *KeyboardButtonRequestUsers) tg.KeyboardButton {
peerType := &tg.RequestPeerTypeUser{}
if r.UserIsBot != nil {
peerType.SetBot(*r.UserIsBot)
Expand All @@ -65,16 +68,18 @@ func requestUsersToTg(text string, r *KeyboardButtonRequestUsers) *tg.KeyboardBu
maxQuantity = 1
}

return &tg.KeyboardButtonRequestPeer{
Text: text,
ButtonID: r.RequestID,
PeerType: peerType,
MaxQuantity: maxQuantity,
return tg.KeyboardButton{
Text: text,
Type: &tg.ButtonTypeRequestPeer{
ButtonID: r.RequestID,
PeerType: peerType,
MaxQuantity: maxQuantity,
},
}
}

// requestChatToTg builds the MTProto request-peer button for a chat request.
func requestChatToTg(text string, r *KeyboardButtonRequestChat) *tg.KeyboardButtonRequestPeer {
func requestChatToTg(text string, r *KeyboardButtonRequestChat) tg.KeyboardButton {
var peerType tg.RequestPeerTypeClass

if r.ChatIsChannel {
Expand Down Expand Up @@ -113,9 +118,5 @@ func requestChatToTg(text string, r *KeyboardButtonRequestChat) *tg.KeyboardButt
peerType = chat
}

return &tg.KeyboardButtonRequestPeer{
Text: text,
ButtonID: r.RequestID,
PeerType: peerType,
}
return markup.RequestPeer(text, r.RequestID, peerType)
}
2 changes: 1 addition & 1 deletion verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (b *Bot) setCustomVerification(ctx context.Context, peer tg.InputPeerClass,
req.SetEnabled(enabled)

if enabled && cfg.description != "" {
req.SetCustomDescription(cfg.description)
req.SetCustomDescription(tg.TextWithEntities{Text: cfg.description})
}

if _, err := b.raw.BotsSetCustomVerification(ctx, req); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestVerifyUser(t *testing.T) {
t.Fatal("enabled should be true")
}

if desc, ok := req.GetCustomDescription(); !ok || desc != "trusted" {
if desc, ok := req.GetCustomDescription(); !ok || desc.Text != "trusted" {
t.Fatalf("description = %q, ok=%v", desc, ok)
}

Expand Down
Loading