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