From 8ee1cc507c54d93ccddcab53b2f561f0ff677301 Mon Sep 17 00:00:00 2001 From: Bader Date: Sat, 14 Oct 2023 14:19:46 +0100 Subject: [PATCH] implemented Api forum topic methods --- include/tgbotxx/Api.hpp | 57 ++++++++++++++++++++++++++++++++++++++++- src/Api.cpp | 40 +++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/include/tgbotxx/Api.hpp b/include/tgbotxx/Api.hpp index d34b36bc2..d5e63cc50 100644 --- a/include/tgbotxx/Api.hpp +++ b/include/tgbotxx/Api.hpp @@ -1152,7 +1152,7 @@ namespace tgbotxx { /// @brief Use this method to create a topic in a forum supergroup chat. - /// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights. + /// The bot must be an administrator in the chat for this to work and must have the canManageTopics administrator rights. /// https://core.telegram.org/bots/api#forumtopic /// @param chatId Integer Unique identifier for the target chat or username of the target channel (in the format \@channelusername) /// @param name Topic name, 1-128 characters @@ -1168,6 +1168,61 @@ namespace tgbotxx { const std::string& iconCustomEmojiId = "") const; + /// @brief Use this method to edit name and icon of a topic in a forum supergroup chat. + /// The bot must be an administrator in the chat for this to work and must have the canManageTopics administrator rights. + /// https://core.telegram.org/bots/api#forumtopic + /// @param chatId Integer Unique identifier for the target chat or username of the target channel (in the format \@channelusername) + /// @param messageThreadId Unique identifier for the target message thread of the forum topic + /// @param name Optional. New topic name, 0-128 characters. If not specified or empty, the current name of the topic will be kept + /// @param iconCustomEmojiId Optional. New unique identifier of the custom emoji shown as the topic icon. + /// Use getForumTopicIconStickers to get all allowed custom emoji identifiers. + /// Pass an empty string to remove the icon. If not specified, the current icon will be kept + /// @returns True on success. + /// @throws Exception on failure + /// @relatedalso createForumTopic closeForumTopic reopenForumTopic deleteForumTopic + /// @ref https://core.telegram.org/bots/api#editforumtopic + bool editForumTopic(std::int64_t chatId, + std::int32_t messageThreadId, + const std::string& name, + const std::optional& iconCustomEmojiId = std::nullopt) const; + + + /// @brief Use this method to close an open topic in a forum supergroup chat. + /// The bot must be an administrator in the chat for this to work and must have the canManageTopics administrator rights. + /// https://core.telegram.org/bots/api#forumtopic + /// @param chatId Integer Unique identifier for the target chat or username of the target channel (in the format \@channelusername) + /// @param messageThreadId Unique identifier for the target message thread of the forum topic + /// @returns True on success. + /// @throws Exception on failure + /// @relatedalso createForumTopic editForumTopic reopenForumTopic deleteForumTopic + /// @ref https://core.telegram.org/bots/api#closeforumtopic + bool closeForumTopic(std::int64_t chatId, std::int32_t messageThreadId) const; + + + /// @brief Use this method to reopen a closed topic in a forum supergroup chat. + /// The bot must be an administrator in the chat for this to work and must have the canManageTopics administrator rights. + /// https://core.telegram.org/bots/api#forumtopic + /// @param chatId Integer Unique identifier for the target chat or username of the target channel (in the format \@channelusername) + /// @param messageThreadId Unique identifier for the target message thread of the forum topic + /// @returns True on success. + /// @throws Exception on failure + /// @relatedalso createForumTopic editForumTopic closeForumTopic deleteForumTopic + /// @ref https://core.telegram.org/bots/api#reopenforumtopic + bool reopenForumTopic(std::int64_t chatId, std::int32_t messageThreadId) const; + + + /// @brief Use this method to delete a forum topic along with all its messages in a forum supergroup chat. + /// The bot must be an administrator in the chat for this to work and must have the canManageTopics administrator rights. + /// https://core.telegram.org/bots/api#forumtopic + /// @param chatId Integer Unique identifier for the target chat or username of the target channel (in the format \@channelusername) + /// @param messageThreadId Unique identifier for the target message thread of the forum topic + /// @returns True on success. + /// @throws Exception on failure + /// @relatedalso createForumTopic editForumTopic closeForumTopic reopenForumTopic + /// @ref https://core.telegram.org/bots/api#deleteforumtopic + bool deleteForumTopic(std::int64_t chatId, std::int32_t messageThreadId) const; + + /// @brief Use this method to remove webhook integration if you decide to switch back to getUpdates. /// @param dropPendingUpdates: Pass True to drop all pending updates. /// @returns True on success. diff --git a/src/Api.cpp b/src/Api.cpp index 43eb80ff0..3d0902465 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -1356,3 +1356,43 @@ Ptr Api::createForumTopic(std::int64_t chatId, Ptr forumTopic(new ForumTopic(forumTopicObj)); return forumTopic; } + +bool Api::editForumTopic(std::int64_t chatId, + std::int32_t messageThreadId, + const std::string& name, + const std::optional& iconCustomEmojiId) const { + cpr::Multipart data{}; + data.parts.reserve(4); + data.parts.emplace_back("chat_id", std::to_string(chatId)); + data.parts.emplace_back("message_thread_id", messageThreadId); + if (not name.empty()) + data.parts.emplace_back("name", name); + if (iconCustomEmojiId.has_value()) { + data.parts.emplace_back("icon_custom_emoji_id", *iconCustomEmojiId); + } + return sendRequest("editForumTopic", data); +} + +bool Api::closeForumTopic(std::int64_t chatId, std::int32_t messageThreadId) const { + cpr::Multipart data{}; + data.parts.reserve(2); + data.parts.emplace_back("chat_id", std::to_string(chatId)); + data.parts.emplace_back("message_thread_id", messageThreadId); + return sendRequest("closeForumTopic", data); +} + +bool Api::reopenForumTopic(std::int64_t chatId, std::int32_t messageThreadId) const { + cpr::Multipart data{}; + data.parts.reserve(2); + data.parts.emplace_back("chat_id", std::to_string(chatId)); + data.parts.emplace_back("message_thread_id", messageThreadId); + return sendRequest("reopenForumTopic", data); +} + +bool Api::deleteForumTopic(std::int64_t chatId, std::int32_t messageThreadId) const { + cpr::Multipart data{}; + data.parts.reserve(2); + data.parts.emplace_back("chat_id", std::to_string(chatId)); + data.parts.emplace_back("message_thread_id", messageThreadId); + return sendRequest("deleteForumTopic", data); +} \ No newline at end of file