forked from PaulSonOfLars/gotgbot
/
sticker_sendables.go
164 lines (139 loc) · 3.95 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
package ext
import (
"encoding/json"
"net/url"
"strconv"
"github.com/pkg/errors"
)
type File struct {
bot Bot `json:"-"`
FileId string `json:"file_id"`
FileSize int `json:"file_size"`
FilePath string `json:"file_path"`
}
type sendableSticker struct {
bot Bot `json:"-"`
ChatId int
file
DisableNotification bool
ReplyToMessageId int
ReplyMarkup ReplyMarkup
}
func (b Bot) NewSendableSticker(chatId int) *sendableSticker {
return &sendableSticker{bot: b, ChatId: chatId}
}
func (s *sendableSticker) Send() (*Message, error) {
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))
if s.ReplyToMessageId != 0 {
v.Add("reply_to_message_id", strconv.Itoa(s.ReplyToMessageId))
}
if s.ReplyMarkup != nil {
v.Add("reply_markup", string(replyMarkup))
}
r, err := s.bot.sendFile(s.file, "sticker", "sendSticker", v)
if err != nil {
return nil, errors.Wrapf(err, "unable to sendSticker")
}
if !r.Ok {
return nil, errors.New(r.Description)
}
return s.bot.ParseMessage(r.Result)
}
type sendableUploadStickerFile struct {
bot Bot `json:"-"`
UserId int
file
}
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.bot.sendFile(usf.file, "sticker", "uploadStickerFile", v)
if err != nil {
return nil, errors.Wrapf(err, "unable to uploadStickerFile")
}
if !r.Ok {
return nil, errors.New(r.Description)
}
newFile := &File{}
newFile.bot = usf.bot
return newFile, json.Unmarshal(r.Result, newFile)
}
type sendableCreateNewSticker struct {
bot Bot `json:"-"`
UserId int
Name string
Title string
file
Emojis string
ContainsMasks bool
MaskPosition *MaskPosition
}
func (b Bot) NewSendableCreateNewSticker(userId int, name string, title string, emojis string) *sendableCreateNewSticker {
return &sendableCreateNewSticker{bot: b, UserId: userId, Name: name, Title: title, Emojis: emojis}
}
func (cns *sendableCreateNewSticker) Send() (bool, 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))
r, err := cns.bot.sendFile(cns.file, "sticker", "createNewStickerSet", v)
if err != nil {
return false, errors.Wrapf(err, "unable to createNewStickerSet")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}
type sendableAddStickerToSet struct {
bot Bot `json:"-"`
UserId int
Name string
file
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) {
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))
r, err := asts.bot.sendFile(asts.file, "sticker", "addStickerToSet", v)
if err != nil {
return false, errors.Wrapf(err, "unable to addStickerToSet")
}
if !r.Ok {
return false, errors.New(r.Description)
}
var bb bool
return bb, json.Unmarshal(r.Result, &bb)
}