Skip to content

Commit

Permalink
implement bot api 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Nov 5, 2021
1 parent 13d54ae commit 8f5f797
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 7 deletions.
14 changes: 12 additions & 2 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand All @@ -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.
Expand All @@ -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.
Expand Down
61 changes: 57 additions & 4 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -248,6 +249,7 @@ func (b *Bot) NewContext(upd Update) Context {
pollAnswer: upd.PollAnswer,
myChatMember: upd.MyChatMember,
chatMember: upd.ChatMember,
chatJoinRequest: upd.ChatJoinRequest,
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
29 changes: 28 additions & 1 deletion chat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package telebot

import "strconv"
import (
"strconv"
"time"
)

// User object represents a Telegram user, bot.
type User struct {
Expand Down Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -160,6 +163,7 @@ type nativeContext struct {
pollAnswer *PollAnswer
myChatMember *ChatMemberUpdate
chatMember *ChatMemberUpdate
chatJoinRequest *ChatJoinRequest

lock sync.RWMutex
store map[string]interface{}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions telebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8f5f797

Please sign in to comment.