From a4654b8b23b9198f2324d8360ad67a0870fe6a92 Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Tue, 8 Nov 2016 14:29:40 +0300 Subject: [PATCH] Sending photo by URL implemented. --- include/telebotxx/BotApi.hpp | 5 +++++ src/BotApi.cpp | 40 ++++++++++++++++++++++++++++++++++++ tests/TestApi.cpp | 17 +++++++++++++++ tests/config.json.example | 3 ++- 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/include/telebotxx/BotApi.hpp b/include/telebotxx/BotApi.hpp index ad7a296..3aca14a 100644 --- a/include/telebotxx/BotApi.hpp +++ b/include/telebotxx/BotApi.hpp @@ -39,6 +39,11 @@ namespace telebotxx /// \param [in] caption optional photo caption void sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption = ""); + /// \brief Send image by URL + /// \param [in] chat chat identifier + /// \param [in] url image URL + /// \param [in] caption optional photo caption + void sendPhotoUrl(const std::string& chat, const std::string& url, const std::string& caption = ""); private: class Impl; diff --git a/src/BotApi.cpp b/src/BotApi.cpp index f6e921d..27b6750 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -160,6 +160,41 @@ public: parseResponse(doc); } + inline void sendPhotoUrl(const std::string& chat, const std::string& url, const std::string& caption) + { + // Construct JSON body + using namespace rapidjson; + StringBuffer s; + Writer writer(s); + + writer.StartObject(); + writer.String("chat_id"); + writer.String(chat.c_str()); + writer.String("photo"); + writer.String(url.c_str()); + writer.String("caption"); + writer.String(caption.c_str()); + writer.EndObject(); + + std::string request = s.GetString(); + + auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"}, + cpr::Header{{"Content-Type", "application/json"}}, + cpr::Body{request} + ); + auto& response = r.text; + + if (debugMode) + std::cout << "Response: " << response << std::endl; + + using namespace rapidjson; + Document doc; + doc.Parse(response.c_str()); + + /// \todo Parse message + parseResponse(doc); + } + private: std::string token_; @@ -188,3 +223,8 @@ void BotApi::sendPhoto(const std::string& chat, const std::string& filename, con { return impl_->sendPhoto(chat, filename, caption); } + +void BotApi::sendPhotoUrl(const std::string& chat, const std::string& url, const std::string& caption) +{ + return impl_->sendPhotoUrl(chat, url, caption); +} diff --git a/tests/TestApi.cpp b/tests/TestApi.cpp index a0b2e73..f8fca9a 100644 --- a/tests/TestApi.cpp +++ b/tests/TestApi.cpp @@ -16,6 +16,7 @@ const std::string CONFIG_FILE_NAME = "config.json"; std::string token; std::string chat; std::string photoFile; +std::string photoUrl; struct TestConfig { @@ -63,6 +64,15 @@ struct TestConfig else throw std::invalid_argument("Config error: 'photo' not set."); + // Read photo URL + if (document.HasMember("photoURL")) + if (document["photoURL"].IsString()) + photoUrl = document["photoURL"].GetString(); + else + throw std::invalid_argument("Config error: 'photoURL' must be unsigned int value."); + else + throw std::invalid_argument("Config error: 'photoURL' not set."); + telebotxx::setDebugMode(true); } @@ -114,4 +124,11 @@ BOOST_AUTO_TEST_SUITE(TestBotApi) BOOST_REQUIRE_NO_THROW(bot->sendPhoto(chat, photoFile, "Sample caption")); } + BOOST_AUTO_TEST_CASE(SendPhotoUrl) + { + PRINT_TESTNAME; + BOOST_REQUIRE(bot); + BOOST_REQUIRE_NO_THROW(bot->sendPhotoUrl(chat, photoUrl, "Sample caption")); + } + BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/config.json.example b/tests/config.json.example index 4ad4b95..6d7cdae 100644 --- a/tests/config.json.example +++ b/tests/config.json.example @@ -1,5 +1,6 @@ { "token": "123456789:AAEXK65dnCM-pEX9j4pjqlI3N6YQGRFjoCQ", "chat": "@telebotxx_test", - "photo": "photo.png" + "photo": "photo.png", + "photoURL": "https://files.ultracoder.org/Atlas_Pbody.jpg" }