Sending photo by URL implemented.

This commit is contained in:
Kirill Kirilenko 2016-11-08 14:29:40 +03:00
parent d5fce8062a
commit a4654b8b23
4 changed files with 64 additions and 1 deletions

View file

@ -39,6 +39,11 @@ namespace telebotxx
/// \param [in] caption optional photo caption /// \param [in] caption optional photo caption
void sendPhoto(const std::string& chat, const std::string& filename, const std::string& 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: private:
class Impl; class Impl;

View file

@ -160,6 +160,41 @@ public:
parseResponse(doc); 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<StringBuffer> 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: private:
std::string token_; 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); 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);
}

View file

@ -16,6 +16,7 @@ const std::string CONFIG_FILE_NAME = "config.json";
std::string token; std::string token;
std::string chat; std::string chat;
std::string photoFile; std::string photoFile;
std::string photoUrl;
struct TestConfig struct TestConfig
{ {
@ -63,6 +64,15 @@ struct TestConfig
else else
throw std::invalid_argument("Config error: 'photo' not set."); 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); telebotxx::setDebugMode(true);
} }
@ -114,4 +124,11 @@ BOOST_AUTO_TEST_SUITE(TestBotApi)
BOOST_REQUIRE_NO_THROW(bot->sendPhoto(chat, photoFile, "Sample caption")); 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() BOOST_AUTO_TEST_SUITE_END()

View file

@ -1,5 +1,6 @@
{ {
"token": "123456789:AAEXK65dnCM-pEX9j4pjqlI3N6YQGRFjoCQ", "token": "123456789:AAEXK65dnCM-pEX9j4pjqlI3N6YQGRFjoCQ",
"chat": "@telebotxx_test", "chat": "@telebotxx_test",
"photo": "photo.png" "photo": "photo.png",
"photoURL": "https://files.ultracoder.org/Atlas_Pbody.jpg"
} }