From 44c6ca0db46525f5e0a9058731b37d35d303c949 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 | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 14430ec7..6bebb10f 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -5,7 +5,10 @@ #include #include +#include "tgbot/TgException.h" #include "tgbot/types/InlineKeyboardMarkup.h" +#include "tgbot/types/InlineQueryResult.h" +#include "tgbot/types/InlineQueryResultCachedPhoto.h" namespace TgBot { @@ -3342,6 +3345,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 +3363,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 +3378,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; }