From 34f110bcb625ad4af64a512c3a66e8509aaa7867 Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Tue, 27 Sep 2016 00:49:07 +0300 Subject: [PATCH] Send photo implemented. --- include/telebotxx/BotApi.hpp | 4 ++-- src/BotApi.cpp | 27 +++++++++++++++++---------- tests/TestApi.cpp | 7 +++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/telebotxx/BotApi.hpp b/include/telebotxx/BotApi.hpp index e93052b..f64e3f2 100644 --- a/include/telebotxx/BotApi.hpp +++ b/include/telebotxx/BotApi.hpp @@ -27,9 +27,9 @@ namespace telebotxx /// \brief Send image /// \param [in] chat chat identifier - /// \param [in] file opened image stream + /// \param [in] filename image location /// \param [in] caption optional photo caption - void sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption = ""); + void sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption = ""); private: diff --git a/src/BotApi.cpp b/src/BotApi.cpp index 9930c53..dea192d 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -164,35 +164,42 @@ public: parseResponse(doc); } - inline void sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption) + inline void sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption) { // Construct HTTP request curlpp::Easy request; - std::list headers; + std::stringstream responseStream; { // Forms takes ownership of pointers! curlpp::Forms formParts; formParts.push_back(new curlpp::FormParts::Content("chat_id", chat)); - formParts.push_back(new curlpp::FormParts::Content("photo", "value2")); - + formParts.push_back(new curlpp::FormParts::File("photo", filename)); + formParts.push_back(new curlpp::FormParts::Content("caption", caption)); request.setOpt(new curlpp::options::HttpPost(formParts)); } // Set options request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto")); request.setOpt(new curlpp::Options::Verbose(false)); - request.setOpt(new curlpp::options::HttpHeader(headers)); - request.setOpt(new curlpp::Options::Post(true)); + request.setOpt(new curlpp::options::WriteStream(&responseStream)); // Perform request request.perform(); + + BOOST_LOG_TRIVIAL(debug) << responseStream.str(); + + using namespace rapidjson; + IStreamWrapper isw(responseStream); + Document doc; + doc.ParseStream(isw); + + /// \todo Parse message + parseResponse(doc); } - private: - std::string token_; std::string telegramMainUrl_; User botUser_; @@ -215,7 +222,7 @@ void BotApi::sendMessage(const std::string& chat, const std::string& text) return impl_->sendMessage(chat, text); } -void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption) +void BotApi::sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption) { - return impl_->sendPhoto(chat, file, caption); + return impl_->sendPhoto(chat, filename, caption); } diff --git a/tests/TestApi.cpp b/tests/TestApi.cpp index e282087..b62e34b 100644 --- a/tests/TestApi.cpp +++ b/tests/TestApi.cpp @@ -86,4 +86,11 @@ BOOST_AUTO_TEST_SUITE(TestBotApi) BOOST_REQUIRE_NO_THROW(bot->sendMessage(chat, "Hello from C++!")); } + BOOST_AUTO_TEST_CASE(SendPhoto) + { + PRINT_TESTNAME; + BOOST_REQUIRE(bot); + BOOST_REQUIRE_NO_THROW(bot->sendPhoto(chat, photoFile, "Sample caption")); + } + BOOST_AUTO_TEST_SUITE_END()