1 module telega.telegram.stickers;
2 
3 import std.typecons : Nullable;
4 import telega.botapi : BotApi, TelegramMethod, HTTPMethod, isTelegramId, ChatId;
5 import telega.telegram.basic : PhotoSize, ReplyMarkup, InputFile, Message, File;
6 
7 struct Sticker
8 {
9     string       file_id;
10     uint         width;
11     uint         height;
12     PhotoSize    thumb;
13     string       emoji;
14     string       set_name;
15     MaskPosition mask_position;
16     uint         file_size;
17 }
18 
19 struct StickerSet
20 {
21     string    name;
22     string    title;
23     bool      contains_masks;
24     Sticker[] stickers;
25 }
26 
27 struct MaskPosition
28 {
29     string point;
30     float  x_shift;
31     float  y_shift;
32     float  scale;
33 }
34 
35 // methods
36 
37 struct SendStickerMethod
38 {
39     mixin TelegramMethod!"/sendStickerMethod";
40 
41     ChatId      chat_id;
42     string      sticker; // TODO InputFile|string
43     Nullable!bool        disable_notification;
44     Nullable!uint        reply_to_message_id;
45     Nullable!ReplyMarkup reply_markup;
46 }
47 
48 unittest
49 {
50     import telega.serialization : serializeToJsonString;
51 
52     SendStickerMethod m = {
53         chat_id: "111",
54         sticker: "s",
55     };
56 
57     assert(m.serializeToJsonString() ==
58         `{"chat_id":"111","sticker":"s"}`
59     );
60 }
61 
62 struct GetStickerSetMethod
63 {
64     mixin TelegramMethod!("/getStickerSetMethod", HTTPMethod.GET);
65 
66     string name;
67 }
68 
69 struct UploadStickerFileMethod
70 {
71     mixin TelegramMethod!"/uploadStickerFileMethod";
72 
73     int       user_id;
74     InputFile png_sticker;
75 }
76 
77 struct CreateNewStickerSetMethod
78 {
79     mixin TelegramMethod!"/createNewStickerSetMethod";
80 
81     int          user_id;
82     string       name;
83     string       title;
84     string       png_sticker; // TODO InputFile|string
85     string       emojis;
86     bool         contains_masks;
87     MaskPosition mask_position;
88 }
89 
90 struct AddStickerToSetMethod
91 {
92     mixin TelegramMethod!"/addStickerToSetMethod";
93 
94     int          user_id;
95     string       name;
96     string       png_sticker; // TODO InputFile|string
97     string       emojis;
98     MaskPosition mask_position;
99 }
100 
101 struct SetStickerPositionInSetMethod
102 {
103     mixin TelegramMethod!"/setStickerPositionInSetMethod";
104 
105     string sticker;
106     int    position;
107 }
108 
109 struct DeleteStickerFromSetMethod
110 {
111     mixin TelegramMethod!"/deleteStickerFromSetMethod";
112 
113     string sticker;
114 }
115 
116 // API methods
117 Message sendSticker(BotApi api, ref SendStickerMethod m)
118 {
119     return api.callMethod!Message(m);
120 }
121 
122 // TODO sticker is InputFile|string
123 Message sendSticker(T1)(BotApi api, T1 chatId, string sticker)
124     if (isTelegramId!T1)
125 {
126     SendStickerMethod m = {
127         chat_id : chatId,
128         sticker : sticker
129     };
130 
131     return api.sendSticker(m);
132 }
133 
134 StickerSet getStickerSet(BotApi api, ref GetStickerSetMethod m)
135 {
136     return api.callMethod!StickerSet(m);
137 }
138 
139 StickerSet getStickerSet(BotApi api, string name)
140 {
141     GetStickerSetMethod m = {
142         name : name
143     };
144 
145     return api.getStickerSet(m);
146 }
147 
148 File uploadStickerFile(BotApi api, ref UploadStickerFileMethod m)
149 {
150     return api.callMethod!File(m);
151 }
152 
153 File uploadStickerFile(BotApi api, int userId, InputFile pngSticker)
154 {
155     UploadStickerFileMethod m = {
156         user_id : userId,
157         png_sticker : pngSticker
158     };
159 
160     return api.uploadStickerFile(m);
161 }
162 
163 bool createNewStickerSet(BotApi api, ref CreateNewStickerSetMethod m)
164 {
165     return api.callMethod!bool(m);
166 }
167 
168 // TODO pngSticker is InputFile|string
169 bool createNewStickerSet(BotApi api, int userId, string name, string title, string pngSticker, string emojis)
170 {
171     CreateNewStickerSetMethod m = {
172         user_id : userId,
173         name : name,
174         title : title,
175         png_sticker : pngSticker,
176         emojis : emojis
177     };
178 
179     return api.createNewStickerSet(m);
180 }
181 
182 bool addStickerToSet(BotApi api,ref AddStickerToSetMethod m)
183 {
184     return api.callMethod!bool(m);
185 }
186 
187 bool addStickerToSet(BotApi api, int userId, string name, string pngSticker, string emojis)
188 {
189     AddStickerToSetMethod m = {
190         user_id : userId,
191         name : name,
192         png_sticker : pngSticker,
193         emojis : emojis
194     };
195 
196     return api.addStickerToSet(m);
197 }
198 
199 bool setStickerPositionInSet(BotApi api, ref SetStickerPositionInSetMethod m)
200 {
201     return api.callMethod!bool(m);
202 }
203 
204 bool setStickerPositionInSet(BotApi api, string sticker, uint position)
205 {
206     SetStickerPositionInSetMethod m = {
207         sticker : sticker,
208         position : position
209     };
210 
211     return api.setStickerPositionInSet(m);
212 }
213 
214 bool deleteStickerFromSet(BotApi api, ref DeleteStickerFromSetMethod m)
215 {
216     return api.callMethod!bool(m);
217 }
218 
219 bool deleteStickerFromSet(BotApi api, string sticker)
220 {
221     DeleteStickerFromSetMethod m = {
222         sticker : sticker
223     };
224 
225     return api.deleteStickerFromSet(m);
226 }
227 
228 unittest
229 {
230     class BotApiMock : BotApi
231     {
232         this(string token)
233         {
234             super(token);
235         }
236 
237         T callMethod(T, M)(M method)
238         {
239             T result;
240 
241             logDiagnostic("[%d] Requesting %s", requestCounter, method.name);
242 
243             return result;
244         }
245     }
246 
247     auto api = new BotApiMock(null);
248 
249     api.sendSticker("chat-id", "sticker");
250     api.getStickerSet("sticker-set");
251     api.uploadStickerFile(1, InputFile());
252     api.createNewStickerSet(1, "name", "title", "png-sticker", "emojis");
253     api.addStickerToSet(1, "name", "png-sticker", "emojis");
254     api.setStickerPositionInSet("sticker", 42);
255     api.deleteStickerFromSet("sticker");
256 }