Skip to content

Commit

Permalink
Fix TgTypeParser of InlineQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Oct 15, 2024
1 parent ed08e5c commit 44c6ca0
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/TgTypeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include <cstdint>
#include <utility>

#include "tgbot/TgException.h"
#include "tgbot/types/InlineKeyboardMarkup.h"
#include "tgbot/types/InlineQueryResult.h"
#include "tgbot/types/InlineQueryResultCachedPhoto.h"

namespace TgBot {

Expand Down Expand Up @@ -3342,33 +3345,28 @@ DECLARE_PARSER_TO_JSON(InlineKeyboardButton) {
return ptree;
}

template <typename T, typename CachedT>
auto put(const InlineQueryResult::Ptr& ptr) {
if (const auto cached = std::dynamic_pointer_cast<CachedT>(ptr)) {
return put<typename CachedT::Ptr>(cached);
} else if (const auto result = std::dynamic_pointer_cast<T>(ptr)) {
return put<typename T::Ptr>(result);
} else {
throw TgBot::TgException("Invalid inline query result type", TgException::ErrorCode::Internal);
}
}

DECLARE_PARSER_TO_JSON(InlineQueryResult) {
if (!object) return {};
JsonWrapper ptree;
ptree.put("type", object->type);
ptree.put("id", object->id);
ptree.put("reply_markup", put(object->replyMarkup));

if (object->type == InlineQueryResultCachedAudio::TYPE) {
ptree.put("data", put<InlineQueryResultCachedAudio>(object));
} else if (object->type == InlineQueryResultCachedDocument::TYPE) {
ptree.put("data", put<InlineQueryResultCachedDocument>(object));
} else if (object->type == InlineQueryResultCachedGif::TYPE) {
ptree.put("data", put<InlineQueryResultCachedGif>(object));
} else if (object->type == InlineQueryResultCachedMpeg4Gif::TYPE) {
ptree.put("data", put<InlineQueryResultCachedMpeg4Gif>(object));
} else if (object->type == InlineQueryResultCachedPhoto::TYPE) {
ptree.put("data", put<InlineQueryResultCachedPhoto>(object));
} else if (object->type == InlineQueryResultCachedSticker::TYPE) {
ptree.put("data", put<InlineQueryResultCachedSticker>(object));
} else if (object->type == InlineQueryResultCachedVideo::TYPE) {
ptree.put("data", put<InlineQueryResultCachedVideo>(object));
} else if (object->type == InlineQueryResultCachedVoice::TYPE) {
ptree.put("data", put<InlineQueryResultCachedVoice>(object));
} else if (object->type == InlineQueryResultArticle::TYPE) {
if (object->type == InlineQueryResultArticle::TYPE) {
ptree.put("data", put<InlineQueryResultArticle>(object));
} else if (object->type == InlineQueryResultAudio::TYPE) {
ptree.put("data", put<InlineQueryResultAudio>(object));
ptree.put("data", put<InlineQueryResultAudio, InlineQueryResultCachedAudio>(object));
} else if (object->type == InlineQueryResultContact::TYPE) {
ptree.put("data", put<InlineQueryResultContact>(object));
} else if (object->type == InlineQueryResultGame::TYPE) {
Expand All @@ -3380,15 +3378,15 @@ DECLARE_PARSER_TO_JSON(InlineQueryResult) {
} else if (object->type == InlineQueryResultVenue::TYPE) {
ptree.put("data", put<InlineQueryResultVenue>(object));
} else if (object->type == InlineQueryResultVoice::TYPE) {
ptree.put("data", put<InlineQueryResultVoice>(object));
ptree.put("data", put<InlineQueryResultVoice,InlineQueryResultCachedVoice>(object));
} else if (object->type == InlineQueryResultPhoto::TYPE) {
ptree.put("data", put<InlineQueryResultPhoto>(object));
ptree.put("data", put<InlineQueryResultPhoto, InlineQueryResultCachedPhoto>(object));
} else if (object->type == InlineQueryResultGif::TYPE) {
ptree.put("data", put<InlineQueryResultGif>(object));
ptree.put("data", put<InlineQueryResultGif, InlineQueryResultCachedGif>(object));
} else if (object->type == InlineQueryResultMpeg4Gif::TYPE) {
ptree.put("data", put<InlineQueryResultMpeg4Gif>(object));
ptree.put("data", put<InlineQueryResultMpeg4Gif, InlineQueryResultCachedMpeg4Gif>(object));
} else if (object->type == InlineQueryResultVideo::TYPE) {
ptree.put("data", put<InlineQueryResultVideo>(object));
ptree.put("data", put<InlineQueryResultVideo, InlineQueryResultCachedVideo>(object));
}
return ptree;
}
Expand Down

0 comments on commit 44c6ca0

Please sign in to comment.