From cc4f0e783b3364ac6648eee783c45c7d3dc53260 Mon Sep 17 00:00:00 2001 From: InkCrow Date: Sun, 22 Nov 2026 00:08:47 +0800 Subject: [PATCH 1/2] feat: support join request queries from Bot API 10.1 --- methods.go | 14 +++++++++ methods_params.go | 10 ++++++ methods_test.go | 47 +++++++++++++++++++++++++++ models/chat.go | 11 +++++++ models/chat_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ models/user.go | 33 +++++++++---------- models/user_test.go | 37 ++++++++++++++++++++++ 7 files changed, 213 insertions(+), 16 deletions(-) create mode 100644 models/chat_test.go create mode 100644 models/user_test.go diff --git a/methods.go b/methods.go index 49fb3635..47d34815 100644 --- a/methods.go +++ b/methods.go @@ -361,6 +361,20 @@ func (b *Bot) DeclineChatJoinRequest(ctx context.Context, params *DeclineChatJoi return result, err } +// AnswerChatJoinRequestQuery https://core.telegram.org/bots/api#answerchatjoinrequestquery +func (b *Bot) AnswerChatJoinRequestQuery(ctx context.Context, params *AnswerChatJoinRequestQueryParams) (bool, error) { + var result bool + err := b.rawRequest(ctx, "answerChatJoinRequestQuery", params, &result) + return result, err +} + +// SendChatJoinRequestWebApp https://core.telegram.org/bots/api#sendchatjoinrequestwebapp +func (b *Bot) SendChatJoinRequestWebApp(ctx context.Context, params *SendChatJoinRequestWebAppParams) (bool, error) { + var result bool + err := b.rawRequest(ctx, "sendChatJoinRequestWebApp", params, &result) + return result, err +} + // SetChatPhoto https://core.telegram.org/bots/api#setchatphoto func (b *Bot) SetChatPhoto(ctx context.Context, params *SetChatPhotoParams) (bool, error) { var result bool diff --git a/methods_params.go b/methods_params.go index cd68efab..261b700b 100644 --- a/methods_params.go +++ b/methods_params.go @@ -639,6 +639,16 @@ type DeclineChatJoinRequestParams struct { UserID int64 `json:"user_id"` } +type AnswerChatJoinRequestQueryParams struct { + ChatJoinRequestQueryID string `json:"chat_join_request_query_id"` + Result models.ChatJoinRequestQueryResult `json:"result"` +} + +type SendChatJoinRequestWebAppParams struct { + ChatJoinRequestQueryID string `json:"chat_join_request_query_id"` + WebAppURL string `json:"web_app_url"` +} + type SetChatPhotoParams struct { ChatID any `json:"chat_id"` Photo models.InputFile `json:"photo"` diff --git a/methods_test.go b/methods_test.go index 2879df8e..9ffb1a76 100644 --- a/methods_test.go +++ b/methods_test.go @@ -582,6 +582,53 @@ func TestBot_Methods(t *testing.T) { assertTrue(t, resp) }) + t.Run("AnswerChatJoinRequestQuery", func(t *testing.T) { + for _, tc := range []struct { + result models.ChatJoinRequestQueryResult + want string + }{ + {models.ChatJoinRequestQueryResultApprove, "approve"}, + {models.ChatJoinRequestQueryResultDecline, "decline"}, + {models.ChatJoinRequestQueryResultQueue, "queue"}, + } { + c := &httpClient{ + t: t, + resp: `true`, + reqPath: "/bot/answerChatJoinRequestQuery", + reqFields: map[string]string{ + "chat_join_request_query_id": "q1", + "result": tc.want, + }, + } + b := &Bot{client: c} + resp, err := b.AnswerChatJoinRequestQuery(context.Background(), &AnswerChatJoinRequestQueryParams{ + ChatJoinRequestQueryID: "q1", + Result: tc.result, + }) + assertNoErr(t, err) + assertTrue(t, resp) + } + }) + + t.Run("SendChatJoinRequestWebApp", func(t *testing.T) { + c := &httpClient{ + t: t, + resp: `true`, + reqPath: "/bot/sendChatJoinRequestWebApp", + reqFields: map[string]string{ + "chat_join_request_query_id": "q1", + "web_app_url": "https://example.com/app", + }, + } + b := &Bot{client: c} + resp, err := b.SendChatJoinRequestWebApp(context.Background(), &SendChatJoinRequestWebAppParams{ + ChatJoinRequestQueryID: "q1", + WebAppURL: "https://example.com/app", + }) + assertNoErr(t, err) + assertTrue(t, resp) + }) + t.Run("SetChatPhoto", func(t *testing.T) { c := &httpClient{t: t, resp: `true`, reqFields: map[string]string{ "chat_id": "123", diff --git a/models/chat.go b/models/chat.go index 171c8a7b..718f17f1 100644 --- a/models/chat.go +++ b/models/chat.go @@ -8,8 +8,18 @@ type ChatJoinRequest struct { Date int `json:"date"` Bio string `json:"bio,omitempty"` InviteLink *ChatInviteLink `json:"invite_link,omitempty"` + QueryID string `json:"query_id,omitempty"` } +// ChatJoinRequestQueryResult https://core.telegram.org/bots/api#answerchatjoinrequestquery +type ChatJoinRequestQueryResult string + +const ( + ChatJoinRequestQueryResultApprove ChatJoinRequestQueryResult = "approve" + ChatJoinRequestQueryResultDecline ChatJoinRequestQueryResult = "decline" + ChatJoinRequestQueryResultQueue ChatJoinRequestQueryResult = "queue" +) + // ChatPhoto https://core.telegram.org/bots/api#chatphoto type ChatPhoto struct { SmallFileID string `json:"small_file_id"` @@ -158,6 +168,7 @@ type ChatFullInfo struct { LinkedChatID int64 `json:"linked_chat_id,omitempty"` Location *ChatLocation `json:"location,omitempty"` PaidMessageStarCount int `json:"paid_message_star_count,omitempty"` + GuardBot *User `json:"guard_bot,omitempty"` Rating *UserRating `json:"rating,omitempty"` FirstProfileAudio *Audio `json:"first_profile_audio,omitempty"` TonWalletCard *TonWalletCard `json:"ton_wallet_card,omitempty"` diff --git a/models/chat_test.go b/models/chat_test.go new file mode 100644 index 00000000..832c1222 --- /dev/null +++ b/models/chat_test.go @@ -0,0 +1,77 @@ +package models + +import ( + "encoding/json" + "testing" +) + +// TestChatJoinRequest_QueryID verifies query_id decodes when present and stays empty when absent. +func TestChatJoinRequest_QueryID(t *testing.T) { + tests := []struct { + name string + src string + want string + }{ + { + name: "with query_id", + src: `{"chat":{"id":1,"type":"supergroup"},"from":{"id":2,"is_bot":false,"first_name":"A"},"user_chat_id":2,"date":1,"query_id":"q1"}`, + want: "q1", + }, + { + name: "without query_id", + src: `{"chat":{"id":1,"type":"supergroup"},"from":{"id":2,"is_bot":false,"first_name":"A"},"user_chat_id":2,"date":1}`, + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var r ChatJoinRequest + if err := json.Unmarshal([]byte(tt.src), &r); err != nil { + t.Fatalf("unmarshal: %v", err) + } + if r.QueryID != tt.want { + t.Fatalf("query_id = %q, want %q", r.QueryID, tt.want) + } + }) + } +} + +// TestChatFullInfo_GuardBot verifies guard_bot decodes when present and stays nil when absent. +func TestChatFullInfo_GuardBot(t *testing.T) { + tests := []struct { + name string + src string + want *User + }{ + { + name: "with guard_bot", + src: `{"id":1,"type":"supergroup","accent_color_id":0,"max_reaction_count":11,"guard_bot":{"id":42,"is_bot":true,"first_name":"Guard","username":"guard_bot"}}`, + want: &User{ID: 42, IsBot: true, FirstName: "Guard", Username: "guard_bot"}, + }, + { + name: "without guard_bot", + src: `{"id":1,"type":"supergroup","accent_color_id":0,"max_reaction_count":11}`, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var c ChatFullInfo + if err := json.Unmarshal([]byte(tt.src), &c); err != nil { + t.Fatalf("unmarshal: %v", err) + } + if tt.want == nil { + if c.GuardBot != nil { + t.Fatalf("guard_bot = %+v, want nil", c.GuardBot) + } + return + } + if c.GuardBot == nil { + t.Fatal("guard_bot not decoded") + } + if *c.GuardBot != *tt.want { + t.Fatalf("guard_bot = %+v, want %+v", *c.GuardBot, *tt.want) + } + }) + } +} diff --git a/models/user.go b/models/user.go index 617e14d5..d5d94b80 100644 --- a/models/user.go +++ b/models/user.go @@ -8,22 +8,23 @@ type UserProfilePhotos struct { // User https://core.telegram.org/bots/api#user type User struct { - ID int64 `json:"id"` - IsBot bool `json:"is_bot"` - FirstName string `json:"first_name,omitempty"` - LastName string `json:"last_name,omitempty"` - Username string `json:"username,omitempty"` - LanguageCode string `json:"language_code,omitempty"` - IsPremium bool `json:"is_premium,omitempty"` - AddedToAttachmentMenu bool `json:"added_to_attachment_menu,omitempty"` - CanJoinGroups bool `json:"can_join_groups,omitempty"` - CanReadAllGroupMessages bool `json:"can_read_all_group_messages,omitempty"` - SupportInlineQueries bool `json:"support_inline_queries,omitempty"` - CanConnectToBusiness bool `json:"can_connect_to_business,omitempty"` - HasTopicsEnabled bool `json:"has_topics_enabled,omitempty"` - AllowsUsersToCreateTopics bool `json:"allows_users_to_create_topics,omitempty"` - CanManageBots bool `json:"can_manage_bots,omitempty"` - SupportsGuestQueries bool `json:"supports_guest_queries,omitempty"` + ID int64 `json:"id"` + IsBot bool `json:"is_bot"` + FirstName string `json:"first_name,omitempty"` + LastName string `json:"last_name,omitempty"` + Username string `json:"username,omitempty"` + LanguageCode string `json:"language_code,omitempty"` + IsPremium bool `json:"is_premium,omitempty"` + AddedToAttachmentMenu bool `json:"added_to_attachment_menu,omitempty"` + CanJoinGroups bool `json:"can_join_groups,omitempty"` + CanReadAllGroupMessages bool `json:"can_read_all_group_messages,omitempty"` + SupportInlineQueries bool `json:"support_inline_queries,omitempty"` + CanConnectToBusiness bool `json:"can_connect_to_business,omitempty"` + HasTopicsEnabled bool `json:"has_topics_enabled,omitempty"` + AllowsUsersToCreateTopics bool `json:"allows_users_to_create_topics,omitempty"` + CanManageBots bool `json:"can_manage_bots,omitempty"` + SupportsJoinRequestQueries bool `json:"supports_join_request_queries,omitempty"` + SupportsGuestQueries bool `json:"supports_guest_queries,omitempty"` } // UserProfileAudios https://core.telegram.org/bots/api#userprofileaudios diff --git a/models/user_test.go b/models/user_test.go new file mode 100644 index 00000000..e89e11d1 --- /dev/null +++ b/models/user_test.go @@ -0,0 +1,37 @@ +package models + +import ( + "encoding/json" + "testing" +) + +// TestUser_SupportsJoinRequestQueries verifies supports_join_request_queries decodes when present and stays false when absent. +func TestUser_SupportsJoinRequestQueries(t *testing.T) { + tests := []struct { + name string + src string + want bool + }{ + { + name: "with supports_join_request_queries", + src: `{"id":1,"is_bot":true,"first_name":"Bot","supports_join_request_queries":true}`, + want: true, + }, + { + name: "without supports_join_request_queries", + src: `{"id":1,"is_bot":true,"first_name":"Bot"}`, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var u User + if err := json.Unmarshal([]byte(tt.src), &u); err != nil { + t.Fatalf("unmarshal: %v", err) + } + if u.SupportsJoinRequestQueries != tt.want { + t.Fatalf("supports_join_request_queries = %v, want %v", u.SupportsJoinRequestQueries, tt.want) + } + }) + } +} From 956219d9aab6cbe17cf9e8456d1e5164d9f17621 Mon Sep 17 00:00:00 2001 From: InkCrow Date: Sun, 22 Nov 2026 00:34:11 +0800 Subject: [PATCH 2/2] fix: decode User.SupportInlineQueries from supports_inline_queries --- models/user.go | 2 +- models/user_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/models/user.go b/models/user.go index d5d94b80..f5f5836b 100644 --- a/models/user.go +++ b/models/user.go @@ -18,7 +18,7 @@ type User struct { AddedToAttachmentMenu bool `json:"added_to_attachment_menu,omitempty"` CanJoinGroups bool `json:"can_join_groups,omitempty"` CanReadAllGroupMessages bool `json:"can_read_all_group_messages,omitempty"` - SupportInlineQueries bool `json:"support_inline_queries,omitempty"` + SupportInlineQueries bool `json:"supports_inline_queries,omitempty"` CanConnectToBusiness bool `json:"can_connect_to_business,omitempty"` HasTopicsEnabled bool `json:"has_topics_enabled,omitempty"` AllowsUsersToCreateTopics bool `json:"allows_users_to_create_topics,omitempty"` diff --git a/models/user_test.go b/models/user_test.go index e89e11d1..256c2ef1 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -35,3 +35,34 @@ func TestUser_SupportsJoinRequestQueries(t *testing.T) { }) } } + +// TestUser_SupportInlineQueries verifies supports_inline_queries decodes when present and stays false when absent. +func TestUser_SupportInlineQueries(t *testing.T) { + tests := []struct { + name string + src string + want bool + }{ + { + name: "with supports_inline_queries", + src: `{"id":1,"is_bot":true,"first_name":"Bot","supports_inline_queries":true}`, + want: true, + }, + { + name: "without supports_inline_queries", + src: `{"id":1,"is_bot":true,"first_name":"Bot"}`, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var u User + if err := json.Unmarshal([]byte(tt.src), &u); err != nil { + t.Fatalf("unmarshal: %v", err) + } + if u.SupportInlineQueries != tt.want { + t.Fatalf("supports_inline_queries = %v, want %v", u.SupportInlineQueries, tt.want) + } + }) + } +}