diff --git a/convert.go b/convert.go index 16682d0..2819f0a 100644 --- a/convert.go +++ b/convert.go @@ -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 } diff --git a/convert_message_test.go b/convert_message_test.go index 67f0993..dee6f05 100644 --- a/convert_message_test.go +++ b/convert_message_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/gotd/td/bin" + "github.com/gotd/td/telegram/message/markup" "github.com/gotd/td/tg" ) @@ -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) diff --git a/convert_test.go b/convert_test.go index 518045c..8f7d897 100644 --- a/convert_test.go +++ b/convert_test.go @@ -3,6 +3,7 @@ package botapi import ( "testing" + "github.com/gotd/td/telegram/message/markup" "github.com/gotd/td/tg" ) @@ -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) diff --git a/go.mod b/go.mod index 751f847..8904168 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 46e695b..67705f5 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/markup_to_tg.go b/markup_to_tg.go index 13ab22f..fba7e0a 100644 --- a/markup_to_tg.go +++ b/markup_to_tg.go @@ -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 { @@ -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) } @@ -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 @@ -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) diff --git a/markup_to_tg_test.go b/markup_to_tg_test.go index 9311bca..c93d3c2 100644 --- a/markup_to_tg_test.go +++ b/markup_to_tg_test.go @@ -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]) } @@ -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) } } diff --git a/prepared_keyboard_test.go b/prepared_keyboard_test.go index dea522f..4227156 100644 --- a/prepared_keyboard_test.go +++ b/prepared_keyboard_test.go @@ -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) } @@ -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 { diff --git a/request_peer_button.go b/request_peer_button.go index 20c3a7e..1dd134c 100644 --- a/request_peer_button.go +++ b/request_peer_button.go @@ -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. @@ -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) @@ -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 { @@ -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) } diff --git a/verification.go b/verification.go index 8800b0a..8d9c366 100644 --- a/verification.go +++ b/verification.go @@ -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 { diff --git a/verification_test.go b/verification_test.go index 3fe93ae..d434394 100644 --- a/verification_test.go +++ b/verification_test.go @@ -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) }