1 module telega.telegram.groupchat; 2 3 import std.typecons : Nullable; 4 import telega.botapi : BotApi, TelegramMethod, HTTPMethod, ChatId, isTelegramId; 5 import telega.telegram.basic : User, InputFile; 6 7 /******************************************************************/ 8 /* Telegram types and enums */ 9 /******************************************************************/ 10 11 struct ChatMember 12 { 13 User user; 14 string status; 15 Nullable!uint until_date; 16 Nullable!bool can_be_edited; 17 Nullable!bool can_change_info; 18 Nullable!bool can_post_messages; 19 Nullable!bool can_edit_messages; 20 Nullable!bool can_delete_messages; 21 Nullable!bool can_invite_users; 22 Nullable!bool can_restrict_members; 23 Nullable!bool can_pin_messages; 24 Nullable!bool can_promote_members; 25 Nullable!bool can_send_messages; 26 Nullable!bool can_send_media_messages; 27 Nullable!bool can_send_other_messages; 28 Nullable!bool can_add_web_page_previews; 29 } 30 31 /******************************************************************/ 32 /* Telegram methods */ 33 /******************************************************************/ 34 35 struct KickChatMemberMethod 36 { 37 mixin TelegramMethod!"/kickChatMember"; 38 39 ChatId chat_id; 40 uint user_id; 41 uint until_date; 42 } 43 44 struct UnbanChatMemberMethod 45 { 46 mixin TelegramMethod!"/unbanChatMember"; 47 48 ChatId chat_id; 49 uint user_id; 50 } 51 52 struct RestrictChatMemberMethod 53 { 54 mixin TelegramMethod!"/restrictChatMember"; 55 56 ChatId chat_id; 57 uint user_id; 58 uint until_date; 59 bool can_send_messages; 60 bool can_send_media_messages; 61 bool can_send_other_messages; 62 bool can_add_web_page_previews; 63 } 64 65 struct PromoteChatMemberMethod 66 { 67 mixin TelegramMethod!"/promoteChatMember"; 68 69 ChatId chat_id; 70 uint user_id; 71 bool can_change_info; 72 bool can_post_messages; 73 bool can_edit_messages; 74 bool can_delete_messages; 75 bool can_invite_users; 76 bool can_restrict_members; 77 bool can_pin_messages; 78 bool can_promote_members; 79 } 80 81 struct ExportChatInviteLinkMethod 82 { 83 mixin TelegramMethod!"/exportChatInviteLink"; 84 85 ChatId chat_id; 86 } 87 88 struct SetChatPhotoMethod 89 { 90 mixin TelegramMethod!"/setChatPhoto"; 91 92 ChatId chat_id; 93 InputFile photo; 94 95 } 96 97 struct DeleteChatPhotoMethod 98 { 99 mixin TelegramMethod!"/deleteChatPhoto"; 100 101 ChatId chat_id; 102 } 103 104 struct SetChatTitleMethod 105 { 106 mixin TelegramMethod!"/setChatTitle"; 107 108 ChatId chat_id; 109 string title; 110 } 111 112 struct SetChatDescriptionMethod 113 { 114 mixin TelegramMethod!"/setChatDescription"; 115 116 ChatId chat_id; 117 string description; 118 } 119 120 struct PinChatMessageMethod 121 { 122 mixin TelegramMethod!"/pinChatMessage"; 123 124 ChatId chat_id; 125 uint message_id; 126 bool disable_notification; 127 } 128 129 struct UnpinChatMessageMethod 130 { 131 mixin TelegramMethod!"/unpinChatMessage"; 132 133 ChatId chat_id; 134 } 135 136 struct LeaveChatMethod 137 { 138 mixin TelegramMethod!"/leaveChat"; 139 140 ChatId chat_id; 141 } 142 143 struct GetChatAdministratorsMethod 144 { 145 mixin TelegramMethod!("/getChatAdministrators", HTTPMethod.GET); 146 147 ChatId chat_id; 148 } 149 150 struct GetChatMembersCountMethod 151 { 152 mixin TelegramMethod!("/getChatMembersCount", HTTPMethod.GET); 153 154 ChatId chat_id; 155 } 156 157 struct GetChatMemberMethod 158 { 159 mixin TelegramMethod!("/getChatMember", HTTPMethod.GET); 160 161 ChatId chat_id; 162 uint user_id; 163 } 164 165 struct SetChatStickerSetMethod 166 { 167 mixin TelegramMethod!"/setChatStickerSet"; 168 169 ChatId chat_id; 170 string sticker_set_name; 171 } 172 173 struct DeleteChatStickerSetMethod 174 { 175 mixin TelegramMethod!"/deleteChatStickerSet"; 176 177 ChatId chat_id; 178 } 179 180 bool kickChatMember(BotApi api, ref KickChatMemberMethod m) 181 { 182 return api.callMethod!(bool, KickChatMemberMethod)(m); 183 } 184 185 bool kickChatMember(T1)(BotApi api, T1 chatId, int userId) 186 if(isTelegramId!T1) 187 { 188 KickChatMemberMethod m = { 189 chat_id : chatId, 190 user_id : userId 191 }; 192 193 return kickChatMember(api, m); 194 } 195 196 bool unbanChatMember(BotApi api, ref UnbanChatMemberMethod m) 197 { 198 return api.callMethod!(bool, UnbanChatMemberMethod)(m); 199 } 200 201 bool unbanChatMember(T1)(BotApi api, T1 chatId, int userId) 202 if(isTelegramId!T1) 203 { 204 UnbanChatMemberMethod m = { 205 chat_id : chatId, 206 user_id : userId 207 }; 208 209 return unbanChatMember(api, m); 210 } 211 212 bool restrictChatMember(BotApi api, ref RestrictChatMemberMethod m) 213 { 214 return api.callMethod!bool(m); 215 } 216 217 bool restrictChatMember(T1)(BotApi api, T1 chatId, int userId) 218 if(isTelegramId!T1) 219 { 220 RestrictChatMemberMethod m = { 221 chat_id : chatId, 222 user_id : userId 223 }; 224 225 return restrictChatMember(api, m); 226 } 227 228 bool promoteChatMember(BotApi api, ref PromoteChatMemberMethod m) 229 { 230 return api.callMethod!bool(m); 231 } 232 233 bool promoteChatMember(T1)(BotApi api, T1 chatId, int userId) 234 if(isTelegramId!T1) 235 { 236 PromoteChatMemberMethod m = { 237 chat_id : chatId, 238 user_id : userId 239 }; 240 241 return promoteChatMember(api, m); 242 } 243 244 string exportChatInviteLink(BotApi api, ref ExportChatInviteLinkMethod m) 245 { 246 return api.callMethod!string(m); 247 } 248 249 string exportChatInviteLink(T1)(BotApi api, T1 chatId) 250 if(isTelegramId!T1) 251 { 252 ExportChatInviteLinkMethod m = { 253 chat_id : chatId, 254 }; 255 256 return exportChatInviteLink(api, m); 257 } 258 259 bool setChatPhoto(BotApi api, ref SetChatPhotoMethod m) 260 { 261 return api.callMethod!bool(m); 262 } 263 264 bool setChatPhoto(T1)(BotApi api, T1 chatId, InputFile photo) 265 if (isTelegramId!T1) 266 { 267 SetChatPhotoMethod m = { 268 chat_id : chatId, 269 photo : photo 270 }; 271 272 return setChatPhoto(api, m); 273 } 274 275 bool deleteChatPhoto(BotApi api, ref DeleteChatPhotoMethod m) 276 { 277 return api.callMethod!bool(m); 278 } 279 280 bool deleteChatPhoto(T1)(BotApi api, T1 chatId) 281 if (isTelegramId!T1) 282 { 283 DeleteChatPhotoMethod m = { 284 chat_id : chatId, 285 }; 286 287 return deleteChatPhoto(api, m); 288 } 289 290 bool setChatTitle(BotApi api, ref SetChatTitleMethod m) 291 { 292 return api.callMethod!bool(m); 293 } 294 295 bool setChatTitle(T1)(BotApi api, T1 chatId, string title) 296 if (isTelegramId!T1) 297 { 298 SetChatTitleMethod m = { 299 chat_id : chatId, 300 title : title 301 }; 302 303 return setChatTitle(api, m); 304 } 305 306 bool setChatDescription(BotApi api, ref SetChatDescriptionMethod m) 307 { 308 return api.callMethod!bool(m); 309 } 310 311 bool setChatDescription(T1)(BotApi api, T1 chatId, string description) 312 if (isTelegramId!T1) 313 { 314 SetChatDescriptionMethod m = { 315 chat_id : chatId, 316 description : description 317 }; 318 319 return setChatDescription(api, m); 320 } 321 322 bool pinChatMessage(BotApi api, ref PinChatMessageMethod m) 323 { 324 return api.callMethod!bool(m); 325 } 326 327 bool pinChatMessage(T1)(BotApi api, T1 chatId, uint messageId) 328 if (isTelegramId!T1) 329 { 330 PinChatMessageMethod m = { 331 chat_id : chatId, 332 message_id : messageId 333 }; 334 335 return pinChatMessage(api, m); 336 } 337 338 bool unpinChatMessage(BotApi api, ref UnpinChatMessageMethod m) 339 { 340 return api.callMethod!bool(m); 341 } 342 343 bool unpinChatMessage(T1)(BotApi api, T1 chatId) 344 if (isTelegramId!T1) 345 { 346 UnpinChatMessageMethod m = { 347 chat_id : chatId, 348 }; 349 350 return unpinChatMessage(api, m); 351 } 352 353 bool leaveChat(BotApi api, ref LeaveChatMethod m) 354 { 355 return api.callMethod!bool(m); 356 } 357 358 bool leaveChat(T1)(BotApi api, T1 chatId) 359 if (isTelegramId!T1) 360 { 361 LeaveChatMethod m = { 362 chat_id : chatId, 363 }; 364 365 return leaveChat(api, m); 366 } 367 368 ChatMember getChatAdministrators(BotApi api, ref GetChatAdministratorsMethod m) 369 { 370 return api.callMethod!ChatMember(m); 371 } 372 373 ChatMember getChatAdministrators(T1)(BotApi api, T1 chatId) 374 if (isTelegramId!T1) 375 { 376 GetChatAdministratorsMethod m = { 377 chat_id : chatId, 378 }; 379 380 return getChatAdministrators(api, m); 381 } 382 383 uint getChatMembersCount(BotApi api, ref GetChatMembersCountMethod m) 384 { 385 return api.callMethod!uint(m); 386 } 387 388 uint getChatMembersCount(T1)(BotApi api, T1 chatId) 389 if (isTelegramId!T1) 390 { 391 GetChatMembersCountMethod m = { 392 chat_id : chatId, 393 }; 394 395 return getChatMembersCount(api, m); 396 } 397 398 ChatMember getChatMember(BotApi api, ref GetChatMemberMethod m) 399 { 400 return api.callMethod!ChatMember(m); 401 } 402 403 ChatMember getChatMember(T1)(BotApi api, T1 chatId, int userId) 404 if (isTelegramId!T1) 405 { 406 GetChatMemberMethod m = { 407 chat_id : chatId, 408 user_id : userId 409 }; 410 411 return getChatMember(api, m); 412 } 413 414 bool setChatStickerSet(BotApi api, ref SetChatStickerSetMethod m) 415 { 416 return api.callMethod!bool(m); 417 } 418 419 bool setChatStickerSet(T1)(BotApi api, T1 chatId, string stickerSetName) 420 if (isTelegramId!T1) 421 { 422 SetChatStickerSetMethod m = { 423 chat_id : chatId, 424 sticker_set_name : stickerSetName 425 }; 426 427 return setChatStickerSet(api, m); 428 } 429 430 bool deleteChatStickerSet(BotApi api, ref DeleteChatStickerSetMethod m) 431 { 432 return api.callMethod!bool(m); 433 } 434 435 bool deleteChatStickerSet(T1)(BotApi api, T1 chatId) 436 if (isTelegramId!T1) 437 { 438 DeleteChatStickerSetMethod m = { 439 chat_id : chatId, 440 }; 441 442 return deleteChatStickerSet(api, m); 443 } 444 445 unittest 446 { 447 class BotApiMock : BotApi 448 { 449 this(string token) 450 { 451 super(token); 452 } 453 454 T callMethod(T, M)(M method) 455 { 456 T result; 457 458 logDiagnostic("[%d] Requesting %s", requestCounter, method.name); 459 460 return result; 461 } 462 } 463 464 auto api = new BotApiMock(null); 465 466 api.kickChatMember("chat-id", 1); 467 api.unbanChatMember("chat-id", 1); 468 api.restrictChatMember("chat-id", 1); 469 api.promoteChatMember("chat-id", 1); 470 api.exportChatInviteLink("chat-id"); 471 api.setChatPhoto("chat-id", InputFile()); 472 api.deleteChatPhoto("chat-id"); 473 api.setChatTitle("chat-id", "chat-title"); 474 api.setChatDescription("chat-id", "chat-description"); 475 api.pinChatMessage("chat-id", 1); 476 api.unpinChatMessage("chat-id"); 477 api.leaveChat("chat-id"); 478 api.getChatAdministrators("chat-id"); 479 api.getChatMembersCount("chat-id"); 480 api.getChatMember("chat-id", 1); 481 api.setChatStickerSet("chat-id", "sticker-set"); 482 api.deleteChatStickerSet("chat-id"); 483 }