From 8f5f797de8c176f8c594a6240636270187bd603d Mon Sep 17 00:00:00 2001 From: Demian Date: Fri, 5 Nov 2021 22:31:32 +0200 Subject: [PATCH] implement bot api 5.4 --- admin.go | 14 +++++++++++-- bot.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---- chat.go | 29 +++++++++++++++++++++++++- context.go | 8 +++++++ telebot.go | 4 ++++ 5 files changed, 109 insertions(+), 7 deletions(-) diff --git a/admin.go b/admin.go index 0fe7064..066bd90 100644 --- a/admin.go +++ b/admin.go @@ -11,6 +11,9 @@ type ChatInviteLink struct { // The invite link. InviteLink string `json:"invite_link"` + // Invite link name. + Name string `json:"name"` + // The creator of the link. Creator *User `json:"creator"` @@ -21,12 +24,19 @@ type ChatInviteLink struct { IsRevoked bool `json:"is_revoked"` // (Optional) Point in time when the link will expire, - // use ExpireDate() to get time.Time + // use ExpireDate() to get time.Time. ExpireUnixtime int64 `json:"expire_date,omitempty"` // (Optional) Maximum number of users that can be members of // the chat simultaneously. MemberLimit int `json:"member_limit,omitempty"` + + // (Optional) True, if users joining the chat via the link need to + // be approved by chat administrators. If True, member_limit can't be specified. + JoinRequest bool `json:"creates_join_request"` + + // (Optional) Number of pending join requests created using this link. + PendingCount int `json:"pending_join_request_count"` } // ExpireDate returns the moment of the link expiration in local time. @@ -42,7 +52,7 @@ type ChatMemberUpdate struct { // Sender which user the action was triggered. Sender *User `json:"from"` - // Unixtime, use Date() to get time.Time + // Unixtime, use Date() to get time.Time. Unixtime int64 `json:"date"` // Previous information about the chat member. diff --git a/bot.go b/bot.go index 8ef4e15..fbdd386 100644 --- a/bot.go +++ b/bot.go @@ -134,6 +134,7 @@ type Update struct { PollAnswer *PollAnswer `json:"poll_answer,omitempty"` MyChatMember *ChatMemberUpdate `json:"my_chat_member,omitempty"` ChatMember *ChatMemberUpdate `json:"chat_member,omitempty"` + ChatJoinRequest *ChatJoinRequest `json:"chat_join_request,omitempty"` } // Command represents a bot command. @@ -248,6 +249,7 @@ func (b *Bot) NewContext(upd Update) Context { pollAnswer: upd.PollAnswer, myChatMember: upd.MyChatMember, chatMember: upd.ChatMember, + chatJoinRequest: upd.ChatJoinRequest, } } @@ -486,6 +488,11 @@ func (b *Bot) ProcessUpdate(upd Update) { b.handle(OnChatMember, c) return } + + if upd.ChatJoinRequest != nil { + b.handle(OnChatJoinRequest, c) + return + } } func (b *Bot) handle(end string, c Context) bool { @@ -1572,8 +1579,16 @@ func (b *Bot) CreateInviteLink(chat *Chat, link *ChatInviteLink) (*ChatInviteLin "chat_id": chat.Recipient(), } if link != nil { - params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10) - params["member_limit"] = strconv.Itoa(link.MemberLimit) + params["name"] = link.Name + + if link.ExpireUnixtime != 0 { + params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10) + } + if link.MemberLimit > 0 { + params["member_limit"] = strconv.Itoa(link.MemberLimit) + } else if link.JoinRequest { + params["creates_join_request"] = "true" + } } data, err := b.Raw("createChatInviteLink", params) @@ -1596,8 +1611,16 @@ func (b *Bot) EditInviteLink(chat *Chat, link *ChatInviteLink) (*ChatInviteLink, } if link != nil { params["invite_link"] = link.InviteLink - params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10) - params["member_limit"] = strconv.Itoa(link.MemberLimit) + params["name"] = link.Name + + if link.ExpireUnixtime != 0 { + params["expire_date"] = strconv.FormatInt(link.ExpireUnixtime, 10) + } + if link.MemberLimit > 0 { + params["member_limit"] = strconv.Itoa(link.MemberLimit) + } else if link.JoinRequest { + params["creates_join_request"] = "true" + } } data, err := b.Raw("editChatInviteLink", params) @@ -1632,3 +1655,33 @@ func (b *Bot) RevokeInviteLink(chat *Chat, link string) (*ChatInviteLink, error) return &resp, nil } + +// ApproveChatJoinRequest approves a chat join request. +func (b *Bot) ApproveChatJoinRequest(chat *Chat, user *User) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "user_id": user.Recipient(), + } + + data, err := b.Raw("approveChatJoinRequest", params) + if err != nil { + return err + } + + return extractOk(data) +} + +// DeclineChatJoinRequest declines a chat join request. +func (b *Bot) DeclineChatJoinRequest(chat *Chat, user *User) error { + params := map[string]string{ + "chat_id": chat.Recipient(), + "user_id": user.Recipient(), + } + + data, err := b.Raw("declineChatJoinRequest", params) + if err != nil { + return err + } + + return extractOk(data) +} diff --git a/chat.go b/chat.go index cf94e04..b763a42 100644 --- a/chat.go +++ b/chat.go @@ -1,6 +1,9 @@ package telebot -import "strconv" +import ( + "strconv" + "time" +) // User object represents a Telegram user, bot. type User struct { @@ -117,3 +120,27 @@ type ChatID int64 func (i ChatID) Recipient() string { return strconv.FormatInt(int64(i), 10) } + +// ChatJoinRequest represents a join request sent to a chat. +type ChatJoinRequest struct { + // Chat to which the request was sent. + Chat *Chat `json:"chat"` + + // Sender is the user that sent the join request. + Sender *User `json:"user"` + + // Unixtime, use ChatJoinRequest.Time() to get time.Time. + Unixtime int64 `json:"date"` + + // Bio of the user, optional. + Bio string `json:"bio"` + + // InviteLink is the chat invite link that was used by + //the user to send the join request, optional. + InviteLink *ChatInviteLink `json:"invite_link"` +} + +// Time returns the moment of chat join request sending in local time. +func (r ChatJoinRequest) Time() time.Time { + return time.Unix(r.Unixtime, 0) +} diff --git a/context.go b/context.go index a101292..2f09579 100644 --- a/context.go +++ b/context.go @@ -46,6 +46,9 @@ type Context interface { // ChatMember returns chat member changes. ChatMember() *ChatMemberUpdate + // ChatJoinRequest returns cha + ChatJoinRequest() *ChatJoinRequest + // Migration returns both migration from and to chat IDs. Migration() (int64, int64) @@ -160,6 +163,7 @@ type nativeContext struct { pollAnswer *PollAnswer myChatMember *ChatMemberUpdate chatMember *ChatMemberUpdate + chatJoinRequest *ChatJoinRequest lock sync.RWMutex store map[string]interface{} @@ -215,6 +219,10 @@ func (c *nativeContext) ChatMember() *ChatMemberUpdate { } } +func (c *nativeContext) ChatJoinRequest() *ChatJoinRequest { + return c.chatJoinRequest +} + func (c *nativeContext) Poll() *Poll { return c.poll } diff --git a/telebot.go b/telebot.go index 498175d..b187e7a 100644 --- a/telebot.go +++ b/telebot.go @@ -111,6 +111,9 @@ const ( // Will fire on chat member's changes. OnChatMember = "\achat_member" + // Will fire on chat join request. + OnChatJoinRequest = "\achat_join_request" + // Will fire on the start of a voice chat. OnVoiceChatStarted = "\avoice_chat_started" @@ -144,6 +147,7 @@ const ( RecordingAudio ChatAction = "record_audio" RecordingVNote ChatAction = "record_video_note" FindingLocation ChatAction = "find_location" + ChoosingSticker ChatAction = "choose_sticker" ) // ParseMode determines the way client applications treat the text of the message