From eb1de0a6148c88510be63ac57f1c03fddb6cb395 Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Tue, 30 Aug 2022 18:25:46 +0200 Subject: Rework Bot API 4.7 --- src/Api.cpp | 93 ++++++++++++--------- src/TgTypeParser.cpp | 224 +++++++++++++++++++++++++-------------------------- 2 files changed, 165 insertions(+), 152 deletions(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index 22115c2..38ad8e3 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -23,34 +23,17 @@ User::Ptr Api::getMe() const { return _tgTypeParser.parseJsonAndGetUser(sendRequest("getMe")); } -Message::Ptr Api::sendMessage(std::int64_t chatId, const string& text, bool disableWebPagePreview, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendMessage(boost::variant chatId, const std::string& text, const std::string& parseMode, + bool disableWebPagePreview, bool disableNotification, std::int32_t replyToMessageId, + GenericReply::Ptr replyMarkup) const { vector args; args.reserve(7); + args.emplace_back("chat_id", chatId); args.emplace_back("text", text); - if (disableWebPagePreview) { - args.emplace_back("disable_web_page_preview", disableWebPagePreview); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } if (!parseMode.empty()) { args.emplace_back("parse_mode", parseMode); } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendMessage", args)); -} - -Message::Ptr Api::sendMessage(const std::string &chatId, const string& text, bool disableWebPagePreview, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector args; - args.reserve(7); - args.emplace_back("chat_id", chatId); - args.emplace_back("text", text); if (disableWebPagePreview) { args.emplace_back("disable_web_page_preview", disableWebPagePreview); } @@ -63,12 +46,24 @@ Message::Ptr Api::sendMessage(const std::string &chatId, const string& text, boo if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendMessage", args)); } +Message::Ptr Api::sendMessage(const std::string& chatId, const std::string& text, bool disableWebPagePreview, + std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, + bool disableNotification) const { + + return sendMessage(chatId, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup); +} + +Message::Ptr Api::sendMessage(std::int64_t chatId, const std::string& text, bool disableWebPagePreview, + std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, + bool disableNotification) const { + + return sendMessage(chatId, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup); +} + Message::Ptr Api::forwardMessage(std::int64_t chatId, std::int64_t fromChatId, std::int32_t messageId, bool disableNotification) const { vector args; args.reserve(4); @@ -320,16 +315,16 @@ File::Ptr Api::uploadStickerFile(std::int64_t userId, const InputFile::Ptr pngSt return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); } -bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, const std::string& emojis, - boost::variant pngSticker, boost::variant tgsSticker, +bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, + const std::string& emojis, boost::variant pngSticker, boost::variant tgsSticker, bool containsMasks, MaskPosition::Ptr maskPosition) const { vector args; args.reserve(8); + args.emplace_back("user_id", userId); args.emplace_back("name", name); args.emplace_back("title", title); args.emplace_back("emojis", emojis); - if (pngSticker.which() == 0 /* InputFile::Ptr */) { auto file = boost::get(pngSticker); args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); @@ -348,18 +343,25 @@ bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, cons if (maskPosition != nullptr) { args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); } + return sendRequest("createNewStickerSet", args).get("", false); } +bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, + boost::variant pngSticker, const std::string& emojis, bool containsMasks, + MaskPosition::Ptr maskPosition) const { + + return createNewStickerSet(userId, name, title, emojis, pngSticker, "", containsMasks, maskPosition); +} + bool Api::addStickerToSet(std::int64_t userId, const std::string& name, const std::string& emojis, - boost::variant pngSticker, boost::variant tgsSticker, - MaskPosition::Ptr maskPosition) const { + boost::variant pngSticker, boost::variant tgsSticker, MaskPosition::Ptr maskPosition) const { vector args; args.reserve(6); + args.emplace_back("user_id", userId); args.emplace_back("name", name); args.emplace_back("emojis", emojis); - if (pngSticker.which() == 0 /* InputFile::Ptr */) { auto file = boost::get(pngSticker); args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); @@ -375,9 +377,16 @@ bool Api::addStickerToSet(std::int64_t userId, const std::string& name, const st if (maskPosition != nullptr) { args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); } + return sendRequest("addStickerToSet", args).get("", false); } +bool Api::addStickerToSet(std::int64_t userId, const std::string& name, boost::variant pngSticker, + const std::string& emojis, MaskPosition::Ptr maskPosition) const { + + return addStickerToSet(userId, name, emojis, pngSticker, "", maskPosition); +} + bool Api::setStickerPositionInSet(const string& sticker, std::uint32_t position) const { vector args; args.reserve(2); @@ -396,6 +405,7 @@ bool Api::deleteStickerFromSet(const string& sticker) const { bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost::variant thumb) const { vector args; args.reserve(3); + args.emplace_back("name", name); args.emplace_back("user_id", userId); if (thumb.which() == 0 /* InputFile::Ptr */) { @@ -404,6 +414,7 @@ bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost } else /* std::string */ { args.emplace_back("thumb", boost::get(thumb)); } + return sendRequest("setStickerSetThumb", args).get("", false); } @@ -1067,9 +1078,11 @@ bool Api::promoteChatMember(std::int64_t chatId, std::int64_t userId, bool canCh bool Api::setChatAdministratorCustomTitle(std::int64_t chatId, std::int64_t userId, const std::string& customTitle) const { vector args; args.reserve(3); + args.emplace_back("chat_id", chatId); args.emplace_back("user_id", userId); args.emplace_back("custom_title", customTitle); + return sendRequest("setChatAdministratorCustomTitle", args).get("", false); } @@ -1187,17 +1200,18 @@ void Api::deleteMessage(std::int64_t chatId, std::int32_t messageId) const { sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } -Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, bool disableNotification, std::int32_t replyToMessageId, - const GenericReply::Ptr replyMarkup, bool isAnonymous, const std::string& type, bool allowsMultipleAnswers, - std::int32_t correctOptionId, bool isClosed) const { +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, + bool isAnonymous, const std::string& type, bool allowsMultipleAnswers, + std::int32_t correctOptionId, bool isClosed, bool disableNotification, + std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { vector args; args.reserve(11); + args.emplace_back("chat_id", chatId); args.emplace_back("question", question); args.emplace_back("options", _tgTypeParser.parseArray([] (const std::string& option) -> std::string { return StringTools::urlEncode(option); }, options)); - if (!isAnonymous) { args.emplace_back("is_anonymous", isAnonymous); } @@ -1226,11 +1240,18 @@ Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, con return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); } -Message::Ptr Api::sendDice(std::int64_t chatId, bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, + bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { + + return sendPoll(chatId, question, options, true, "", false, 0, false, disableNotification, replyToMessageId, replyMarkup); +} + +Message::Ptr Api::sendDice(std::int64_t chatId, bool disableNotification, std::int32_t replyToMessageId, + GenericReply::Ptr replyMarkup) const { vector args; args.reserve(4); - args.emplace_back("chat_id", chatId); + args.emplace_back("chat_id", chatId); if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 37dafb6..41cf642 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -10,8 +10,8 @@ namespace TgBot { Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { auto result(make_shared()); - result->id = data.get("id"); - string type = data.get("type"); + result->id = data.get("id", 0); + string type = data.get("type", ""); if (type == "private") { result->type = Chat::Type::Private; } else if (type == "group") { @@ -21,20 +21,18 @@ Chat::Ptr TgTypeParser::parseJsonAndGetChat(const ptree& data) const { } else if (type == "channel") { result->type = Chat::Type::Channel; } - result->title = data.get("title", ""); - result->username = data.get("username", ""); - result->firstName = data.get("first_name", ""); - result->lastName = data.get("last_name", ""); - result->allMembersAreAdministrators = data.get("all_members_are_administrators", false); + result->title = data.get("title", ""); + result->username = data.get("username", ""); + result->firstName = data.get("first_name", ""); + result->lastName = data.get("last_name", ""); result->photo = tryParseJson(&TgTypeParser::parseJsonAndGetChatPhoto, data, "photo"); - result->description = data.get("description", ""); - result->inviteLink = data.get("invite_link", ""); + result->description = data.get("description", ""); + result->inviteLink = data.get("invite_link", ""); result->pinnedMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "pinned_message"); result->permissions = tryParseJson(&TgTypeParser::parseJsonAndGetChatPermissions, data, "permissions"); - result->slowModeDelay = data.get("slow_mode_delay"); - result->stickerSetName = data.get("sticker_set_name", ""); + result->slowModeDelay = data.get("slow_mode_delay", 0); + result->stickerSetName = data.get("sticker_set_name", ""); result->canSetStickerSet = data.get("can_set_sticker_set", false); - return result; } @@ -58,7 +56,6 @@ string TgTypeParser::parseChat(const Chat::Ptr& object) const { appendToJson(result, "username", object->username); appendToJson(result, "first_name", object->firstName); appendToJson(result, "last_name", object->lastName); - appendToJson(result, "all_members_are_administrators", object->allMembersAreAdministrators); appendToJson(result, "photo", parseChatPhoto(object->photo)); appendToJson(result, "description", object->description); appendToJson(result, "invite_link", object->inviteLink); @@ -74,12 +71,12 @@ string TgTypeParser::parseChat(const Chat::Ptr& object) const { User::Ptr TgTypeParser::parseJsonAndGetUser(const ptree& data) const { auto result(make_shared()); - result->id = data.get("id"); + result->id = data.get("id", 0); result->isBot = data.get("is_bot", false); - result->firstName = data.get("first_name"); - result->lastName = data.get("last_name", ""); - result->username = data.get("username", ""); - result->languageCode = data.get("language_code", ""); + result->firstName = data.get("first_name", ""); + result->lastName = data.get("last_name", ""); + result->username = data.get("username", ""); + result->languageCode = data.get("language_code", ""); result->canJoinGroups = data.get("can_join_groups", false); result->canReadAllGroupMessages = data.get("can_read_all_group_messages", false); result->supportsInlineQueries = data.get("supports_inline_queries", false); @@ -108,9 +105,9 @@ string TgTypeParser::parseUser(const User::Ptr& object) const { MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const ptree& data) const{ auto result(make_shared()); - result->type = data.get("type"); - result->offset = data.get("offset"); - result->length = data.get("length"); + result->type = data.get("type", ""); + result->offset = data.get("offset", 0); + result->length = data.get("length", 0); result->url = data.get("url", ""); result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); result->language = data.get("language", ""); @@ -136,21 +133,21 @@ string TgTypeParser::parseMessageEntity(const MessageEntity::Ptr& object) const Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { auto result(make_shared()); - result->messageId = data.get("message_id"); + result->messageId = data.get("message_id", 0); result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); - result->date = data.get("date"); + result->date = data.get("date", 0); result->chat = parseJsonAndGetChat(data.find("chat")->second); result->forwardFrom = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "forward_from"); result->forwardFromChat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat"); result->forwardFromMessageId = data.get("forward_from_message_id", 0); - result->forwardSignature = data.get("forward_signature", ""); - result->forwardSenderName = data.get("forward_sender_name", ""); - result->forwardDate = data.get("forward_date", 0); + result->forwardSignature = data.get("forward_signature", ""); + result->forwardSenderName = data.get("forward_sender_name", ""); + result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); result->editDate = data.get("edit_date", 0); - result->mediaGroupId = data.get("media_group_id", ""); - result->authorSignature = data.get("author_signature", ""); - result->text = data.get("text", ""); + result->mediaGroupId = data.get("media_group_id", ""); + result->authorSignature = data.get("author_signature", ""); + result->text = data.get("text", ""); result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); result->captionEntities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); result->audio = tryParseJson