diff --git a/include/telebotxx/BotApi.hpp b/include/telebotxx/BotApi.hpp index c21a939..486af9e 100644 --- a/include/telebotxx/BotApi.hpp +++ b/include/telebotxx/BotApi.hpp @@ -81,6 +81,7 @@ public: /// \param limit maximum number of updates to be retrieved /// \param timeout timeout in seconds for long polling /// \return Updates (vector of Update) + /// \todo allowed_updates Updates getUpdates(int offset = 0, unsigned short limit = 0, unsigned timeout = 0); private: diff --git a/include/telebotxx/Update.hpp b/include/telebotxx/Update.hpp index d671da2..0703021 100644 --- a/include/telebotxx/Update.hpp +++ b/include/telebotxx/Update.hpp @@ -14,7 +14,6 @@ public: enum class Type { Message, - EditedMessage, InlineQuery, ChosenInlineResult, CallbackQuery @@ -41,34 +40,30 @@ using Updates = std::vector; class MessageUpdate : public Update { public: - MessageUpdate(int id, const Message& message); + enum class MessageType + { + Message, + EditedMessage, + ChannelPost, + EditedChannelPost + }; + + MessageUpdate(int id, MessageType type, const Message& message); MessageUpdate(const MessageUpdate&); MessageUpdate(MessageUpdate&&); ~MessageUpdate(); + MessageType getMessageType() const; const Message& getMessage() const; void swap(MessageUpdate& other) noexcept; - const MessageUpdate& operator=(MessageUpdate other); private: + MessageType type_; Message message_; }; -class EditedMessageUpdate : public MessageUpdate -{ -public: - EditedMessageUpdate(int id, const Message& message); - EditedMessageUpdate(const EditedMessageUpdate&); - EditedMessageUpdate(EditedMessageUpdate&&); - ~EditedMessageUpdate(); - - void swap(EditedMessageUpdate& other) noexcept; - - const EditedMessageUpdate& operator=(EditedMessageUpdate other); -}; - } #endif // TELEBOTXX_UPDATE_HPP diff --git a/src/JsonObjects.cpp b/src/JsonObjects.cpp index ff6c99f..00fe82f 100644 --- a/src/JsonObjects.cpp +++ b/src/JsonObjects.cpp @@ -249,18 +249,23 @@ std::unique_ptr parseChat(const rapidjson::Value& parent, const char* name return nullptr; } +/// \todo Other updates std::unique_ptr parseUpdate(const rapidjson::Value& obj) { int id = parse(obj, "update_id", REQUIRED); std::unique_ptr message; if ((message = parseMessage(obj, "message", OPTIONAL))) - return std::make_unique(id, *message); + return std::make_unique(id, MessageUpdate::MessageType::Message, *message); else if ((message = parseMessage(obj, "edited_message", OPTIONAL))) - return std::make_unique(id, *message); - /// \todo: other updates + return std::make_unique(id, MessageUpdate::MessageType::EditedMessage, *message); + else if ((message = parseMessage(obj, "channel_post", OPTIONAL))) + return std::make_unique(id, MessageUpdate::MessageType::ChannelPost, *message); + else if ((message = parseMessage(obj, "edited_channel_post", OPTIONAL))) + return std::make_unique(id, MessageUpdate::MessageType::EditedChannelPost, *message); else - throw ParseError("Unknown update type"); + ///throw ParseError("Unknown update type"); + return nullptr; } std::unique_ptr parseUpdates(const rapidjson::Value& parent, const char* name, bool required) @@ -269,12 +274,9 @@ std::unique_ptr parseUpdates(const rapidjson::Value& parent, const char auto& obj = parseArray(parent, name, required, found); if (found) { - std::vector updates; + Updates updates; for (auto& elem : obj.GetArray()) - { - auto update = parseUpdate(elem); - updates.emplace_back(std::move(update)); - } + updates.emplace_back(parseUpdate(elem)); return std::make_unique(std::move(updates)); } else diff --git a/src/Update.cpp b/src/Update.cpp index 509b9eb..303192e 100644 --- a/src/Update.cpp +++ b/src/Update.cpp @@ -31,9 +31,8 @@ void Update::swap(Update& other) noexcept //////////////////////////////////////////////////////////////// -MessageUpdate::MessageUpdate(int id, const Message& message) - : Update(id, Type::Message), - message_(message) +MessageUpdate::MessageUpdate(int id, MessageType type, const Message& message) + : Update(id, Type::Message), type_(type), message_(message) { } @@ -41,6 +40,11 @@ MessageUpdate::MessageUpdate(const MessageUpdate&) = default; MessageUpdate::MessageUpdate(MessageUpdate&&) = default; MessageUpdate::~MessageUpdate() = default; +MessageUpdate::MessageType MessageUpdate::getMessageType() const +{ + return type_; +} + const Message& MessageUpdate::getMessage() const { return message_; @@ -59,26 +63,4 @@ const MessageUpdate& MessageUpdate::operator=(MessageUpdate other) return *this; } -//////////////////////////////////////////////////////////////// - -EditedMessageUpdate::EditedMessageUpdate(int id, const Message& message) - : MessageUpdate(id, message) -{ -} - -EditedMessageUpdate::EditedMessageUpdate(const EditedMessageUpdate&) = default; -EditedMessageUpdate::EditedMessageUpdate(EditedMessageUpdate&&) = default; -EditedMessageUpdate::~EditedMessageUpdate() = default; - -void EditedMessageUpdate::swap(EditedMessageUpdate& other) noexcept -{ - MessageUpdate::swap(other); -} - -const EditedMessageUpdate& EditedMessageUpdate::operator=(EditedMessageUpdate other) -{ - swap(other); - return *this; -} - } diff --git a/tests/TestApi.cpp b/tests/TestApi.cpp index a64a964..ef9a67f 100644 --- a/tests/TestApi.cpp +++ b/tests/TestApi.cpp @@ -206,12 +206,16 @@ BOOST_AUTO_TEST_SUITE(TestBotApi) BOOST_REQUIRE_NO_THROW(updates = bot->getUpdates()); for (auto update : updates) { - if (update->getType() == Update::Type::Message) + switch (update->getType()) { - auto& message = std::dynamic_pointer_cast(update)->getMessage(); - if (message.getFrom()) - std::cout << *message.getFrom() << ": "; - std::cout << message.getText() << std::endl; + case Update::Type::Message: + { + auto& message = std::dynamic_pointer_cast(update)->getMessage(); + if (message.getFrom()) + std::cout << *message.getFrom() << ": "; + std::cout << message.getText() << std::endl; + break; + } } } }