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