forked from PaulSonOfLars/gotgbot
/
chat_funcs.go
304 lines (251 loc) · 7.45 KB
/
chat_funcs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package ext
import (
"encoding/json"
"io"
"net/url"
"strconv"
"github.com/pkg/errors"
)
func (b Bot) KickChatMember(chatId int, userId int) (bool, error) {
kick := b.NewSendableKickChatMember(chatId, userId)
return kick.Send()
}
func (b Bot) KickChatMemberUntil(chatId int, userId int, untilDate int64) (bool, error) {
kick := b.NewSendableKickChatMember(chatId, userId)
kick.UntilDate = untilDate
return kick.Send()
}
func (b Bot) UnbanChatMember(chatId int, userId int) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
v.Add("user_id", strconv.Itoa(userId))
r, err := Get(b, "unbanChatMember", v)
if err != nil {
return false, errors.Wrapf(err, "could not unbanChatMember")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) RestrictChatMember(chatId int, userId int) (bool, error) {
restrict := b.NewSendableRestrictChatMember(chatId, userId)
return restrict.Send()
}
func (b Bot) UnRestrictChatMember(chatId int, userId int) (bool, error) {
unRestrict := b.NewSendableRestrictChatMember(chatId, userId)
temp := true
unRestrict.Permissions.CanSendMessages = &temp
unRestrict.Permissions.CanSendMediaMessages = &temp
unRestrict.Permissions.CanSendPolls = &temp
unRestrict.Permissions.CanSendOtherMessages = &temp
unRestrict.Permissions.CanAddWebPagePreviews = &temp
unRestrict.Permissions.CanInviteUsers = &temp
return unRestrict.Send()
}
func (b Bot) PromoteChatMember(chatId int, userId int) (bool, error) {
promote := b.NewSendablePromoteChatMember(chatId, userId)
return promote.Send()
}
func (b Bot) DemoteChatMember(chatId int, userId int) (bool, error) {
demote := b.NewSendablePromoteChatMember(chatId, userId)
demote.CanChangeInfo = false
demote.CanPostMessages = false
demote.CanEditMessages = false
demote.CanDeleteMessages = false
demote.CanInviteUsers = false
demote.CanRestrictMembers = false
demote.CanPinMessages = false
demote.CanPromoteMembers = false
return demote.Send()
}
func (b Bot) SetChatPermissions(chatId int, perms ChatPermissions) (bool, error) {
setChatPerms := b.NewSendableSetChatPermissions(chatId, perms)
return setChatPerms.Send()
}
func (b Bot) ExportChatInviteLink(chatId int) (string, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "exportChatInviteLink", v)
if err != nil {
return "", errors.Wrapf(err, "unable to exportChatInviteLink")
}
if !r.Ok {
return "", errors.New(r.Description)
}
var s string
return s, json.Unmarshal(r.Result, &s)
}
func (b Bot) SetChatPhotoStr(chatId int, photoId string) (bool, error) {
setChatPhoto := b.NewSendableSetChatPhoto(chatId)
setChatPhoto.FileId = photoId
return setChatPhoto.Send()
}
func (b Bot) SetChatPhotoPath(chatId int, path string) (bool, error) {
setChatPhoto := b.NewSendableSetChatPhoto(chatId)
setChatPhoto.Path = path
return setChatPhoto.Send()
}
func (b Bot) SetChatPhotoReader(chatId int, reader io.Reader) (bool, error) {
setChatPhoto := b.NewSendableSetChatPhoto(chatId)
setChatPhoto.Reader = reader
return setChatPhoto.Send()
}
func (b Bot) DeleteChatPhoto(chatId int) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "deleteChatPhoto", v)
if err != nil {
return false, errors.Wrapf(err, "unable to deleteChatPhoto")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) SetChatTitle(chatId int, title string) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
v.Add("title", title)
r, err := Get(b, "setChatTitle", v)
if err != nil {
return false, errors.Wrapf(err, "unable to setChatTitle")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) SetChatDescription(chatId int, description string) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
v.Add("description", description)
r, err := Get(b, "setChatDescription", v)
if err != nil {
return false, errors.Wrapf(err, "unable to setChatDescription")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) PinChatMessage(chatId int, messageId int) (bool, error) {
pin := b.NewSendablePinChatMessage(chatId, messageId)
return pin.Send()
}
func (b Bot) PinChatMessageQuiet(chatId int, messageId int) (bool, error) {
pin := b.NewSendablePinChatMessage(chatId, messageId)
pin.DisableNotification = true
return pin.Send()
}
func (b Bot) UnpinChatMessage(chatId int) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "unpinChatMessage", v)
if err != nil {
return false, errors.Wrapf(err, "unable to unpinChatMessage")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) LeaveChat(chatId int) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "leaveChat", v)
if err != nil {
return false, errors.Wrapf(err, "unable to leaveChat")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) GetChat(chatId int) (*Chat, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "getChat", v)
if err != nil {
return nil, errors.Wrapf(err, "unable to getChat")
}
if !r.Ok {
return nil, errors.New(r.Description)
}
var c Chat
c.Bot = b
return &c, json.Unmarshal(r.Result, &c)
}
func (b Bot) GetChatAdministrators(chatId int) ([]ChatMember, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "getChatAdministrators", v)
if err != nil {
return nil, errors.Wrapf(err, "unable to getChatAdministrators")
}
if !r.Ok {
return nil, errors.New(r.Description)
}
var cm []ChatMember
return cm, json.Unmarshal(r.Result, &cm)
}
func (b Bot) GetChatMembersCount(chatId int) (int, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "getChatMembersCount", v)
if err != nil {
return 0, errors.Wrapf(err, "unable to getChatMembersCount")
}
if !r.Ok {
return 0, errors.New(r.Description)
}
var c int
return c, json.Unmarshal(r.Result, &c)
}
func (b Bot) GetChatMember(chatId int, userId int) (*ChatMember, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
v.Add("user_id", strconv.Itoa(userId))
r, err := Get(b, "getChatMember", v)
if err != nil {
return nil, errors.Wrapf(err, "unable to getChatMember")
}
if !r.Ok {
return nil, errors.New(r.Description)
}
var cm ChatMember
return &cm, json.Unmarshal(r.Result, &cm)
}
func (b Bot) SetChatStickerSet(chatId int, stickerSetName string) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
v.Add("sticker_set_name", stickerSetName)
r, err := Get(b, "setChatStickerSet", v)
if err != nil {
return false, errors.Wrapf(err, "unable to setChatStickerSet")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
func (b Bot) DeleteChatStickerSet(chatId int) (bool, error) {
v := url.Values{}
v.Add("chat_id", strconv.Itoa(chatId))
r, err := Get(b, "deleteChatStickerSet", v)
if err != nil {
return false, errors.Wrapf(err, "unable to deleteChatStickerSet")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}