-
Notifications
You must be signed in to change notification settings - Fork 1
/
stickers.go
213 lines (182 loc) · 5.57 KB
/
stickers.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
package telebot
import (
"github.com/3JoB/unsafeConvert"
"github.com/3JoB/telebot/v2/pkg/params"
)
type StickerSetType = string
const (
StickerRegular = "regular"
StickerMask = "mask"
StickerCustomEmoji = "custom_emoji"
)
// StickerSet represents a sticker set.
type StickerSet struct {
Type StickerSetType `json:"sticker_type"`
Name string `json:"name"`
Title string `json:"title"`
Emojis string `json:"emojis"`
Animated bool `json:"is_animated"`
Video bool `json:"is_video"`
Stickers []Sticker `json:"stickers"`
Thumbnail *Photo `json:"thumbnail"`
PNG *File `json:"png_sticker"`
TGS *File `json:"tgs_sticker"`
WebM *File `json:"webm_sticker"`
MaskPosition *MaskPosition `json:"mask_position"`
}
// 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"`
}
// MaskFeature defines sticker mask position.
type MaskFeature string
const (
FeatureForehead MaskFeature = "forehead"
FeatureEyes MaskFeature = "eyes"
FeatureMouth MaskFeature = "mouth"
FeatureChin MaskFeature = "chin"
)
// UploadSticker uploads a PNG file with a sticker for later use.
func (b *Bot) UploadSticker(to Recipient, png *File) (*File, error) {
files := map[string]File{
"png_sticker": *png,
}
params := map[string]any{
"user_id": to.Recipient(),
}
data, err := b.sendFiles("uploadStickerFile", files, params)
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[File]
if err := b.json.NewDecoder(data).Decode(&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
}
defer ReleaseBuffer(data)
var resp Response[*StickerSet]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// CreateStickerSet creates a new sticker set.
func (b *Bot) CreateStickerSet(to Recipient, s StickerSet) error {
files := make(map[string]File)
if s.PNG != nil {
files["png_sticker"] = *s.PNG
}
if s.TGS != nil {
files["tgs_sticker"] = *s.TGS
}
if s.WebM != nil {
files["webm_sticker"] = *s.WebM
}
params := map[string]any{
"user_id": to.Recipient(),
"sticker_type": s.Type,
"name": s.Name,
"title": s.Title,
"emojis": s.Emojis,
}
if s.MaskPosition != nil {
data, _ := b.json.Marshal(&s.MaskPosition)
params["mask_position"] = unsafeConvert.StringPointer(data)
}
r, err := b.sendFiles("createNewStickerSet", files, params)
ReleaseBuffer(r)
return err
}
// Use this method to delete a sticker set that was created by the bot. Returns True on success.
func (b *Bot) DeleteStickerSet(name string) error {
r, err := b.Raw("deleteStickerSet", ¶ms.Any{Name: name})
ReleaseBuffer(r)
return err
}
// AddSticker adds a new sticker to the existing sticker set.
func (b *Bot) AddSticker(to Recipient, s StickerSet) error {
files := make(map[string]File)
if s.PNG != nil {
files["png_sticker"] = *s.PNG
} else if s.TGS != nil {
files["tgs_sticker"] = *s.TGS
} else if s.WebM != nil {
files["webm_sticker"] = *s.WebM
}
params := map[string]any{
"user_id": to.Recipient(),
"name": s.Name,
"emojis": s.Emojis,
}
if s.MaskPosition != nil {
data, _ := b.json.Marshal(&s.MaskPosition)
params["mask_position"] = unsafeConvert.StringPointer(data)
}
r, err := b.sendFiles("addStickerToSet", files, params)
ReleaseBuffer(r)
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": unsafeConvert.IntToString(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
}
// SetStickerSetThumbnail 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) SetStickerSetThumbnail(to Recipient, s StickerSet) error {
files := make(map[string]File)
if s.PNG != nil {
files["thumbnail"] = *s.PNG
} else if s.TGS != nil {
files["thumbnail"] = *s.TGS
}
params := map[string]any{
"name": s.Name,
"user_id": to.Recipient(),
}
_, err := b.sendFiles("setStickerSetThumbnail", files, params)
return err
}
// CustomEmojiStickers returns the information about custom emoji stickers by their ids.
func (b *Bot) CustomEmojiStickers(ids []string) ([]Sticker, error) {
params := map[string][]string{
"custom_emoji_ids": ids,
}
data, err := b.Raw("getCustomEmojiStickers", params)
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[[]Sticker]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}