/
sticker_sendables.go
209 lines (178 loc) · 5.3 KB
/
sticker_sendables.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
package ext
import (
"encoding/json"
"net/url"
"strconv"
"github.com/pkg/errors"
)
type File struct {
bot Bot
FileId string `json:"file_id"`
FileUniqueId string `json:"file_unique_id"`
FileSize int `json:"file_size"`
FilePath string `json:"file_path"`
}
type sendableSticker struct {
bot Bot
ChatId int
Sticker InputFile
DisableNotification bool
ReplyToMessageId int
AllowSendingWithoutReply bool
ReplyMarkup ReplyMarkup
}
func (b Bot) NewSendableSticker(chatId int) *sendableSticker {
return &sendableSticker{
bot: b,
ChatId: chatId,
AllowSendingWithoutReply: b.AllowSendingWithoutReply,
}
}
func (s *sendableSticker) Send() (*Message, error) {
var replyMarkup []byte
if s.ReplyMarkup != nil {
var err error
replyMarkup, err = s.ReplyMarkup.Marshal()
if err != nil {
return nil, err
}
}
v := url.Values{}
v.Add("chat_id", strconv.Itoa(s.ChatId))
v.Add("disable_notification", strconv.FormatBool(s.DisableNotification))
v.Add("reply_to_message_id", strconv.Itoa(s.ReplyToMessageId))
v.Add("allow_sending_without_reply", strconv.FormatBool(s.AllowSendingWithoutReply))
v.Add("reply_markup", string(replyMarkup))
r, err := s.Sticker.send("sendSticker", v, "sticker")
if err != nil {
return nil, err
}
return s.bot.ParseMessage(r)
}
type sendableUploadStickerFile struct {
bot Bot
UserId int
PngSticker InputFile
}
func (b Bot) NewSendableUploadStickerFile(userId int) *sendableUploadStickerFile {
return &sendableUploadStickerFile{bot: b, UserId: userId}
}
func (usf *sendableUploadStickerFile) Send() (*File, error) {
v := url.Values{}
v.Add("user_id", strconv.Itoa(usf.UserId))
r, err := usf.PngSticker.send("uploadStickerFile", v, "png_sticker")
if err != nil {
return nil, err
}
newFile := &File{}
newFile.bot = usf.bot
return newFile, json.Unmarshal(r, newFile)
}
type sendableCreateNewStickerSet struct {
bot Bot
UserId int
Name string
Title string
PngSticker *InputFile
TgsSticker *InputFile
Emojis string
ContainsMasks bool
MaskPosition *MaskPosition
}
func (b Bot) NewSendableCreateNewStickerSet(userId int, name string, title string, emojis string) *sendableCreateNewStickerSet {
return &sendableCreateNewStickerSet{bot: b, UserId: userId, Name: name, Title: title, Emojis: emojis}
}
func (cns *sendableCreateNewStickerSet) Send() (bool, error) {
var maskPos []byte
if cns.MaskPosition != nil {
var err error
maskPos, err = json.Marshal(cns.MaskPosition)
if err != nil {
return false, errors.Wrapf(err, "failed to parse mask position")
}
}
v := url.Values{}
v.Add("user_id", strconv.Itoa(cns.UserId))
v.Add("name", cns.Name)
v.Add("title", cns.Title)
v.Add("emojis", cns.Emojis)
v.Add("contains_mask", strconv.FormatBool(cns.ContainsMasks))
v.Add("mask_position", string(maskPos))
var r json.RawMessage
var err error
if cns.PngSticker != nil && cns.TgsSticker != nil {
return false, errors.New("can only specify one stickertype; png or tgs")
} else if cns.PngSticker != nil {
r, err = cns.PngSticker.send("createNewStickerSet", v, "png_sticker")
} else if cns.TgsSticker != nil {
r, err = cns.TgsSticker.send("createNewStickerSet", v, "tgs_sticker")
} else {
return false, errors.New("no stickertypes specified")
}
if err != nil {
return false, err
}
var bb bool
return bb, json.Unmarshal(r, &bb)
}
type sendableAddStickerToSet struct {
bot Bot
UserId int
Name string
PngSticker *InputFile
TgsSticker *InputFile
Emojis string
MaskPosition *MaskPosition
}
func (b Bot) NewSendableAddStickerToSet(userId int, name string, emojis string) *sendableAddStickerToSet {
return &sendableAddStickerToSet{bot: b, UserId: userId, Name: name, Emojis: emojis}
}
func (asts *sendableAddStickerToSet) Send() (bool, error) {
var maskPos []byte
if asts.MaskPosition != nil {
var err error
maskPos, err = json.Marshal(asts.MaskPosition)
if err != nil {
return false, errors.Wrapf(err, "failed to parse mask position")
}
}
v := url.Values{}
v.Add("user_id", strconv.Itoa(asts.UserId))
v.Add("name", asts.Name)
v.Add("emojis", asts.Emojis)
v.Add("mask_position", string(maskPos))
var r json.RawMessage
var err error
if asts.PngSticker != nil && asts.TgsSticker != nil {
return false, errors.New("can only specify one stickertype; png or tgs")
} else if asts.PngSticker != nil {
r, err = asts.PngSticker.send("addStickerToSet", v, "png_sticker")
} else if asts.TgsSticker != nil {
r, err = asts.TgsSticker.send("addStickerToSet", v, "tgs_sticker")
} else {
return false, errors.New("no stickertypes specified")
}
if err != nil {
return false, err
}
var bb bool
return bb, json.Unmarshal(r, &bb)
}
type sendableSetStickerSetThumb struct {
bot Bot
UserId int
Thumb InputFile
}
func (b Bot) NewSendableSetStickerSetThumb(userId int) *sendableSetStickerSetThumb {
return &sendableSetStickerSetThumb{bot: b, UserId: userId}
}
func (ssst *sendableSetStickerSetThumb) Send() (bool, error) {
v := url.Values{}
v.Add("user_id", strconv.Itoa(ssst.UserId))
r, err := ssst.Thumb.send("setStickerSetThumb", v, "thumb")
if err != nil {
return false, err
}
var bb bool
return bb, json.Unmarshal(r, &bb)
}