From 9c5e9addf055978d194e838d45db6efc1dd9d5a6 Mon Sep 17 00:00:00 2001 From: Vladimir Fetisov Date: Sat, 22 Jan 2022 16:58:32 +0300 Subject: [PATCH] add WithContext functions --- bot.go | 169 ++++++++++++++++++++++++++++++++++++++++++++++++----- button.go | 13 ++++- chat.go | 127 ++++++++++++++++++++++++++++++++++++---- client.go | 150 +++++++++++++++++++++++++++++++++++++++-------- message.go | 96 +++++++++++++++++++++++++----- 5 files changed, 485 insertions(+), 70 deletions(-) diff --git a/bot.go b/bot.go index c03cd0d..8699d0e 100644 --- a/bot.go +++ b/bot.go @@ -30,80 +30,200 @@ type Bot struct { // GetInfo returns information about bot: // id, name, about, avatar +// +// GetInfo uses context.Background internally; to specify the context, use +// GetInfoWithContext. func (b *Bot) GetInfo() (*BotInfo, error) { - return b.client.GetInfo() + return b.GetInfoWithContext(context.Background()) +} + +// GetInfoWithContext returns information about bot: +// id, name, about, avatar +func (b *Bot) GetInfoWithContext(ctx context.Context) (*BotInfo, error) { + return b.client.GetInfoWithContext(ctx) } // GetChatInfo returns information about chat: // id, type, title, public, group, inviteLink, admins +// +// GetChatInfo uses context.Background internally; to specify the context, use +// GetChatInfoWithContext. func (b *Bot) GetChatInfo(chatID string) (*Chat, error) { - return b.client.GetChatInfo(chatID) + return b.GetChatInfoWithContext(context.Background(), chatID) +} + +// GetChatInfoWithContext returns information about chat: +// id, type, title, public, group, inviteLink, admins +func (b *Bot) GetChatInfoWithContext(ctx context.Context, chatID string) (*Chat, error) { + return b.client.GetChatInfoWithContext(ctx, chatID) } // SendChatActions sends an actions like "typing, looking" +// +// SendChatActions uses context.Background internally; to specify the context, use +// SendChatActionsWithContext. func (b *Bot) SendChatActions(chatID string, actions ...ChatAction) error { - return b.client.SendChatActions(chatID, actions...) + return b.SendChatActionsWithContext(context.Background(), chatID, actions...) +} + +// SendChatActionsWithContext sends an actions like "typing, looking" +func (b *Bot) SendChatActionsWithContext(ctx context.Context, chatID string, actions ...ChatAction) error { + return b.client.SendChatActionsWithContext(ctx, chatID, actions...) } // GetChatAdmins returns chat admins list with fields: // userID, creator flag +// +// GetChatAdmins uses context.Background internally; to specify the context, use +// GetChatAdminsWithContext. func (b *Bot) GetChatAdmins(chatID string) ([]ChatMember, error) { - return b.client.GetChatAdmins(chatID) + return b.GetChatAdminsWithContext(context.Background(), chatID) +} + +// GetChatAdminsWithContext returns chat admins list with fields: +// userID, creator flag +func (b *Bot) GetChatAdminsWithContext(ctx context.Context, chatID string) ([]ChatMember, error) { + return b.client.GetChatAdminsWithContext(ctx, chatID) } // GetChatMem returns chat members list with fields: // userID, creator flag, admin flag +// +// GetChatMembers uses context.Background internally; to specify the context, use +// GetChatMembersWithContext. func (b *Bot) GetChatMembers(chatID string) ([]ChatMember, error) { - return b.client.GetChatMembers(chatID) + return b.GetChatMembersWithContext(context.Background(), chatID) +} + +// GetChatMembersWithContext returns chat members list with fields: +// userID, creator flag, admin flag +func (b *Bot) GetChatMembersWithContext(ctx context.Context, chatID string) ([]ChatMember, error) { + return b.client.GetChatMembersWithContext(ctx, chatID) } // GetChatBlockedUsers returns chat blocked users list: // userID +// +// GetChatBlockedUsers uses context.Background internally; to specify the context, use +// GetChatBlockedUsersWithContext. func (b *Bot) GetChatBlockedUsers(chatID string) ([]User, error) { - return b.client.GetChatBlockedUsers(chatID) + return b.GetChatBlockedUsersWithContext(context.Background(), chatID) +} + +// GetChatBlockedUsersWithContext returns chat blocked users list: +// userID +func (b *Bot) GetChatBlockedUsersWithContext(ctx context.Context, chatID string) ([]User, error) { + return b.client.GetChatBlockedUsersWithContext(ctx, chatID) } // GetChatPendingUsers returns chat join pending users list: // userID +// +// GetChatPendingUsers uses context.Background internally; to specify the context, use +// GetChatPendingUsersWithContext. func (b *Bot) GetChatPendingUsers(chatID string) ([]User, error) { - return b.client.GetChatPendingUsers(chatID) + return b.GetChatPendingUsersWithContext(context.Background(), chatID) +} + +// GetChatPendingUsersWithContext returns chat join pending users list: +// userID +func (b *Bot) GetChatPendingUsersWithContext(ctx context.Context, chatID string) ([]User, error) { + return b.client.GetChatPendingUsersWithContext(ctx, chatID) } // BlockChatUser blocks user and removes him from chat. // If deleteLastMessages is true, the messages written recently will be deleted +// +// BlockChatUser uses context.Background internally; to specify the context, use +// BlockChatUserWithContext. func (b *Bot) BlockChatUser(chatID, userID string, deleteLastMessages bool) error { - return b.client.BlockChatUser(chatID, userID, deleteLastMessages) + return b.BlockChatUserWithContext(context.Background(), chatID, userID, deleteLastMessages) +} + +// BlockChatUserWithContext blocks user and removes him from chat. +// If deleteLastMessages is true, the messages written recently will be deleted +func (b *Bot) BlockChatUserWithContext(ctx context.Context, chatID, userID string, deleteLastMessages bool) error { + return b.client.BlockChatUserWithContext(ctx, chatID, userID, deleteLastMessages) } // UnblockChatUser unblocks user in chat +// +// UnblockChatUser uses context.Background internally; to specify the context, use +// UnblockChatUserWithContext. func (b *Bot) UnblockChatUser(chatID, userID string) error { - return b.client.UnblockChatUser(chatID, userID) + return b.UnblockChatUserWithContext(context.Background(), chatID, userID) +} + +// UnblockChatUserWithContext unblocks user in chat +func (b *Bot) UnblockChatUserWithContext(ctx context.Context, chatID, userID string) error { + return b.client.UnblockChatUserWithContext(ctx, chatID, userID) } // ResolveChatJoinRequests sends a decision to accept/decline user join to chat +// +// ResolveChatJoinRequests uses context.Background internally; to specify the context, use +// ResolveChatJoinRequestsWithContext. func (b *Bot) ResolveChatJoinRequests(chatID, userID string, accept, everyone bool) error { - return b.client.ResolveChatPending(chatID, userID, accept, everyone) + return b.ResolveChatJoinRequestsWithContext(context.Background(), chatID, userID, accept, everyone) +} + +// ResolveChatJoinRequestsWithContext sends a decision to accept/decline user join to chat +func (b *Bot) ResolveChatJoinRequestsWithContext(ctx context.Context, chatID, userID string, accept, everyone bool) error { + return b.client.ResolveChatPendingWithContext(ctx, chatID, userID, accept, everyone) } // SetChatTitle changes chat title +// +// SetChatTitle uses context.Background internally; to specify the context, use +// SetChatTitleWithContext. func (b *Bot) SetChatTitle(chatID, title string) error { - return b.client.SetChatTitle(chatID, title) + return b.SetChatTitleWithContext(context.Background(), chatID, title) +} + +// SetChatTitleWithContext changes chat title +func (b *Bot) SetChatTitleWithContext(ctx context.Context, chatID, title string) error { + return b.client.SetChatTitleWithContext(ctx, chatID, title) } // SetChatAbout changes chat about +// +// SetChatAbout uses context.Background internally; to specify the context, use +// SetChatAboutWithContext. func (b *Bot) SetChatAbout(chatID, about string) error { - return b.client.SetChatAbout(chatID, about) + return b.SetChatAboutWithContext(context.Background(), chatID, about) +} + +// SetChatAboutWithContext changes chat about +func (b *Bot) SetChatAboutWithContext(ctx context.Context, chatID, about string) error { + return b.client.SetChatAboutWithContext(ctx, chatID, about) } // SetChatRules changes chat rules +// +// SetChatRules uses context.Background internally; to specify the context, use +// SetChatRulesWithContext. func (b *Bot) SetChatRules(chatID, rules string) error { - return b.client.SetChatRules(chatID, rules) + return b.SetChatRulesWithContext(context.Background(), chatID, rules) +} + +// SetChatRulesWithContext changes chat rules +func (b *Bot) SetChatRulesWithContext(ctx context.Context, chatID, rules string) error { + return b.client.SetChatRulesWithContext(ctx, chatID, rules) } // GetFileInfo returns information about file: // id, type, size, filename, url +// +// GetFileInfo uses context.Background internally; to specify the context, use +// GetFileInfoWithContext. func (b *Bot) GetFileInfo(fileID string) (*File, error) { - return b.client.GetFileInfo(fileID) + return b.GetFileInfoWithContext(context.Background(), fileID) +} + +// GetFileInfoWithContext returns information about file: +// id, type, size, filename, url +func (b *Bot) GetFileInfoWithContext(ctx context.Context, fileID string) (*File, error) { + return b.client.GetFileInfoWithContext(ctx, fileID) } // NewMessage returns new message @@ -206,14 +326,31 @@ func (b *Bot) NewChat(id string) *Chat { // SendMessage sends a message, passed as an argument. // This method fills the argument with ID of sent message and returns an error if any. +// +// SendMessage uses context.Background internally; to specify the context, use +// SendMessageWithContext. func (b *Bot) SendMessage(message *Message) error { + return b.SendMessageWithContext(context.Background(), message) +} + +// SendMessageWithContext sends a message, passed as an argument. +// This method fills the argument with ID of sent message and returns an error if any. +func (b *Bot) SendMessageWithContext(ctx context.Context, message *Message) error { message.client = b.client - return message.Send() + return message.SendWithContext(ctx) } // EditMessage edit a message passed as an argument. +// +// EditMessage uses context.Background internally; to specify the context, use +// EditMessageWithContext. func (b *Bot) EditMessage(message *Message) error { - return b.client.EditMessage(message) + return b.EditMessageWithContext(context.Background(), message) +} + +// EditMessageWithContext edit a message passed as an argument. +func (b *Bot) EditMessageWithContext(ctx context.Context, message *Message) error { + return b.client.EditMessageWithContext(ctx, message) } // GetUpdatesChannel returns a channel, which will be filled with events. diff --git a/button.go b/button.go index 8a4c1f8..6a0a956 100644 --- a/button.go +++ b/button.go @@ -1,5 +1,7 @@ package botgolang +import "context" + //go:generate easyjson -all button.go // Button represents a button in inline keyboard @@ -72,6 +74,15 @@ type ButtonResponse struct { // Send method sends your response message. // Make sure you have QueryID in your ButtonResponse. +// +// Send uses context.Background internally; to specify the context, use +// SendWithContext. func (cl *ButtonResponse) Send() error { - return cl.client.SendAnswerCallbackQuery(cl) + return cl.SendWithContext(context.Background()) +} + +// Send method sends your response message. +// Make sure you have QueryID in your ButtonResponse. +func (cl *ButtonResponse) SendWithContext(ctx context.Context) error { + return cl.client.SendAnswerCallbackQueryWithContext(ctx, cl) } diff --git a/chat.go b/chat.go index f09ac48..5c8f076 100644 --- a/chat.go +++ b/chat.go @@ -1,5 +1,7 @@ package botgolang +import "context" + //go:generate easyjson -all chat.go type ChatAction = string @@ -70,62 +72,163 @@ func (c *Chat) resolveID() string { // You can call this method every time you change the current actions, // or every 10 seconds if the actions have not changed. After sending a // request without active action, you should not re-notify of their absence. +// +// SendActions uses context.Background internally; to specify the context, use +// SendActionsWithContext. func (c *Chat) SendActions(actions ...ChatAction) error { - return c.client.SendChatActions(c.resolveID(), actions...) + return c.SendActionsWithContext(context.Background(), actions...) +} + +// Send bot actions to the chat +// +// You can call this method every time you change the current actions, +// or every 10 seconds if the actions have not changed. After sending a +// request without active action, you should not re-notify of their absence. +func (c *Chat) SendActionsWithContext(ctx context.Context, actions ...ChatAction) error { + return c.client.SendChatActionsWithContext(ctx, c.resolveID(), actions...) } // Get chat administrators list +// +// GetAdmins uses context.Background internally; to specify the context, use +// GetAdminsWithContext. func (c *Chat) GetAdmins() ([]ChatMember, error) { - return c.client.GetChatAdmins(c.ID) + return c.GetAdminsWithContext(context.Background()) +} + +// Get chat administrators list +func (c *Chat) GetAdminsWithContext(ctx context.Context) ([]ChatMember, error) { + return c.client.GetChatAdminsWithContext(ctx, c.ID) } // Get chat members list +// +// GetMembers uses context.Background internally; to specify the context, use +// GetMembersWithContext. func (c *Chat) GetMembers() ([]ChatMember, error) { - return c.client.GetChatMembers(c.ID) + return c.GetMembersWithContext(context.Background()) +} + +// Get chat members list +func (c *Chat) GetMembersWithContext(ctx context.Context) ([]ChatMember, error) { + return c.client.GetChatMembersWithContext(ctx, c.ID) } // Get chat blocked users list +// +// GetBlockedUsers uses context.Background internally; to specify the context, use +// GetBlockedUsersWithContext. func (c *Chat) GetBlockedUsers() ([]User, error) { - return c.client.GetChatBlockedUsers(c.ID) + return c.GetBlockedUsersWithContext(context.Background()) +} + +// Get chat blocked users list +func (c *Chat) GetBlockedUsersWithContext(ctx context.Context) ([]User, error) { + return c.client.GetChatBlockedUsersWithContext(ctx, c.ID) } // Get chat join pending users list +// +// GetPendingUsers uses context.Background internally; to specify the context, use +// GetPendingUsersWithContext. func (c *Chat) GetPendingUsers() ([]User, error) { - return c.client.GetChatPendingUsers(c.ID) + return c.GetPendingUsersWithContext(context.Background()) +} + +// Get chat join pending users list +func (c *Chat) GetPendingUsersWithContext(ctx context.Context) ([]User, error) { + return c.client.GetChatPendingUsersWithContext(ctx, c.ID) } // Block user and remove him from chat. // If deleteLastMessages is true, the messages written recently will be deleted +// +// BlockUser uses context.Background internally; to specify the context, use +// BlockUserWithContext. func (c *Chat) BlockUser(userID string, deleteLastMessages bool) error { - return c.client.BlockChatUser(c.ID, userID, deleteLastMessages) + return c.BlockUserWithContext(context.Background(), userID, deleteLastMessages) +} + +// Block user and remove him from chat. +// If deleteLastMessages is true, the messages written recently will be deleted +func (c *Chat) BlockUserWithContext(ctx context.Context, userID string, deleteLastMessages bool) error { + return c.client.BlockChatUserWithContext(ctx, c.ID, userID, deleteLastMessages) } // Unblock user in chat (but not add him back) +// +// UnblockUser uses context.Background internally; to specify the context, use +// UnblockUserWithContext. func (c *Chat) UnblockUser(userID string) error { - return c.client.UnblockChatUser(c.ID, userID) + return c.UnblockUserWithContext(context.Background(), userID) +} + +// Unblock user in chat (but not add him back) +func (c *Chat) UnblockUserWithContext(ctx context.Context, userID string) error { + return c.client.UnblockChatUserWithContext(ctx, c.ID, userID) } // ResolveJoinRequest resolve specific user chat join request +// +// ResolveJoinRequest uses context.Background internally; to specify the context, use +// ResolveJoinRequestWithContext. func (c *Chat) ResolveJoinRequest(userID string, accept bool) error { - return c.client.ResolveChatPending(c.ID, userID, accept, false) + return c.ResolveJoinRequestWithContext(context.Background(), userID, accept) +} + +// ResolveJoinRequest resolve specific user chat join request +func (c *Chat) ResolveJoinRequestWithContext(ctx context.Context, userID string, accept bool) error { + return c.client.ResolveChatPendingWithContext(ctx, c.ID, userID, accept, false) } // ResolveAllJoinRequest resolve all chat join requests +// +// ResolveAllJoinRequests uses context.Background internally; to specify the context, use +// ResolveAllJoinRequestsWithContext. func (c *Chat) ResolveAllJoinRequests(accept bool) error { - return c.client.ResolveChatPending(c.ID, "", accept, true) + return c.ResolveAllJoinRequestsWithContext(context.Background(), accept) +} + +// ResolveAllJoinRequest resolve all chat join requests +func (c *Chat) ResolveAllJoinRequestsWithContext(ctx context.Context, accept bool) error { + return c.client.ResolveChatPendingWithContext(ctx, c.ID, "", accept, true) } // SetTitle changes chat title +// +// SetTitle uses context.Background internally; to specify the context, use +// SetTitleWithContext. func (c *Chat) SetTitle(title string) error { - return c.client.SetChatTitle(c.ID, title) + return c.SetTitleWithContext(context.Background(), title) +} + +// SetTitle changes chat title +func (c *Chat) SetTitleWithContext(ctx context.Context, title string) error { + return c.client.SetChatTitleWithContext(ctx, c.ID, title) } // SetAbout changes chat about +// +// SetAbout uses context.Background internally; to specify the context, use +// SetAboutWithContext. func (c *Chat) SetAbout(about string) error { - return c.client.SetChatAbout(c.ID, about) + return c.SetAboutWithContext(context.Background(), about) +} + +// SetAbout changes chat about +func (c *Chat) SetAboutWithContext(ctx context.Context, about string) error { + return c.client.SetChatAboutWithContext(ctx, c.ID, about) } // SetRules changes chat rules +// +// SetRules uses context.Background internally; to specify the context, use +// SetRulesWithContext. func (c *Chat) SetRules(rules string) error { - return c.client.SetChatRules(c.ID, rules) + return c.SetRulesWithContext(context.Background(), rules) +} + +// SetRules changes chat rules +func (c *Chat) SetRulesWithContext(ctx context.Context, rules string) error { + return c.client.SetChatRulesWithContext(ctx, c.ID, rules) } diff --git a/client.go b/client.go index 9188bcd..2f32205 100644 --- a/client.go +++ b/client.go @@ -110,7 +110,11 @@ func (c *Client) DoWithContext(ctx context.Context, path string, params url.Valu } func (c *Client) GetInfo() (*BotInfo, error) { - response, err := c.Do("/self/get", url.Values{}, nil) + return c.GetInfoWithContext(context.Background()) +} + +func (c *Client) GetInfoWithContext(ctx context.Context) (*BotInfo, error) { + response, err := c.DoWithContext(ctx, "/self/get", url.Values{}, nil) if err != nil { return nil, fmt.Errorf("error while receiving information: %s", err) } @@ -124,10 +128,14 @@ func (c *Client) GetInfo() (*BotInfo, error) { } func (c *Client) GetChatInfo(chatID string) (*Chat, error) { + return c.GetChatInfoWithContext(context.Background(), chatID) +} + +func (c *Client) GetChatInfoWithContext(ctx context.Context, chatID string) (*Chat, error) { params := url.Values{ "chatId": {chatID}, } - response, err := c.Do("/chats/getInfo", params, nil) + response, err := c.DoWithContext(ctx, "/chats/getInfo", params, nil) if err != nil { return nil, fmt.Errorf("error while receiving information: %s", err) } @@ -147,6 +155,10 @@ func (c *Client) GetChatInfo(chatID string) (*Chat, error) { } func (c *Client) SendChatActions(chatID string, actions ...ChatAction) error { + return c.SendChatActionsWithContext(context.Background(), chatID, actions...) +} + +func (c *Client) SendChatActionsWithContext(ctx context.Context, chatID string, actions ...ChatAction) error { actionsMap := make(map[ChatAction]bool) filteredActions := make([]ChatAction, 0) for _, action := range actions { @@ -159,7 +171,7 @@ func (c *Client) SendChatActions(chatID string, actions ...ChatAction) error { "chatId": {chatID}, "actions": filteredActions, } - _, err := c.Do("/chats/sendActions", params, nil) + _, err := c.DoWithContext(ctx, "/chats/sendActions", params, nil) if err != nil { return fmt.Errorf("error while receiving information: %s", err) } @@ -167,11 +179,15 @@ func (c *Client) SendChatActions(chatID string, actions ...ChatAction) error { } func (c *Client) GetChatAdmins(chatID string) ([]ChatMember, error) { + return c.GetChatAdminsWithContext(context.Background(), chatID) +} + +func (c *Client) GetChatAdminsWithContext(ctx context.Context, chatID string) ([]ChatMember, error) { params := url.Values{ "chatId": {chatID}, } - response, err := c.Do("/chats/getAdmins", params, nil) + response, err := c.DoWithContext(ctx, "/chats/getAdmins", params, nil) if err != nil { return nil, fmt.Errorf("error while receiving admins: %s", err) } @@ -184,11 +200,15 @@ func (c *Client) GetChatAdmins(chatID string) ([]ChatMember, error) { } func (c *Client) GetChatMembers(chatID string) ([]ChatMember, error) { + return c.GetChatMembersWithContext(context.Background(), chatID) +} + +func (c *Client) GetChatMembersWithContext(ctx context.Context, chatID string) ([]ChatMember, error) { params := url.Values{ "chatId": {chatID}, } - response, err := c.Do("/chats/getMembers", params, nil) + response, err := c.DoWithContext(ctx, "/chats/getMembers", params, nil) if err != nil { return nil, fmt.Errorf("error while receiving members: %s", err) } @@ -201,11 +221,15 @@ func (c *Client) GetChatMembers(chatID string) ([]ChatMember, error) { } func (c *Client) GetChatBlockedUsers(chatID string) ([]User, error) { + return c.GetChatBlockedUsersWithContext(context.Background(), chatID) +} + +func (c *Client) GetChatBlockedUsersWithContext(ctx context.Context, chatID string) ([]User, error) { params := url.Values{ "chatId": {chatID}, } - response, err := c.Do("/chats/getBlockedUsers", params, nil) + response, err := c.DoWithContext(ctx, "/chats/getBlockedUsers", params, nil) if err != nil { return nil, fmt.Errorf("error while receiving blocked users: %s", err) } @@ -218,11 +242,15 @@ func (c *Client) GetChatBlockedUsers(chatID string) ([]User, error) { } func (c *Client) GetChatPendingUsers(chatID string) ([]User, error) { + return c.GetChatPendingUsersWithContext(context.Background(), chatID) +} + +func (c *Client) GetChatPendingUsersWithContext(ctx context.Context, chatID string) ([]User, error) { params := url.Values{ "chatId": {chatID}, } - response, err := c.Do("/chats/getPendingUsers", params, nil) + response, err := c.DoWithContext(ctx, "/chats/getPendingUsers", params, nil) if err != nil { return nil, fmt.Errorf("error while receiving pending users: %s", err) } @@ -235,13 +263,17 @@ func (c *Client) GetChatPendingUsers(chatID string) ([]User, error) { } func (c *Client) BlockChatUser(chatID, userID string, deleteLastMessages bool) error { + return c.BlockChatUserWithContext(context.Background(), chatID, userID, deleteLastMessages) +} + +func (c *Client) BlockChatUserWithContext(ctx context.Context, chatID, userID string, deleteLastMessages bool) error { params := url.Values{ "chatId": {chatID}, "userId": {userID}, "delLastMessages": {strconv.FormatBool(deleteLastMessages)}, } - response, err := c.Do("/chats/blockUser", params, nil) + response, err := c.DoWithContext(ctx, "/chats/blockUser", params, nil) if err != nil { return fmt.Errorf("error while blocking user: %s", err) } @@ -254,12 +286,16 @@ func (c *Client) BlockChatUser(chatID, userID string, deleteLastMessages bool) e } func (c *Client) UnblockChatUser(chatID, userID string) error { + return c.UnblockChatUserWithContext(context.Background(), chatID, userID) +} + +func (c *Client) UnblockChatUserWithContext(ctx context.Context, chatID, userID string) error { params := url.Values{ "chatId": {chatID}, "userId": {userID}, } - response, err := c.Do("/chats/unblockUser", params, nil) + response, err := c.DoWithContext(ctx, "/chats/unblockUser", params, nil) if err != nil { return fmt.Errorf("error while unblocking user: %s", err) } @@ -272,6 +308,10 @@ func (c *Client) UnblockChatUser(chatID, userID string) error { } func (c *Client) ResolveChatPending(chatID, userID string, approve, everyone bool) error { + return c.ResolveChatPendingWithContext(context.Background(), chatID, userID, approve, everyone) +} + +func (c *Client) ResolveChatPendingWithContext(ctx context.Context, chatID, userID string, approve, everyone bool) error { params := url.Values{ "chatId": {chatID}, "approve": {strconv.FormatBool(approve)}, @@ -282,53 +322,69 @@ func (c *Client) ResolveChatPending(chatID, userID string, approve, everyone boo params.Set("userId", userID) } - if _, err := c.Do("/chats/resolvePending", params, nil); err != nil { + if _, err := c.DoWithContext(ctx, "/chats/resolvePending", params, nil); err != nil { return fmt.Errorf("error while resolving chat pendings: %s", err) } return nil } func (c *Client) SetChatTitle(chatID, title string) error { + return c.SetChatTitleWithContext(context.Background(), chatID, title) +} + +func (c *Client) SetChatTitleWithContext(ctx context.Context, chatID, title string) error { params := url.Values{ "chatId": {chatID}, "title": {title}, } - if _, err := c.Do("/chats/setTitle", params, nil); err != nil { + if _, err := c.DoWithContext(ctx, "/chats/setTitle", params, nil); err != nil { return fmt.Errorf("error while setting chat title: %s", err) } return nil } func (c *Client) SetChatAbout(chatID, about string) error { + return c.SetChatAboutWithContext(context.Background(), chatID, about) +} + +func (c *Client) SetChatAboutWithContext(ctx context.Context, chatID, about string) error { params := url.Values{ "chatId": {chatID}, "about": {about}, } - if _, err := c.Do("/chats/setAbout", params, nil); err != nil { + if _, err := c.DoWithContext(ctx, "/chats/setAbout", params, nil); err != nil { return fmt.Errorf("error while setting chat about: %s", err) } return nil } func (c *Client) SetChatRules(chatID, rules string) error { + return c.SetChatRulesWithContext(context.Background(), chatID, rules) +} + +func (c *Client) SetChatRulesWithContext(ctx context.Context, chatID, rules string) error { params := url.Values{ "chatId": {chatID}, "rules": {rules}, } - if _, err := c.Do("/chats/setRules", params, nil); err != nil { + if _, err := c.DoWithContext(ctx, "/chats/setRules", params, nil); err != nil { return fmt.Errorf("error while setting chat rules: %s", err) } return nil } func (c *Client) GetFileInfo(fileID string) (*File, error) { + return c.GetFileInfoWithContext(context.Background(), fileID) +} + +func (c *Client) GetFileInfoWithContext(ctx context.Context, fileID string) (*File, error) { params := url.Values{ "fileId": {fileID}, } - response, err := c.Do("/files/getInfo", params, nil) + response, err := c.DoWithContext(ctx, "/files/getInfo", params, nil) if err != nil { return nil, fmt.Errorf("error while receiving information: %s", err) } @@ -342,10 +398,18 @@ func (c *Client) GetFileInfo(fileID string) (*File, error) { } func (c *Client) GetVoiceInfo(fileID string) (*File, error) { - return c.GetFileInfo(fileID) + return c.GetVoiceInfoWithContext(context.Background(), fileID) +} + +func (c *Client) GetVoiceInfoWithContext(ctx context.Context, fileID string) (*File, error) { + return c.GetFileInfoWithContext(ctx, fileID) } func (c *Client) SendTextMessage(message *Message) error { + return c.SendTextMessageWithContext(context.Background(), message) +} + +func (c *Client) SendTextMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "text": {message.Text}, @@ -373,7 +437,7 @@ func (c *Client) SendTextMessage(message *Message) error { params.Set("parseMode", string(message.ParseMode)) } - response, err := c.Do("/messages/sendText", params, nil) + response, err := c.DoWithContext(ctx, "/messages/sendText", params, nil) if err != nil { return fmt.Errorf("error while sending text: %s", err) } @@ -386,6 +450,10 @@ func (c *Client) SendTextMessage(message *Message) error { } func (c *Client) EditMessage(message *Message) error { + return c.EditMessageWithContext(context.Background(), message) +} + +func (c *Client) EditMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "msgId": {message.ID}, "chatId": {message.Chat.ID}, @@ -405,7 +473,7 @@ func (c *Client) EditMessage(message *Message) error { params.Set("parseMode", string(message.ParseMode)) } - response, err := c.Do("/messages/editText", params, nil) + response, err := c.DoWithContext(ctx, "/messages/editText", params, nil) if err != nil { return fmt.Errorf("error while editing text: %s", err) } @@ -418,11 +486,15 @@ func (c *Client) EditMessage(message *Message) error { } func (c *Client) DeleteMessage(message *Message) error { + return c.DeleteMessageWithContext(context.Background(), message) +} + +func (c *Client) DeleteMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "msgId": {message.ID}, "chatId": {message.Chat.ID}, } - _, err := c.Do("/messages/deleteMessages", params, nil) + _, err := c.DoWithContext(ctx, "/messages/deleteMessages", params, nil) if err != nil { return fmt.Errorf("error while deleting message: %s", err) } @@ -431,6 +503,10 @@ func (c *Client) DeleteMessage(message *Message) error { } func (c *Client) SendFileMessage(message *Message) error { + return c.SendFileMessageWithContext(context.Background(), message) +} + +func (c *Client) SendFileMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "caption": {message.Text}, @@ -459,7 +535,7 @@ func (c *Client) SendFileMessage(message *Message) error { params.Set("parseMode", string(message.ParseMode)) } - response, err := c.Do("/messages/sendFile", params, nil) + response, err := c.DoWithContext(ctx, "/messages/sendFile", params, nil) if err != nil { return fmt.Errorf("error while making request: %s", err) } @@ -472,6 +548,10 @@ func (c *Client) SendFileMessage(message *Message) error { } func (c *Client) SendVoiceMessage(message *Message) error { + return c.SendVoiceMessageWithContext(context.Background(), message) +} + +func (c *Client) SendVoiceMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "caption": {message.Text}, @@ -496,7 +576,7 @@ func (c *Client) SendVoiceMessage(message *Message) error { params.Set("inlineKeyboardMarkup", string(data)) } - response, err := c.Do("/messages/sendVoice", params, nil) + response, err := c.DoWithContext(ctx, "/messages/sendVoice", params, nil) if err != nil { return fmt.Errorf("error while making request: %s", err) } @@ -509,6 +589,10 @@ func (c *Client) SendVoiceMessage(message *Message) error { } func (c *Client) UploadFile(message *Message) error { + return c.UploadFileWithContext(context.Background(), message) +} + +func (c *Client) UploadFileWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "caption": {message.Text}, @@ -523,7 +607,7 @@ func (c *Client) UploadFile(message *Message) error { params.Set("inlineKeyboardMarkup", string(data)) } - response, err := c.Do("/messages/sendFile", params, message.File) + response, err := c.DoWithContext(ctx, "/messages/sendFile", params, message.File) if err != nil { return fmt.Errorf("error while making request: %s", err) } @@ -536,6 +620,10 @@ func (c *Client) UploadFile(message *Message) error { } func (c *Client) UploadVoice(message *Message) error { + return c.UploadVoiceWithContext(context.Background(), message) +} + +func (c *Client) UploadVoiceWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "caption": {message.Text}, @@ -550,7 +638,7 @@ func (c *Client) UploadVoice(message *Message) error { params.Set("inlineKeyboardMarkup", string(data)) } - response, err := c.Do("/messages/sendVoice", params, message.File) + response, err := c.DoWithContext(ctx, "/messages/sendVoice", params, message.File) if err != nil { return fmt.Errorf("error while making request: %s", err) } @@ -586,11 +674,15 @@ func (c *Client) GetEventsWithContext(ctx context.Context, lastEventID int, poll } func (c *Client) PinMessage(message *Message) error { + return c.PinMessageWithContext(context.Background(), message) +} + +func (c *Client) PinMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "msgId": {message.ID}, } - _, err := c.Do("/chats/pinMessage", params, nil) + _, err := c.DoWithContext(ctx, "/chats/pinMessage", params, nil) if err != nil { return fmt.Errorf("error while pinning message: %s", err) } @@ -599,11 +691,15 @@ func (c *Client) PinMessage(message *Message) error { } func (c *Client) UnpinMessage(message *Message) error { + return c.UnpinMessageWithContext(context.Background(), message) +} + +func (c *Client) UnpinMessageWithContext(ctx context.Context, message *Message) error { params := url.Values{ "chatId": {message.Chat.ID}, "msgId": {message.ID}, } - _, err := c.Do("/chats/unpinMessage", params, nil) + _, err := c.DoWithContext(ctx, "/chats/unpinMessage", params, nil) if err != nil { return fmt.Errorf("error while unpinning message: %s", err) } @@ -612,6 +708,10 @@ func (c *Client) UnpinMessage(message *Message) error { } func (c *Client) SendAnswerCallbackQuery(answer *ButtonResponse) error { + return c.SendAnswerCallbackQueryWithContext(context.Background(), answer) +} + +func (c *Client) SendAnswerCallbackQueryWithContext(ctx context.Context, answer *ButtonResponse) error { params := url.Values{ "queryId": {answer.QueryID}, "text": {answer.Text}, @@ -619,7 +719,7 @@ func (c *Client) SendAnswerCallbackQuery(answer *ButtonResponse) error { "showAlert": {strconv.FormatBool(answer.ShowAlert)}, } - _, err := c.Do("/messages/answerCallbackQuery", params, nil) + _, err := c.DoWithContext(ctx, "/messages/answerCallbackQuery", params, nil) if err != nil { return fmt.Errorf("error while making request: %s", err) } diff --git a/message.go b/message.go index ad3e788..e7e736e 100644 --- a/message.go +++ b/message.go @@ -1,6 +1,7 @@ package botgolang import ( + "context" "fmt" "os" "path/filepath" @@ -101,7 +102,16 @@ func (m *Message) AttachInlineKeyboard(keyboard Keyboard) { // Send method sends your message. // Make sure you have Text or FileID in your message. +// +// Send uses context.Background internally; to specify the context, use +// SendWithContext. func (m *Message) Send() error { + return m.SendWithContext(context.Background()) +} + +// SendWithContext method sends your message. +// Make sure you have Text or FileID in your message. +func (m *Message) SendWithContext(ctx context.Context) error { if m.client == nil { return fmt.Errorf("client is not inited, create message with constructor NewMessage, NewTextMessage, etc") } @@ -113,41 +123,41 @@ func (m *Message) Send() error { switch m.ContentType { case Voice: if m.FileID != "" { - return m.client.SendVoiceMessage(m) + return m.client.SendVoiceMessageWithContext(ctx, m) } if m.File != nil { - return m.client.UploadVoice(m) + return m.client.UploadVoiceWithContext(ctx, m) } case OtherFile: if m.FileID != "" { - return m.client.SendFileMessage(m) + return m.client.SendFileMessageWithContext(ctx, m) } if m.File != nil { - return m.client.UploadFile(m) + return m.client.UploadFileWithContext(ctx, m) } case Text: - return m.client.SendTextMessage(m) + return m.client.SendTextMessageWithContext(ctx, m) case Unknown: // need to autodetect if m.FileID != "" { // voice message's fileID always starts with 'I' if m.FileID[0] == voiceMessageLeadingRune { - return m.client.SendVoiceMessage(m) + return m.client.SendVoiceMessageWithContext(ctx, m) } - return m.client.SendFileMessage(m) + return m.client.SendFileMessageWithContext(ctx, m) } if m.File != nil { if voiceMessageSupportedExtensions[filepath.Ext(m.File.Name())] { - return m.client.UploadVoice(m) + return m.client.UploadVoiceWithContext(ctx, m) } - return m.client.UploadFile(m) + return m.client.UploadFileWithContext(ctx, m) } if m.Text != "" { - return m.client.SendTextMessage(m) + return m.client.SendTextMessageWithContext(ctx, m) } } @@ -156,26 +166,53 @@ func (m *Message) Send() error { // Edit method edits your message. // Make sure you have ID in your message. +// +// Edit uses context.Background internally; to specify the context, use +// EditWithContext. func (m *Message) Edit() error { + return m.EditWithContext(context.Background()) +} + +// EditWithContext method edits your message. +// Make sure you have ID in your message. +func (m *Message) EditWithContext(ctx context.Context) error { if m.ID == "" { return fmt.Errorf("cannot edit message without id") } - return m.client.EditMessage(m) + return m.client.EditMessageWithContext(ctx, m) } // Delete method deletes your message. // Make sure you have ID in your message. +// +// Delete uses context.Background internally; to specify the context, use +// DeleteWithContext. func (m *Message) Delete() error { + return m.DeleteWithContext(context.Background()) +} + +// DeleteWithContext method deletes your message. +// Make sure you have ID in your message. +func (m *Message) DeleteWithContext(ctx context.Context) error { if m.ID == "" { return fmt.Errorf("cannot delete message without id") } - return m.client.DeleteMessage(m) + return m.client.DeleteMessageWithContext(ctx, m) } // Reply method replies to the message. // Make sure you have ID in the message. +// +// Reply uses context.Background internally; to specify the context, use +// ReplyWithContext. func (m *Message) Reply(text string) error { + return m.ReplyWithContext(context.Background(), text) +} + +// ReplyWithContext method replies to the message. +// Make sure you have ID in the message. +func (m *Message) ReplyWithContext(ctx context.Context, text string) error { if m.ID == "" { return fmt.Errorf("cannot reply to message without id") } @@ -183,12 +220,21 @@ func (m *Message) Reply(text string) error { m.ReplyMsgID = m.ID m.Text = text - return m.client.SendTextMessage(m) + return m.client.SendTextMessageWithContext(ctx, m) } // Forward method forwards your message to chat. // Make sure you have ID in your message. +// +// Forward uses context.Background internally; to specify the context, use +// ForwardWithContext. func (m *Message) Forward(chatID string) error { + return m.ForwardWithContext(context.Background(), chatID) +} + +// ForwardWithContext method forwards your message to chat. +// Make sure you have ID in your message. +func (m *Message) ForwardWithContext(ctx context.Context, chatID string) error { if m.ID == "" { return fmt.Errorf("cannot forward message without id") } @@ -197,25 +243,43 @@ func (m *Message) Forward(chatID string) error { m.ForwardMsgID = m.ID m.Chat.ID = chatID - return m.client.SendTextMessage(m) + return m.client.SendTextMessageWithContext(ctx, m) } // Pin message in chat // Make sure you are admin in this chat +// +// Pin uses context.Background internally; to specify the context, use +// PinWithContext. func (m *Message) Pin() error { + return m.PinWithContext(context.Background()) +} + +// PinWithContext message in chat +// Make sure you are admin in this chat +func (m *Message) PinWithContext(ctx context.Context) error { if m.ID == "" { return fmt.Errorf("cannot pin message without id") } - return m.client.PinMessage(m) + return m.client.PinMessageWithContext(ctx, m) } // Unpin message in chat // Make sure you are admin in this chat +// +// Unpin uses context.Background internally; to specify the context, use +// UnpinWithContext. func (m *Message) Unpin() error { + return m.UnpinWithContext(context.Background()) +} + +// Unpin message in chat +// Make sure you are admin in this chat +func (m *Message) UnpinWithContext(ctx context.Context) error { if m.ID == "" { return fmt.Errorf("cannot unpin message without id") } - return m.client.UnpinMessage(m) + return m.client.UnpinMessageWithContext(ctx, m) }