From 4c15e4c8dea1d661c2bad15e19e483d8ca925c3f Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Sun, 19 Feb 2017 20:21:59 +0300 Subject: [PATCH] Added InlineKeyboard support for sendPhoto request. --- include/telebotxx/SendPhotoRequest.hpp | 2 + src/CMakeLists.txt | 1 + src/ReplyMarkup.cpp | 66 ++++++++++++++++++++++++++ src/ReplyMarkup.hpp | 14 ++++++ src/SendMessageRequest.cpp | 60 +---------------------- src/SendPhotoRequest.cpp | 28 +++++++++++ tests/TestApi.cpp | 15 ++++++ 7 files changed, 128 insertions(+), 58 deletions(-) create mode 100644 src/ReplyMarkup.cpp create mode 100644 src/ReplyMarkup.hpp diff --git a/include/telebotxx/SendPhotoRequest.hpp b/include/telebotxx/SendPhotoRequest.hpp index c07e3e9..11cfaa9 100644 --- a/include/telebotxx/SendPhotoRequest.hpp +++ b/include/telebotxx/SendPhotoRequest.hpp @@ -18,10 +18,12 @@ public: void setCaption(const Caption& caption); void setDisableNotification(const DisableNotification& disableNotification); void setReplyToMessageId(const ReplyTo& replyToMessageId); + void setReplyMarkup(const ReplyMarkup& replyMarkup); void setOption(const Caption& caption); void setOption(const DisableNotification& disableNotification); void setOption(const ReplyTo& replyToMessageId); + void setOption(const ReplyMarkup& replyMarkup); Message execute(); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f8c2f7b..bec42d2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ set(SOURCE_FILES Attachment.cpp Message.cpp Update.cpp User.cpp + ReplyMarkup.cpp RequestOptions.cpp SendMessageRequest.cpp SendPhotoRequest.cpp diff --git a/src/ReplyMarkup.cpp b/src/ReplyMarkup.cpp new file mode 100644 index 0000000..50f5d81 --- /dev/null +++ b/src/ReplyMarkup.cpp @@ -0,0 +1,66 @@ +#include "ReplyMarkup.hpp" + +namespace telebotxx { + +void writeInlineKeyboardButton(rapidjson::Writer& writer, const InlineKeyboardButton& button) +{ + writer.StartObject(); + + writer.String("text"); + writer.String(button.getText().c_str()); + + switch (button.getActionType()) + { + case InlineKeyboardButton::ActionType::Url: + { + writer.String("url"); + writer.String(button.getUrl().getValue().c_str()); + break; + } + case InlineKeyboardButton::ActionType::CallbackData: + { + writer.String("callback_data"); + writer.String(button.getCallbackData().getValue().c_str()); + break; + } + case InlineKeyboardButton::ActionType::SwitchInlineQuery: + { + writer.String("switch_inline_query"); + writer.String(button.getSwitchInlineQuery().getValue().c_str()); + break; + } + case InlineKeyboardButton::ActionType::SwitchInlineQueryCurrentChat: + { + writer.String("switch_inline_query_current_chat"); + writer.String(button.getSwitchInlineQueryCurrentChat().getValue().c_str()); + break; + } + } + + writer.EndObject(); +} + +void writeInlineKeyboardMarkup(rapidjson::Writer& writer, const InlineKeyboardMarkup& markup) +{ + writer.StartObject(); + writer.String("inline_keyboard"); + writer.StartArray(); + auto rows = markup.getRows(); + for (auto& row : rows) + { + writer.StartArray(); + for (auto& button : row) + writeInlineKeyboardButton(writer, button); + writer.EndArray(); + } + writer.EndArray(); + writer.EndObject(); +} + +void writeReplyMarkup(rapidjson::Writer& writer, const ReplyMarkup& markup) +{ + if (markup.getType() == ReplyMarkup::Type::InlineKeyboardMarkup) + writeInlineKeyboardMarkup(writer, markup.getInlineKeyboardMarkup()); +} + +} diff --git a/src/ReplyMarkup.hpp b/src/ReplyMarkup.hpp new file mode 100644 index 0000000..eccb337 --- /dev/null +++ b/src/ReplyMarkup.hpp @@ -0,0 +1,14 @@ +#ifndef TELEBOTXX_REPLY_MARKUP_HPP +#define TELEBOTXX_REPLY_MARKUP_HPP + +#include + +#include + +namespace telebotxx { + +void writeReplyMarkup(rapidjson::Writer& writer, const ReplyMarkup& markup); + +} + +#endif // TELEBOTXX_REPLY_MARKUP_HPP diff --git a/src/SendMessageRequest.cpp b/src/SendMessageRequest.cpp index 4880ce1..faa77a4 100644 --- a/src/SendMessageRequest.cpp +++ b/src/SendMessageRequest.cpp @@ -1,6 +1,7 @@ #include #include #include "JsonObjects.hpp" +#include "ReplyMarkup.hpp" #include #include @@ -11,44 +12,6 @@ namespace telebotxx { -void writeInlineKeyboardButton(rapidjson::Writer& writer, const InlineKeyboardButton& button) -{ - writer.StartObject(); - - writer.String("text"); - writer.String(button.getText().c_str()); - - switch (button.getActionType()) - { - case InlineKeyboardButton::ActionType::Url: - { - writer.String("url"); - writer.String(button.getUrl().getValue().c_str()); - break; - } - case InlineKeyboardButton::ActionType::CallbackData: - { - writer.String("callback_data"); - writer.String(button.getCallbackData().getValue().c_str()); - break; - } - case InlineKeyboardButton::ActionType::SwitchInlineQuery: - { - writer.String("switch_inline_query"); - writer.String(button.getSwitchInlineQuery().getValue().c_str()); - break; - } - case InlineKeyboardButton::ActionType::SwitchInlineQueryCurrentChat: - { - writer.String("switch_inline_query_current_chat"); - writer.String(button.getSwitchInlineQueryCurrentChat().getValue().c_str()); - break; - } - } - - writer.EndObject(); -} - class SendMessageRequest::Impl { public: @@ -134,26 +97,7 @@ public: if (replyMarkup_) { writer.String("reply_markup"); - writer.StartObject(); - writer.String("inline_keyboard"); - if (replyMarkup_->getType() == ReplyMarkup::Type::InlineKeyboardMarkup) - { - writer.StartArray(); - auto rows = replyMarkup_->getInlineKeyboardMarkup().getRows(); - for (auto& row : rows) - { - writer.StartArray(); - for (auto& button : row) - { - writeInlineKeyboardButton(writer, button); - } - writer.EndArray(); - } - writer.EndArray(); - } - else - writer.String(chatId_.getUsername().c_str()); - writer.EndObject(); + writeReplyMarkup(writer, *replyMarkup_); } writer.EndObject(); diff --git a/src/SendPhotoRequest.cpp b/src/SendPhotoRequest.cpp index 6fadf10..001dfeb 100644 --- a/src/SendPhotoRequest.cpp +++ b/src/SendPhotoRequest.cpp @@ -1,6 +1,7 @@ #include #include #include "JsonObjects.hpp" +#include "ReplyMarkup.hpp" #include #include @@ -34,6 +35,11 @@ public: replyToMessageId_ = replyToMessageId; } + void setReplyMarkup(const ReplyMarkup& replyMarkup) + { + replyMarkup_ = replyMarkup; + } + Message execute() { cpr::Multipart multipart{}; @@ -71,6 +77,17 @@ public: if (replyToMessageId_) multipart.parts.push_back({"reply_to_message_id", replyToMessageId_->value()}); + // Add reply_markup + if (replyMarkup_) + { + // Construct JSON body + using namespace rapidjson; + StringBuffer s; + Writer writer(s); + writeReplyMarkup(writer, *replyMarkup_); + multipart.parts.push_back({"reply_markup", s.GetString()}); + } + auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"}, multipart); auto& response = r.text; @@ -92,6 +109,7 @@ private: boost::optional caption_; boost::optional disableNotification_; boost::optional replyToMessageId_; + boost::optional replyMarkup_; }; SendPhotoRequest::SendPhotoRequest(const std::string& telegramMainUrl, const ChatId& chat, const Photo& photo) @@ -118,6 +136,11 @@ void SendPhotoRequest::setReplyToMessageId(const ReplyTo& replyToMessageId) impl_->setReplyToMessageId(replyToMessageId); } +void SendPhotoRequest::setReplyMarkup(const ReplyMarkup& replyMarkup) +{ + impl_->setReplyMarkup(replyMarkup); +} + void SendPhotoRequest::setOption(const Caption& caption) { setCaption(caption); @@ -133,6 +156,11 @@ void SendPhotoRequest::setOption(const ReplyTo& replyToMessageId) setReplyToMessageId(replyToMessageId); } +void SendPhotoRequest::setOption(const ReplyMarkup& replyMarkup) +{ + setReplyMarkup(replyMarkup); +} + Message SendPhotoRequest::execute() { return impl_->execute(); diff --git a/tests/TestApi.cpp b/tests/TestApi.cpp index 36521e5..28cec8b 100644 --- a/tests/TestApi.cpp +++ b/tests/TestApi.cpp @@ -214,6 +214,21 @@ BOOST_AUTO_TEST_SUITE(TestSend) )); } + BOOST_AUTO_TEST_CASE(SendPhotoWithInlineKeyboard) + { + PRINT_TESTNAME; + BOOST_REQUIRE(bot); + InlineKeyboardMarkup markup; + markup.addRow(InlineKeyboardButtonRow{ + InlineKeyboardButton("Google", Url{"http://google.com/"}) + }); + BOOST_REQUIRE_NO_THROW(bot->sendPhoto(chat, + Photo{File{photoFile}}, + Caption{"Photo with inline keyboard"}, + ReplyMarkup{markup} + )); + } + BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE(TestParse)