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
14 changes: 14 additions & 0 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions methods_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
47 changes: 47 additions & 0 deletions methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions models/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down
77 changes: 77 additions & 0 deletions models/chat_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
33 changes: 17 additions & 16 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"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"`
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
Expand Down
68 changes: 68 additions & 0 deletions models/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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)
}
})
}
}

// 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)
}
})
}
}
Loading