Skip to content

Commit ff26b57

Browse files
committed
add WithContext functions
1 parent 949689d commit ff26b57

File tree

5 files changed

+484
-69
lines changed

5 files changed

+484
-69
lines changed

bot.go

Lines changed: 153 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,80 +30,200 @@ type Bot struct {
3030

3131
// GetInfo returns information about bot:
3232
// id, name, about, avatar
33+
//
34+
// GetInfo uses context.Background internally; to specify the context, use
35+
// GetInfoWithContext.
3336
func (b *Bot) GetInfo() (*BotInfo, error) {
34-
return b.client.GetInfo()
37+
return b.GetInfoWithContext(context.Background())
38+
}
39+
40+
// GetInfoWithContext returns information about bot:
41+
// id, name, about, avatar
42+
func (b *Bot) GetInfoWithContext(ctx context.Context) (*BotInfo, error) {
43+
return b.client.GetInfoWithContext(ctx)
3544
}
3645

3746
// GetChatInfo returns information about chat:
3847
// id, type, title, public, group, inviteLink, admins
48+
//
49+
// GetChatInfo uses context.Background internally; to specify the context, use
50+
// GetChatInfoWithContext.
3951
func (b *Bot) GetChatInfo(chatID string) (*Chat, error) {
40-
return b.client.GetChatInfo(chatID)
52+
return b.GetChatInfoWithContext(context.Background(), chatID)
53+
}
54+
55+
// GetChatInfoWithContext returns information about chat:
56+
// id, type, title, public, group, inviteLink, admins
57+
func (b *Bot) GetChatInfoWithContext(ctx context.Context, chatID string) (*Chat, error) {
58+
return b.client.GetChatInfoWithContext(ctx, chatID)
4159
}
4260

4361
// SendChatActions sends an actions like "typing, looking"
62+
//
63+
// SendChatActions uses context.Background internally; to specify the context, use
64+
// SendChatActionsWithContext.
4465
func (b *Bot) SendChatActions(chatID string, actions ...ChatAction) error {
45-
return b.client.SendChatActions(chatID, actions...)
66+
return b.SendChatActionsWithContext(context.Background(), chatID, actions...)
67+
}
68+
69+
// SendChatActionsWithContext sends an actions like "typing, looking"
70+
func (b *Bot) SendChatActionsWithContext(ctx context.Context, chatID string, actions ...ChatAction) error {
71+
return b.client.SendChatActionsWithContext(ctx, chatID, actions...)
4672
}
4773

4874
// GetChatAdmins returns chat admins list with fields:
4975
// userID, creator flag
76+
//
77+
// GetChatAdmins uses context.Background internally; to specify the context, use
78+
// GetChatAdminsWithContext.
5079
func (b *Bot) GetChatAdmins(chatID string) ([]ChatMember, error) {
51-
return b.client.GetChatAdmins(chatID)
80+
return b.GetChatAdminsWithContext(context.Background(), chatID)
81+
}
82+
83+
// GetChatAdminsWithContext returns chat admins list with fields:
84+
// userID, creator flag
85+
func (b *Bot) GetChatAdminsWithContext(ctx context.Context, chatID string) ([]ChatMember, error) {
86+
return b.client.GetChatAdminsWithContext(ctx, chatID)
5287
}
5388

5489
// GetChatMem returns chat members list with fields:
5590
// userID, creator flag, admin flag
91+
//
92+
// GetChatMembers uses context.Background internally; to specify the context, use
93+
// GetChatMembersWithContext.
5694
func (b *Bot) GetChatMembers(chatID string) ([]ChatMember, error) {
57-
return b.client.GetChatMembers(chatID)
95+
return b.GetChatMembersWithContext(context.Background(), chatID)
96+
}
97+
98+
// GetChatMembersWithContext returns chat members list with fields:
99+
// userID, creator flag, admin flag
100+
func (b *Bot) GetChatMembersWithContext(ctx context.Context, chatID string) ([]ChatMember, error) {
101+
return b.client.GetChatMembersWithContext(ctx, chatID)
58102
}
59103

60104
// GetChatBlockedUsers returns chat blocked users list:
61105
// userID
106+
//
107+
// GetChatBlockedUsers uses context.Background internally; to specify the context, use
108+
// GetChatBlockedUsersWithContext.
62109
func (b *Bot) GetChatBlockedUsers(chatID string) ([]User, error) {
63-
return b.client.GetChatBlockedUsers(chatID)
110+
return b.GetChatBlockedUsersWithContext(context.Background(), chatID)
111+
}
112+
113+
// GetChatBlockedUsersWithContext returns chat blocked users list:
114+
// userID
115+
func (b *Bot) GetChatBlockedUsersWithContext(ctx context.Context, chatID string) ([]User, error) {
116+
return b.client.GetChatBlockedUsersWithContext(ctx, chatID)
64117
}
65118

66119
// GetChatPendingUsers returns chat join pending users list:
67120
// userID
121+
//
122+
// GetChatPendingUsers uses context.Background internally; to specify the context, use
123+
// GetChatPendingUsersWithContext.
68124
func (b *Bot) GetChatPendingUsers(chatID string) ([]User, error) {
69-
return b.client.GetChatPendingUsers(chatID)
125+
return b.GetChatPendingUsersWithContext(context.Background(), chatID)
126+
}
127+
128+
// GetChatPendingUsersWithContext returns chat join pending users list:
129+
// userID
130+
func (b *Bot) GetChatPendingUsersWithContext(ctx context.Context, chatID string) ([]User, error) {
131+
return b.client.GetChatPendingUsersWithContext(ctx, chatID)
70132
}
71133

72134
// BlockChatUser blocks user and removes him from chat.
73135
// If deleteLastMessages is true, the messages written recently will be deleted
136+
//
137+
// BlockChatUser uses context.Background internally; to specify the context, use
138+
// BlockChatUserWithContext.
74139
func (b *Bot) BlockChatUser(chatID, userID string, deleteLastMessages bool) error {
75-
return b.client.BlockChatUser(chatID, userID, deleteLastMessages)
140+
return b.BlockChatUserWithContext(context.Background(), chatID, userID, deleteLastMessages)
141+
}
142+
143+
// BlockChatUserWithContext blocks user and removes him from chat.
144+
// If deleteLastMessages is true, the messages written recently will be deleted
145+
func (b *Bot) BlockChatUserWithContext(ctx context.Context, chatID, userID string, deleteLastMessages bool) error {
146+
return b.client.BlockChatUserWithContext(ctx, chatID, userID, deleteLastMessages)
76147
}
77148

78149
// UnblockChatUser unblocks user in chat
150+
//
151+
// UnblockChatUser uses context.Background internally; to specify the context, use
152+
// UnblockChatUserWithContext.
79153
func (b *Bot) UnblockChatUser(chatID, userID string) error {
80-
return b.client.UnblockChatUser(chatID, userID)
154+
return b.UnblockChatUserWithContext(context.Background(), chatID, userID)
155+
}
156+
157+
// UnblockChatUserWithContext unblocks user in chat
158+
func (b *Bot) UnblockChatUserWithContext(ctx context.Context, chatID, userID string) error {
159+
return b.client.UnblockChatUserWithContext(ctx, chatID, userID)
81160
}
82161

83162
// ResolveChatJoinRequests sends a decision to accept/decline user join to chat
163+
//
164+
// ResolveChatJoinRequests uses context.Background internally; to specify the context, use
165+
// ResolveChatJoinRequestsWithContext.
84166
func (b *Bot) ResolveChatJoinRequests(chatID, userID string, accept, everyone bool) error {
85-
return b.client.ResolveChatPending(chatID, userID, accept, everyone)
167+
return b.ResolveChatJoinRequestsWithContext(context.Background(), chatID, userID, accept, everyone)
168+
}
169+
170+
// ResolveChatJoinRequestsWithContext sends a decision to accept/decline user join to chat
171+
func (b *Bot) ResolveChatJoinRequestsWithContext(ctx context.Context, chatID, userID string, accept, everyone bool) error {
172+
return b.client.ResolveChatPendingWithContext(ctx, chatID, userID, accept, everyone)
86173
}
87174

88175
// SetChatTitle changes chat title
176+
//
177+
// SetChatTitle uses context.Background internally; to specify the context, use
178+
// SetChatTitleWithContext.
89179
func (b *Bot) SetChatTitle(chatID, title string) error {
90-
return b.client.SetChatTitle(chatID, title)
180+
return b.SetChatTitleWithContext(context.Background(), chatID, title)
181+
}
182+
183+
// SetChatTitleWithContext changes chat title
184+
func (b *Bot) SetChatTitleWithContext(ctx context.Context, chatID, title string) error {
185+
return b.client.SetChatTitleWithContext(ctx, chatID, title)
91186
}
92187

93188
// SetChatAbout changes chat about
189+
//
190+
// SetChatAbout uses context.Background internally; to specify the context, use
191+
// SetChatAboutWithContext.
94192
func (b *Bot) SetChatAbout(chatID, about string) error {
95-
return b.client.SetChatAbout(chatID, about)
193+
return b.SetChatAboutWithContext(context.Background(), chatID, about)
194+
}
195+
196+
// SetChatAboutWithContext changes chat about
197+
func (b *Bot) SetChatAboutWithContext(ctx context.Context, chatID, about string) error {
198+
return b.client.SetChatAboutWithContext(ctx, chatID, about)
96199
}
97200

98201
// SetChatRules changes chat rules
202+
//
203+
// SetChatRules uses context.Background internally; to specify the context, use
204+
// SetChatRulesWithContext.
99205
func (b *Bot) SetChatRules(chatID, rules string) error {
100-
return b.client.SetChatRules(chatID, rules)
206+
return b.SetChatRulesWithContext(context.Background(), chatID, rules)
207+
}
208+
209+
// SetChatRulesWithContext changes chat rules
210+
func (b *Bot) SetChatRulesWithContext(ctx context.Context, chatID, rules string) error {
211+
return b.client.SetChatRulesWithContext(ctx, chatID, rules)
101212
}
102213

103214
// GetFileInfo returns information about file:
104215
// id, type, size, filename, url
216+
//
217+
// GetFileInfo uses context.Background internally; to specify the context, use
218+
// GetFileInfoWithContext.
105219
func (b *Bot) GetFileInfo(fileID string) (*File, error) {
106-
return b.client.GetFileInfo(fileID)
220+
return b.GetFileInfoWithContext(context.Background(), fileID)
221+
}
222+
223+
// GetFileInfoWithContext returns information about file:
224+
// id, type, size, filename, url
225+
func (b *Bot) GetFileInfoWithContext(ctx context.Context, fileID string) (*File, error) {
226+
return b.client.GetFileInfoWithContext(ctx, fileID)
107227
}
108228

109229
// NewMessage returns new message
@@ -206,14 +326,31 @@ func (b *Bot) NewChat(id string) *Chat {
206326

207327
// SendMessage sends a message, passed as an argument.
208328
// This method fills the argument with ID of sent message and returns an error if any.
329+
//
330+
// SendMessage uses context.Background internally; to specify the context, use
331+
// SendMessageWithContext.
209332
func (b *Bot) SendMessage(message *Message) error {
333+
return b.SendMessageWithContext(context.Background(), message)
334+
}
335+
336+
// SendMessageWithContext sends a message, passed as an argument.
337+
// This method fills the argument with ID of sent message and returns an error if any.
338+
func (b *Bot) SendMessageWithContext(ctx context.Context, message *Message) error {
210339
message.client = b.client
211-
return message.Send()
340+
return message.SendWithContext(ctx)
212341
}
213342

214343
// EditMessage edit a message passed as an argument.
344+
//
345+
// EditMessage uses context.Background internally; to specify the context, use
346+
// EditMessageWithContext.
215347
func (b *Bot) EditMessage(message *Message) error {
216-
return b.client.EditMessage(message)
348+
return b.EditMessageWithContext(context.Background(), message)
349+
}
350+
351+
// EditMessageWithContext edit a message passed as an argument.
352+
func (b *Bot) EditMessageWithContext(ctx context.Context, message *Message) error {
353+
return b.client.EditMessageWithContext(ctx, message)
217354
}
218355

219356
// GetUpdatesChannel returns a channel, which will be filled with events.

button.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package botgolang
22

3+
import "context"
4+
35
//go:generate easyjson -all button.go
46

57
// Button represents a button in inline keyboard
@@ -72,6 +74,15 @@ type ButtonResponse struct {
7274

7375
// Send method sends your response message.
7476
// Make sure you have QueryID in your ButtonResponse.
77+
//
78+
// Send uses context.Background internally; to specify the context, use
79+
// SendWithContext.
7580
func (cl *ButtonResponse) Send() error {
76-
return cl.client.SendAnswerCallbackQuery(cl)
81+
return cl.SendWithContext(context.Background())
82+
}
83+
84+
// Send method sends your response message.
85+
// Make sure you have QueryID in your ButtonResponse.
86+
func (cl *ButtonResponse) SendWithContext(ctx context.Context) error {
87+
return cl.client.SendAnswerCallbackQueryWithContext(ctx, cl)
7788
}

0 commit comments

Comments
 (0)