From 084a6492103eff0d021562694c6869fdcce58261 Mon Sep 17 00:00:00 2001 From: nitanmarcel Date: Wed, 20 Nov 2019 21:04:11 +0200 Subject: API 4.4 --- src/Api.cpp | 29 +++++++++++++---------------- src/TgTypeParser.cpp | 46 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index ae6ecee..ee8f36f 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -983,28 +983,17 @@ bool Api::unbanChatMember(int64_t chatId, int32_t userId) const { args.emplace_back("user_id", userId); return sendRequest("unbanChatMember", args).get("", false); } - -bool Api::restrictChatMember(int64_t chatId, int32_t userId, uint64_t untilDate, bool canSendMessages, - bool canSendMediaMessages, bool canSendOtherMessages, bool canAddWebPagePreviews) const { +bool Api::restrictChatMember(int64_t chatId, int32_t userId, TgBot::ChatPermissions::Ptr permissions, + uint64_t untilDate) const { vector args; - args.reserve(7); + args.reserve(4); args.emplace_back("chat_id", chatId); args.emplace_back("user_id", userId); + args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); if (untilDate) { args.emplace_back("until_date", untilDate); } - if (canSendMessages) { - args.emplace_back("can_send_messages", canSendMessages); - } - if (canSendMediaMessages) { - args.emplace_back("can_send_media_messages", canSendMediaMessages); - } - if (canSendOtherMessages) { - args.emplace_back("can_send_other_messages", canSendOtherMessages); - } - if (canAddWebPagePreviews) { - args.emplace_back("can_add_web_page_previews", canAddWebPagePreviews); - } + return sendRequest("restrictChatMember", args).get("", false); } @@ -1038,6 +1027,14 @@ bool Api::promoteChatMember(int64_t chatId, int32_t userId, bool canChangeInfo, return sendRequest("promoteChatMember", args).get("", false); } +bool Api::setChatPermissions(int64_t chatId, ChatPermissions::Ptr permissions) const{ + vector args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); + return sendRequest("setChatPermissions", args).get("", false); +} + string Api::exportChatInviteLink(int64_t chatId) const { vector args; args.reserve(1); diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 5206286..0a098f0 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -318,6 +318,7 @@ Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const ptree& data) const { result->fileId = data.get("file_id"); result->width = data.get("width"); result->height = data.get("height"); + result->isAnimated = data.get("is_animated", false); result->thumb = tryParseJson(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb"); result->emoji = data.get("emoji", ""); result->setName = data.get("set_name", ""); @@ -335,6 +336,7 @@ string TgTypeParser::parseSticker(const Sticker::Ptr& object) const { appendToJson(result, "file_id", object->fileId); appendToJson(result, "width", object->width); appendToJson(result, "height", object->height); + appendToJson(result, "is_animated", object->isAnimated); appendToJson(result, "thumb", parsePhotoSize(object->thumb)); appendToJson(result, "emoji", object->emoji); appendToJson(result, "file_size", object->fileSize); @@ -348,6 +350,7 @@ StickerSet::Ptr TgTypeParser::parseJsonAndGetStickerSet(const ptree& data) const result->name = data.get("name", ""); result->title = data.get("title", ""); result->containsMasks = data.get("contains_masks", false); + result->isAnimated = data.get("is_animated", false); result->stickers = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetSticker, data, "stickers"); return result; } @@ -360,6 +363,7 @@ string TgTypeParser::parseStickerSet(const StickerSet::Ptr& object) const { result += '{'; appendToJson(result, "name", object->name); appendToJson(result, "title", object->title); + appendToJson(result, "is_animated", object->isAnimated); appendToJson(result, "contains_masks", object->containsMasks); appendToJson(result, "stickers", parseArray(&TgTypeParser::parseSticker, object->stickers)); removeLastComma(result); @@ -415,11 +419,11 @@ string TgTypeParser::parsePoll(const Poll::Ptr& object) const { return result; } - PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const ptree& data) const { - auto result(make_shared()); - result->text = data.get("text", ""); - result->voter_count = data.get("voter_count", 0); - return result; +PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const ptree& data) const { + auto result(make_shared()); + result->text = data.get("text", ""); + result->voter_count = data.get("voter_count", 0); + return result; } string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { @@ -435,6 +439,38 @@ string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { return result; } +ChatPermissions::Ptr TgTypeParser::parseJsonAndGetChatPermissions(const ptree& data) const { + auto result(make_shared()); + result->canSendMessages = data.get("can_send_messages"); + result->canSendMediaMessages = data.get("can_send_media_messages"); + result->canSendPolls = data.get("can_send_polls"); + result->canSendOtherMessages = data.get("can_send_other_messages"); + result->canAddWebPagePreviews = data.get("can_add_web_page_previews"); + result->canChangeInfo = data.get("can_change_info"); + result->canInviteUsers = data.get("can_invite_users"); + result->canPinMessages = data.get("can_pin_messages"); + return result; +} + +string TgTypeParser::parseChatPermissions(const ChatPermissions::Ptr& object) const { + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "can_send_messages", object->canSendMessages); + appendToJson(result, "can_send_media_messages", object->canSendMediaMessages); + appendToJson(result, "can_send_polls", object->canSendPolls); + appendToJson(result, "can_send_other_messages", object->canSendOtherMessages); + appendToJson(result, "can_add_web_page_previews", object->canAddWebPagePreviews); + appendToJson(result, "can_change_info", object->canChangeInfo); + appendToJson(result, "can_invite_users", object->canInviteUsers); + appendToJson(result, "can_pin_messages", object->canPinMessages); + removeLastComma(result); + result += '}'; + return result; +} + Video::Ptr TgTypeParser::parseJsonAndGetVideo(const ptree& data) const { auto result(make_shared