-
Notifications
You must be signed in to change notification settings - Fork 469
/
sticker.go
331 lines (277 loc) · 8.45 KB
/
sticker.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package telebot
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
type (
StickerSetType = string
StickerSetFormat = string
MaskFeature = string
)
const (
StickerRegular StickerSetType = "regular"
StickerMask StickerSetType = "mask"
StickerCustomEmoji StickerSetType = "custom_emoji"
)
const (
StickerStatic StickerSetFormat = "static"
StickerAnimated StickerSetFormat = "animated"
StickerVideo StickerSetFormat = "video"
)
const (
MaskForehead MaskFeature = "forehead"
MaskEyes MaskFeature = "eyes"
MaskMouth MaskFeature = "mouth"
MaskChin MaskFeature = "chin"
)
// StickerSet represents a sticker set.
type StickerSet struct {
Type StickerSetType `json:"sticker_type"`
Format StickerSetFormat `json:"sticker_format"`
Name string `json:"name"`
Title string `json:"title"`
Stickers []Sticker `json:"stickers"`
Thumbnail *Photo `json:"thumbnail"`
Emojis string `json:"emojis"`
ContainsMasks bool `json:"contains_masks"` // FIXME: can be removed
MaskPosition *MaskPosition `json:"mask_position"`
Repaint bool `json:"needs_repainting"`
// Input is a field used in createNewStickerSet method to specify a list
// of pre-defined stickers of type InputSticker to add to the set.
Input []InputSticker
}
type InputSticker struct {
File
Sticker string `json:"sticker"`
Format string `json:"format"`
MaskPosition *MaskPosition `json:"mask_position"`
Emojis []string `json:"emoji_list"`
Keywords []string `json:"keywords"`
}
// MaskPosition describes the position on faces where
// a mask should be placed by default.
type MaskPosition struct {
Feature MaskFeature `json:"point"`
XShift float32 `json:"x_shift"`
YShift float32 `json:"y_shift"`
Scale float32 `json:"scale"`
}
// UploadSticker uploads a sticker file for later use.
func (b *Bot) UploadSticker(to Recipient, format StickerSetFormat, f File) (*File, error) {
params := map[string]string{
"user_id": to.Recipient(),
"sticker_format": format,
}
data, err := b.sendFiles("uploadStickerFile", map[string]File{"0": f}, params)
if err != nil {
return nil, err
}
var resp struct {
Result File
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return &resp.Result, nil
}
// StickerSet returns a sticker set on success.
func (b *Bot) StickerSet(name string) (*StickerSet, error) {
data, err := b.Raw("getStickerSet", map[string]string{"name": name})
if err != nil {
return nil, err
}
var resp struct {
Result *StickerSet
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// CreateStickerSet creates a new sticker set.
func (b *Bot) CreateStickerSet(of Recipient, set *StickerSet) error {
files := make(map[string]File)
for i, s := range set.Input {
repr := s.File.process(strconv.Itoa(i), files)
if repr == "" {
return fmt.Errorf("telebot: sticker #%d does not exist", i+1)
}
set.Input[i].Sticker = repr
}
data, _ := json.Marshal(set.Input)
params := map[string]string{
"user_id": of.Recipient(),
"name": set.Name,
"title": set.Title,
"stickers": string(data),
}
if set.Type != "" {
params["sticker_type"] = set.Type
}
if set.Repaint {
params["needs_repainting"] = "true"
}
_, err := b.sendFiles("createNewStickerSet", files, params)
return err
}
// AddStickerToSet adds a new sticker to the existing sticker set.
func (b *Bot) AddStickerToSet(of Recipient, name string, sticker InputSticker) error {
files := make(map[string]File)
repr := sticker.File.process("0", files)
if repr == "" {
return errors.New("telebot: sticker does not exist")
}
sticker.Sticker = repr
data, _ := json.Marshal(sticker)
params := map[string]string{
"user_id": of.Recipient(),
"name": name,
"sticker": string(data),
}
_, err := b.sendFiles("addStickerToSet", files, params)
return err
}
// SetStickerPosition moves a sticker in set to a specific position.
func (b *Bot) SetStickerPosition(sticker string, position int) error {
params := map[string]string{
"sticker": sticker,
"position": strconv.Itoa(position),
}
_, err := b.Raw("setStickerPositionInSet", params)
return err
}
// DeleteSticker deletes a sticker from a set created by the bot.
func (b *Bot) DeleteSticker(sticker string) error {
_, err := b.Raw("deleteStickerFromSet", map[string]string{"sticker": sticker})
return err
}
// SetStickerSetThumb sets a thumbnail of the sticker set.
// Animated thumbnails can be set for animated sticker sets only.
//
// Thumbnail must be a PNG image, up to 128 kilobytes in size
// and have width and height exactly 100px, or a TGS animation
// up to 32 kilobytes in size.
//
// Animated sticker set thumbnail can't be uploaded via HTTP URL.
func (b *Bot) SetStickerSetThumb(of Recipient, set *StickerSet) error {
if set.Thumbnail == nil {
return errors.New("telebot: thumbnail is required")
}
files := make(map[string]File)
repr := set.Thumbnail.File.process("thumb", files)
if repr == "" {
return errors.New("telebot: thumbnail does not exist")
}
params := map[string]string{
"user_id": of.Recipient(),
"name": set.Name,
"format": set.Format,
"thumbnail": repr,
}
_, err := b.sendFiles("setStickerSetThumbnail", files, params)
return err
}
// SetStickerSetTitle sets the title of a created sticker set.
func (b *Bot) SetStickerSetTitle(s StickerSet) error {
params := map[string]string{
"name": s.Name,
"title": s.Title,
}
_, err := b.Raw("setStickerSetTitle", params)
return err
}
// DeleteStickerSet deletes a sticker set that was created by the bot.
func (b *Bot) DeleteStickerSet(name string) error {
params := map[string]string{"name": name}
_, err := b.Raw("deleteStickerSet", params)
return err
}
// SetStickerEmojis changes the list of emoji assigned to a regular or custom emoji sticker.
func (b *Bot) SetStickerEmojis(sticker string, emojis []string) error {
data, err := json.Marshal(emojis)
if err != nil {
return err
}
params := map[string]string{
"sticker": sticker,
"emoji_list": string(data),
}
_, err = b.Raw("setStickerEmojiList", params)
return err
}
// SetStickerKeywords changes search keywords assigned to a regular or custom emoji sticker.
func (b *Bot) SetStickerKeywords(sticker string, keywords []string) error {
mk, err := json.Marshal(keywords)
if err != nil {
return err
}
params := map[string]string{
"sticker": sticker,
"keywords": string(mk),
}
_, err = b.Raw("setStickerKeywords", params)
return err
}
// SetStickerMaskPosition changes the mask position of a mask sticker.
func (b *Bot) SetStickerMaskPosition(sticker string, mask MaskPosition) error {
data, err := json.Marshal(mask)
if err != nil {
return err
}
params := map[string]string{
"sticker": sticker,
"mask_position": string(data),
}
_, err = b.Raw("setStickerMaskPosition", params)
return err
}
// CustomEmojiStickers returns the information about custom emoji stickers by their ids.
func (b *Bot) CustomEmojiStickers(ids []string) ([]Sticker, error) {
data, _ := json.Marshal(ids)
params := map[string]string{
"custom_emoji_ids": string(data),
}
data, err := b.Raw("getCustomEmojiStickers", params)
if err != nil {
return nil, err
}
var resp struct {
Result []Sticker
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// SetCustomEmojiStickerSetThumb sets the thumbnail of a custom emoji sticker set.
func (b *Bot) SetCustomEmojiStickerSetThumb(name, id string) error {
params := map[string]string{
"name": name,
"custom_emoji_id": id,
}
_, err := b.Raw("setCustomEmojiStickerSetThumbnail", params)
return err
}
// ReplaceStickerInSet returns True on success, if existing sticker was replaced with a new one.
func (b *Bot) ReplaceStickerInSet(of Recipient, stickerSet, oldSticker string, sticker InputSticker) (bool, error) {
files := make(map[string]File)
repr := sticker.File.process("0", files)
if repr == "" {
return false, errors.New("telebot: sticker does not exist")
}
sticker.Sticker = repr
data, err := json.Marshal(sticker)
if err != nil {
return false, err
}
params := map[string]string{
"user_id": of.Recipient(),
"name": stickerSet,
"old_sticker": oldSticker,
"sticker": string(data),
}
_, err = b.sendFiles("replaceStickerInSet", files, params)
return true, err
}