From 864a3aac28af33e8d86fc0bd223849081c0c28da Mon Sep 17 00:00:00 2001 From: royna2544 Date: Tue, 15 Oct 2024 16:49:46 +0900 Subject: [PATCH] Fix TgTypeParser of InlineQuery --- src/TgTypeParser.cpp | 46 +++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 14430ec7..2b98ef3b 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -1,12 +1,11 @@ -#include "tgbot/TgTypeParser.h" +#include +#include #include #include #include -#include "tgbot/types/InlineKeyboardMarkup.h" - namespace TgBot { // T should be instance of std::shared_ptr. @@ -3342,6 +3341,17 @@ DECLARE_PARSER_TO_JSON(InlineKeyboardButton) { return ptree; } +template +auto put(const InlineQueryResult::Ptr& ptr) { + if (const auto cached = std::dynamic_pointer_cast(ptr)) { + return put(cached); + } else if (const auto result = std::dynamic_pointer_cast(ptr)) { + return put(result); + } else { + throw TgBot::TgException("Invalid inline query result type", TgException::ErrorCode::Internal); + } +} + DECLARE_PARSER_TO_JSON(InlineQueryResult) { if (!object) return {}; JsonWrapper ptree; @@ -3349,26 +3359,10 @@ DECLARE_PARSER_TO_JSON(InlineQueryResult) { ptree.put("id", object->id); ptree.put("reply_markup", put(object->replyMarkup)); - if (object->type == InlineQueryResultCachedAudio::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedDocument::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedGif::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedPhoto::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedSticker::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedVideo::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultCachedVoice::TYPE) { - ptree.put("data", put(object)); - } else if (object->type == InlineQueryResultArticle::TYPE) { + if (object->type == InlineQueryResultArticle::TYPE) { ptree.put("data", put(object)); } else if (object->type == InlineQueryResultAudio::TYPE) { - ptree.put("data", put(object)); + ptree.put("data", put(object)); } else if (object->type == InlineQueryResultContact::TYPE) { ptree.put("data", put(object)); } else if (object->type == InlineQueryResultGame::TYPE) { @@ -3380,15 +3374,15 @@ DECLARE_PARSER_TO_JSON(InlineQueryResult) { } else if (object->type == InlineQueryResultVenue::TYPE) { ptree.put("data", put(object)); } else if (object->type == InlineQueryResultVoice::TYPE) { - ptree.put("data", put(object)); + ptree.put("data", put(object)); } else if (object->type == InlineQueryResultPhoto::TYPE) { - ptree.put("data", put(object)); + ptree.put("data", put(object)); } else if (object->type == InlineQueryResultGif::TYPE) { - ptree.put("data", put(object)); + ptree.put("data", put(object)); } else if (object->type == InlineQueryResultMpeg4Gif::TYPE) { - ptree.put("data", put(object)); + ptree.put("data", put(object)); } else if (object->type == InlineQueryResultVideo::TYPE) { - ptree.put("data", put(object)); + ptree.put("data", put(object)); } return ptree; }