Send photo implemented.

This commit is contained in:
Kirill Kirilenko 2016-09-27 00:49:07 +03:00
parent f58208f864
commit 34f110bcb6
3 changed files with 26 additions and 12 deletions

View file

@ -27,9 +27,9 @@ namespace telebotxx
/// \brief Send image /// \brief Send image
/// \param [in] chat chat identifier /// \param [in] chat chat identifier
/// \param [in] file opened image stream /// \param [in] filename image location
/// \param [in] caption optional photo caption /// \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: private:

View file

@ -164,35 +164,42 @@ public:
parseResponse(doc); 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 // Construct HTTP request
curlpp::Easy request; curlpp::Easy request;
std::list<std::string> headers; std::stringstream responseStream;
{ {
// Forms takes ownership of pointers! // Forms takes ownership of pointers!
curlpp::Forms formParts; curlpp::Forms formParts;
formParts.push_back(new curlpp::FormParts::Content("chat_id", chat)); 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)); request.setOpt(new curlpp::options::HttpPost(formParts));
} }
// Set options // Set options
request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto")); request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto"));
request.setOpt(new curlpp::Options::Verbose(false)); request.setOpt(new curlpp::Options::Verbose(false));
request.setOpt(new curlpp::options::HttpHeader(headers)); request.setOpt(new curlpp::options::WriteStream(&responseStream));
request.setOpt(new curlpp::Options::Post(true));
// Perform request // Perform request
request.perform(); 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: private:
std::string token_; std::string token_;
std::string telegramMainUrl_; std::string telegramMainUrl_;
User botUser_; User botUser_;
@ -215,7 +222,7 @@ void BotApi::sendMessage(const std::string& chat, const std::string& text)
return impl_->sendMessage(chat, 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);
} }

View file

@ -86,4 +86,11 @@ BOOST_AUTO_TEST_SUITE(TestBotApi)
BOOST_REQUIRE_NO_THROW(bot->sendMessage(chat, "Hello from C++!")); 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() BOOST_AUTO_TEST_SUITE_END()