From 7b72d7a35fce82049fa63444b0ce5f2a3fc22129 Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Sun, 14 Apr 2024 23:43:12 +0200 Subject: Update to Bot API 6.6 --- src/Api.cpp | 374 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 261 insertions(+), 113 deletions(-) (limited to 'src/Api.cpp') diff --git a/src/Api.cpp b/src/Api.cpp index c8ce399..bb0ac45 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -293,7 +293,7 @@ Message::Ptr Api::sendAudio(boost::variant chatId, std::int32_t duration, const std::string& performer, const std::string& title, - boost::variant thumb, + boost::variant thumbnail, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, @@ -333,13 +333,13 @@ Message::Ptr Api::sendAudio(boost::variant chatId, if (!title.empty()) { args.emplace_back("title", title); } - if (thumb.which() == 0) { // InputFile::Ptr - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + if (thumbnail.which() == 0) { // InputFile::Ptr + auto file = boost::get(thumbnail); + args.emplace_back("thumbnail", file->data, true, file->mimeType, file->fileName); } else { // std::string - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); + auto thumbnailStr = boost::get(thumbnail); + if (!thumbnailStr.empty()) { + args.emplace_back("thumbnail", thumbnailStr); } } if (disableNotification) { @@ -363,7 +363,7 @@ Message::Ptr Api::sendAudio(boost::variant chatId, Message::Ptr Api::sendDocument(boost::variant chatId, boost::variant document, - boost::variant thumb, + boost::variant thumbnail, const std::string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -387,13 +387,13 @@ Message::Ptr Api::sendDocument(boost::variant chatId, } else { // std::string args.emplace_back("document", boost::get(document)); } - if (thumb.which() == 0) { // InputFile::Ptr - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + if (thumbnail.which() == 0) { // InputFile::Ptr + auto file = boost::get(thumbnail); + args.emplace_back("thumbnail", file->data, true, file->mimeType, file->fileName); } else { // std::string - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); + auto thumbnailStr = boost::get(thumbnail); + if (!thumbnailStr.empty()) { + args.emplace_back("thumbnail", thumbnailStr); } } if (!caption.empty()) { @@ -433,7 +433,7 @@ Message::Ptr Api::sendVideo(boost::variant chatId, std::int32_t duration, std::int32_t width, std::int32_t height, - boost::variant thumb, + boost::variant thumbnail, const std::string& caption , std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -466,13 +466,13 @@ Message::Ptr Api::sendVideo(boost::variant chatId, if (height != 0) { args.emplace_back("height", height); } - if (thumb.which() == 0) { // InputFile::Ptr - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + if (thumbnail.which() == 0) { // InputFile::Ptr + auto file = boost::get(thumbnail); + args.emplace_back("thumbnail", file->data, true, file->mimeType, file->fileName); } else { // std::string - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); + auto thumbnailStr = boost::get(thumbnail); + if (!thumbnailStr.empty()) { + args.emplace_back("thumbnail", thumbnailStr); } } if (!caption.empty()) { @@ -514,7 +514,7 @@ Message::Ptr Api::sendAnimation(boost::variant chatId std::int32_t duration, std::int32_t width, std::int32_t height, - boost::variant thumb, + boost::variant thumbnail, const std::string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, @@ -547,13 +547,13 @@ Message::Ptr Api::sendAnimation(boost::variant chatId if (height != 0) { args.emplace_back("height", height); } - if (thumb.which() == 0) { // InputFile::Ptr - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + if (thumbnail.which() == 0) { // InputFile::Ptr + auto file = boost::get(thumbnail); + args.emplace_back("thumbnail", file->data, true, file->mimeType, file->fileName); } else { // std::string - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); + auto thumbnailStr = boost::get(thumbnail); + if (!thumbnailStr.empty()) { + args.emplace_back("thumbnail", thumbnailStr); } } if (!caption.empty()) { @@ -649,7 +649,7 @@ Message::Ptr Api::sendVideoNote(boost::variant chatId bool disableNotification, std::int32_t duration, std::int32_t length, - boost::variant thumb, + boost::variant thumbnail, GenericReply::Ptr replyMarkup, bool allowSendingWithoutReply, bool protectContent, @@ -673,13 +673,13 @@ Message::Ptr Api::sendVideoNote(boost::variant chatId if (length) { args.emplace_back("length", length); } - if (thumb.which() == 0) { // InputFile::Ptr - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + if (thumbnail.which() == 0) { // InputFile::Ptr + auto file = boost::get(thumbnail); + args.emplace_back("thumbnail", file->data, true, file->mimeType, file->fileName); } else { // std::string - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); + auto thumbnailStr = boost::get(thumbnail); + if (!thumbnailStr.empty()) { + args.emplace_back("thumbnail", thumbnailStr); } } if (disableNotification) { @@ -797,8 +797,14 @@ Message::Ptr Api::editMessageLiveLocation(float latitude, std::vector args; args.reserve(9); - if ((boost::get(chatId) != 0) || (boost::get(chatId) != "")) { - args.emplace_back("chat_id", chatId); + if (chatId.which() == 0) { // std::int64_t + if (boost::get(chatId) != 0) { + args.emplace_back("chat_id", chatId); + } + } else { // std::string + if (boost::get(chatId) != "") { + args.emplace_back("chat_id", chatId); + } } if (messageId) { args.emplace_back("message_id", messageId); @@ -831,8 +837,14 @@ Message::Ptr Api::stopMessageLiveLocation(boost::variant args; args.reserve(4); - if ((boost::get(chatId) != 0) || (boost::get(chatId) != "")) { - args.emplace_back("chat_id", chatId); + if (chatId.which() == 0) { // std::int64_t + if (boost::get(chatId) != 0) { + args.emplace_back("chat_id", chatId); + } + } else { // std::string + if (boost::get(chatId) != "") { + args.emplace_back("chat_id", chatId); + } } if (messageId) { args.emplace_back("message_id", messageId); @@ -1539,7 +1551,7 @@ ForumTopic::Ptr Api::createForumTopic(boost::variant bool Api::editForumTopic(boost::variant chatId, std::int32_t messageThreadId, const std::string& name, - boost::variant iconCustomEmojiId) const { + boost::variant iconCustomEmojiId) const { std::vector args; args.reserve(4); @@ -1548,8 +1560,14 @@ bool Api::editForumTopic(boost::variant chatId, if (!name.empty()) { args.emplace_back("name", name); } - if (iconCustomEmojiId.which() == 1) { // std::string - args.emplace_back("icon_custom_emoji_id", boost::get(iconCustomEmojiId)); + if (iconCustomEmojiId.which() == 0) { // std::int32_t + if (boost::get(iconCustomEmojiId) != 0) { + args.emplace_back("icon_custom_emoji_id", iconCustomEmojiId); + } + } else { // std::string + if (boost::get(iconCustomEmojiId) != "") { + args.emplace_back("icon_custom_emoji_id", iconCustomEmojiId); + } } return sendRequest("editForumTopic", args).get("", false); @@ -1689,7 +1707,7 @@ bool Api::setMyCommands(const std::vector& commands, } bool Api::deleteMyCommands(BotCommandScope::Ptr scope, - const std::string& languageCode) const { + const std::string& languageCode) const { std::vector args; args.reserve(2); @@ -1707,7 +1725,7 @@ std::vector Api::getMyCommands(BotCommandScope::Ptr scope, const std::string& languageCode) const { std::vector args; args.reserve(2); -; + if (scope != nullptr) { args.emplace_back("scope", _tgTypeParser.parseBotCommandScope(scope)); } @@ -1718,6 +1736,58 @@ std::vector Api::getMyCommands(BotCommandScope::Ptr scope, return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetBotCommand, sendRequest("getMyCommands", args)); } +bool Api::setMyDescription(const std::string& description, + const std::string& languageCode) const { + std::vector args; + args.reserve(2); + + if (!description.empty()) { + args.emplace_back("description", description); + } + if (!languageCode.empty()) { + args.emplace_back("language_code", languageCode); + } + + return sendRequest("setMyDescription", args).get("", false); +} + +BotDescription::Ptr Api::getMyDescription(const std::string& languageCode) const { + std::vector args; + args.reserve(1); + + if (!languageCode.empty()) { + args.emplace_back("language_code", languageCode); + } + + return _tgTypeParser.parseJsonAndGetBotDescription(sendRequest("getMyDescription", args)); +} + +bool Api::setMyShortDescription(const std::string& shortDescription, + const std::string& languageCode) const { + std::vector args; + args.reserve(2); + + if (!shortDescription.empty()) { + args.emplace_back("short_description", shortDescription); + } + if (!languageCode.empty()) { + args.emplace_back("language_code", languageCode); + } + + return sendRequest("setMyShortDescription", args).get("", false); +} + +BotShortDescription::Ptr Api::getMyShortDescription(const std::string& languageCode) const { + std::vector args; + args.reserve(1); + + if (!languageCode.empty()) { + args.emplace_back("language_code", languageCode); + } + + return _tgTypeParser.parseJsonAndGetBotShortDescription(sendRequest("getMyShortDescription", args)); +} + bool Api::setChatMenuButton(std::int64_t chatId, MenuButton::Ptr menuButton) const { std::vector args; @@ -1781,8 +1851,14 @@ Message::Ptr Api::editMessageText(const std::string& text, std::vector args; args.reserve(8); - if ((boost::get(chatId) != 0) || (boost::get(chatId) != "")) { - args.emplace_back("chat_id", chatId); + if (chatId.which() == 0) { // std::int64_t + if (boost::get(chatId) != 0) { + args.emplace_back("chat_id", chatId); + } + } else { // std::string + if (boost::get(chatId) != "") { + args.emplace_back("chat_id", chatId); + } } if (messageId) { args.emplace_back("message_id", messageId); @@ -1822,8 +1898,14 @@ Message::Ptr Api::editMessageCaption(boost::variant c std::vector args; args.reserve(7); - if ((boost::get(chatId) != 0) || (boost::get(chatId) != "")) { - args.emplace_back("chat_id", chatId); + if (chatId.which() == 0) { // std::int64_t + if (boost::get(chatId) != 0) { + args.emplace_back("chat_id", chatId); + } + } else { // std::string + if (boost::get(chatId) != "") { + args.emplace_back("chat_id", chatId); + } } if (messageId) { args.emplace_back("message_id", messageId); @@ -1861,10 +1943,16 @@ Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::vector args; args.reserve(5); - args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); - if ((boost::get(chatId) != 0) || (boost::get(chatId) != "")) { - args.emplace_back("chat_id", chatId); + if (chatId.which() == 0) { // std::int64_t + if (boost::get(chatId) != 0) { + args.emplace_back("chat_id", chatId); + } + } else { // std::string + if (boost::get(chatId) != "") { + args.emplace_back("chat_id", chatId); + } } + args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); if (messageId) { args.emplace_back("message_id", messageId); } @@ -1891,8 +1979,14 @@ Message::Ptr Api::editMessageReplyMarkup(boost::variant args; args.reserve(4); - if ((boost::get(chatId) != 0) || (boost::get(chatId) != "")) { - args.emplace_back("chat_id", chatId); + if (chatId.which() == 0) { // std::int64_t + if (boost::get(chatId) != 0) { + args.emplace_back("chat_id", chatId); + } + } else { // std::string + if (boost::get(chatId) != "") { + args.emplace_back("chat_id", chatId); + } } if (messageId) { args.emplace_back("message_id", messageId); @@ -1945,9 +2039,10 @@ Message::Ptr Api::sendSticker(boost::variant chatId, bool disableNotification, bool allowSendingWithoutReply, bool protectContent, - std::int32_t messageThreadId) const { + std::int32_t messageThreadId, + const std::string& emoji) const { std::vector args; - args.reserve(8); + args.reserve(9); args.emplace_back("chat_id", chatId); if (messageThreadId != 0) { @@ -1959,6 +2054,9 @@ Message::Ptr Api::sendSticker(boost::variant chatId, } else { // std::string args.emplace_back("sticker", boost::get(sticker)); } + if (!emoji.empty()) { + args.emplace_back("emoji", emoji); + } if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } @@ -1991,7 +2089,8 @@ std::vector Api::getCustomEmojiStickers(const std::vector args; args.reserve(1); - args.emplace_back("custom_emoji_ids", _tgTypeParser.parseArray([] (const std::string& customEmojiId) -> std::string { + args.emplace_back("custom_emoji_ids", _tgTypeParser.parseArray( + [] (const std::string& customEmojiId) -> std::string { return "\"" + StringTools::escapeJsonString(customEmojiId) + "\""; }, customEmojiIds)); @@ -1999,12 +2098,14 @@ std::vector Api::getCustomEmojiStickers(const std::vector args; - args.reserve(2); + args.reserve(3); args.emplace_back("user_id", userId); - args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); + args.emplace_back("sticker", sticker->data, true, sticker->mimeType, sticker->fileName); + args.emplace_back("sticker_format", stickerFormat); return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); } @@ -2012,70 +2113,37 @@ File::Ptr Api::uploadStickerFile(std::int64_t userId, bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, - const std::string& emojis, - MaskPosition::Ptr maskPosition, - boost::variant pngSticker, - InputFile::Ptr tgsSticker, - InputFile::Ptr webmSticker, - const std::string& stickerType) const { + const std::vector& stickers, + const std::string& stickerFormat, + const std::string& stickerType, + bool needsRepainting) const { std::vector args; - args.reserve(10); + args.reserve(7); args.emplace_back("user_id", userId); args.emplace_back("name", name); args.emplace_back("title", title); - if (pngSticker.which() == 0) { // InputFile::Ptr - auto file = boost::get(pngSticker); - args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else { // std::string - args.emplace_back("png_sticker", boost::get(pngSticker)); - } - if (tgsSticker != nullptr) { - args.emplace_back("tgs_sticker", tgsSticker->data, true, tgsSticker->mimeType, tgsSticker->fileName); - } - if (webmSticker != nullptr) { - args.emplace_back("webm_sticker", webmSticker->data, true, webmSticker->mimeType, webmSticker->fileName); - } + args.emplace_back("stickers", _tgTypeParser.parseArray(&TgTypeParser::parseInputSticker, stickers)); + args.emplace_back("sticker_format", stickerFormat); if (!stickerType.empty()) { args.emplace_back("sticker_type", stickerType); } - args.emplace_back("emojis", emojis); - if (maskPosition != nullptr) { - args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + if (needsRepainting) { + args.emplace_back("needs_repainting", needsRepainting); } return sendRequest("createNewStickerSet", args).get("", false); } bool Api::addStickerToSet(std::int64_t userId, - const std::string& name, - const std::string& emojis, - MaskPosition::Ptr maskPosition, - boost::variant pngSticker, - InputFile::Ptr tgsSticker, - InputFile::Ptr webmSticker) const { + const std::string& name, + InputSticker::Ptr sticker) const { std::vector args; - args.reserve(7); + args.reserve(3); args.emplace_back("user_id", userId); args.emplace_back("name", name); - - if (pngSticker.which() == 0) { // InputFile::Ptr - auto file = boost::get(pngSticker); - args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else { // std::string - args.emplace_back("png_sticker", boost::get(pngSticker)); - } - if (tgsSticker != nullptr) { - args.emplace_back("tgs_sticker", tgsSticker->data, true, tgsSticker->mimeType, tgsSticker->fileName); - } - if (webmSticker != nullptr) { - args.emplace_back("webm_sticker", webmSticker->data, true, webmSticker->mimeType, webmSticker->fileName); - } - args.emplace_back("emojis", emojis); - if (maskPosition != nullptr) { - args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); - } + args.emplace_back("sticker", _tgTypeParser.parseInputSticker(sticker)); return sendRequest("addStickerToSet", args).get("", false); } @@ -2100,22 +2168,102 @@ bool Api::deleteStickerFromSet(const std::string& sticker) const { return sendRequest("deleteStickerFromSet", args).get("", false); } -bool Api::setStickerSetThumb(const std::string& name, - std::int64_t userId, - boost::variant thumb) const { +bool Api::setStickerEmojiList(const std::string& sticker, + const std::vector& emojiList) const { + std::vector args; + args.reserve(2); + + args.emplace_back("sticker", sticker); + args.emplace_back("emoji_list", _tgTypeParser.parseArray( + [](const std::string& emoji)->std::string { + return "\"" + StringTools::escapeJsonString(emoji) + "\""; + }, emojiList)); + + return sendRequest("setStickerEmojiList", args).get("", false); +} + +bool Api::setStickerKeywords(const std::string& sticker, + const std::vector& keywords) const { + std::vector args; + args.reserve(2); + + args.emplace_back("sticker", sticker); + if (!keywords.empty()) { + args.emplace_back("keywords", _tgTypeParser.parseArray( + [](const std::string& keyword)->std::string { + return "\"" + StringTools::escapeJsonString(keyword) + "\""; + }, keywords)); + } + + return sendRequest("setStickerKeywords", args).get("", false); +} + +bool Api::setStickerMaskPosition(const std::string& sticker, + MaskPosition::Ptr maskPosition) const { + std::vector args; + args.reserve(2); + + args.emplace_back("sticker", sticker); + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } + + return sendRequest("setStickerMaskPosition", args).get("", false); +} + +bool Api::setStickerSetTitle(const std::string& name, + const std::string& title) const { + std::vector args; + args.reserve(2); + + args.emplace_back("name", name); + args.emplace_back("title", title); + + return sendRequest("setStickerSetTitle", args).get("", false); +} + +bool Api::setStickerSetThumbnail(const std::string& name, + std::int64_t userId, + boost::variant thumbnail) const { std::vector args; args.reserve(3); args.emplace_back("name", name); args.emplace_back("user_id", userId); - if (thumb.which() == 0) { // InputFile::Ptr - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + if (thumbnail.which() == 0) { // InputFile::Ptr + if (boost::get(thumbnail) != nullptr) { + auto file = boost::get(thumbnail); + args.emplace_back("thumbnail", file->data, true, file->mimeType, file->fileName); + } } else { // std::string - args.emplace_back("thumb", boost::get(thumb)); + if (boost::get(thumbnail) != "") { + args.emplace_back("thumbnail", boost::get(thumbnail)); + } + } + + return sendRequest("setStickerSetThumbnail", args).get("", false); +} + +bool Api::setCustomEmojiStickerSetThumbnail(const std::string& name, + const std::string& customEmojiId) const { + std::vector args; + args.reserve(2); + + args.emplace_back("name", name); + if (!customEmojiId.empty()) { + args.emplace_back("custom_emoji_id", customEmojiId); } - return sendRequest("setStickerSetThumb", args).get("", false); + return sendRequest("setCustomEmojiStickerSetThumbnail", args).get("", false); +} + +bool Api::deleteStickerSet(const std::string& name) const { + std::vector args; + args.reserve(1); + + args.emplace_back("name", name); + + return sendRequest("deleteStickerSet", args).get("", false); } bool Api::answerInlineQuery(const std::string& inlineQueryId, -- cgit v1.2.3