diff --git a/include/telebotxx/BotApi.hpp b/include/telebotxx/BotApi.hpp index 300b29f..57cfc47 100644 --- a/include/telebotxx/BotApi.hpp +++ b/include/telebotxx/BotApi.hpp @@ -2,6 +2,7 @@ #define TELEBOTXX_BOTAPI_H #include +#include namespace telebotxx { @@ -11,6 +12,8 @@ namespace telebotxx /// \param [in] token bot's secret token BotApi(const std::string& token); + ~BotApi(); + /// \brief Send text message /// \param [in] chat chat identifier /// \param [in] text message text @@ -24,8 +27,8 @@ namespace telebotxx private: - std::string token_; - std::string telegramMainUrl_; + class Impl; + std::unique_ptr impl_; }; } diff --git a/src/BotApi.cpp b/src/BotApi.cpp index 9b7e76e..c4608e1 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -10,79 +10,103 @@ using namespace telebotxx; -BotApi::BotApi(const std::string& token) - : token_(token) +class BotApi::Impl { - telegramMainUrl_ = "https://api.telegram.org/bot" + token_; +public: + Impl(const std::string& token) + : token_(token) + { + telegramMainUrl_ = "https://api.telegram.org/bot" + token_; - /// \todo run getMe command to check token + /// \todo run getMe command to check token + } + + void sendMessage(const std::string& chat, const std::string& text) + { + // Construct JSON body and istream + using namespace rapidjson; + StringBuffer s; + Writer writer(s); + + writer.StartObject(); + writer.String("chat_id"); + writer.String(chat.c_str()); + writer.String("text"); + writer.String(text.c_str()); + writer.EndObject(); + + std::istringstream myStream(s.GetString()); + std::cout << myStream.str() << std::endl; + auto size = myStream.str().size(); + + // Construct HTTP request + curlpp::Easy request; + std::list headers; + + // Content-Type + headers.push_back("Content-Type: application/json"); + + // Content-Length + std::ostringstream ss; + ss << "Content-Length: " << size; + headers.push_back(ss.str()); + + // Set options + request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendMessage")); + request.setOpt(new curlpp::Options::Verbose(true)); + request.setOpt(new curlpp::Options::ReadStream(&myStream)); + request.setOpt(new curlpp::Options::InfileSize(size)); + request.setOpt(new curlpp::options::HttpHeader(headers)); + request.setOpt(new curlpp::Options::Post(true)); + + // Perform request + request.perform(); + } + + void sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption) + { + // Construct HTTP request + curlpp::Easy request; + std::list headers; + + { + // 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")); + + request.setOpt(new curlpp::options::HttpPost(formParts)); + } + + // Set options + request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto")); + request.setOpt(new curlpp::Options::Verbose(true)); + request.setOpt(new curlpp::options::HttpHeader(headers)); + request.setOpt(new curlpp::Options::Post(true)); + + // Perform request + request.perform(); + } + + +private: + std::string token_; + std::string telegramMainUrl_; +}; + +BotApi::BotApi(const std::string& token) + : impl_(std::make_unique(token)) +{ } -void BotApi::sendMessage(const std::string &chat, const std::string &text) +BotApi::~BotApi() = default; + +void BotApi::sendMessage(const std::string& chat, const std::string& text) { - // Construct JSON body and istream - using namespace rapidjson; - StringBuffer s; - Writer writer(s); - - writer.StartObject(); - writer.String("chat_id"); - writer.String(chat.c_str()); - writer.String("text"); - writer.String(text.c_str()); - writer.EndObject(); - - std::istringstream myStream(s.GetString()); - std::cout << myStream.str() << std::endl; - auto size = myStream.str().size(); - - // Construct HTTP request - curlpp::Easy request; - std::list headers; - - // Content-Type - headers.push_back("Content-Type: application/json"); - - // Content-Length - std::ostringstream ss; - ss << "Content-Length: " << size; - headers.push_back(ss.str()); - - // Set options - request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendMessage")); - request.setOpt(new curlpp::Options::Verbose(true)); - request.setOpt(new curlpp::Options::ReadStream(&myStream)); - request.setOpt(new curlpp::Options::InfileSize(size)); - request.setOpt(new curlpp::options::HttpHeader(headers)); - request.setOpt(new curlpp::Options::Post(true)); - - // Perform request - request.perform(); + return impl_->sendMessage(chat, text); } void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption) { - // Construct HTTP request - curlpp::Easy request; - std::list headers; - - { - // 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")); - - request.setOpt(new curlpp::options::HttpPost(formParts)); - } - - // Set options - request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto")); - request.setOpt(new curlpp::Options::Verbose(true)); - request.setOpt(new curlpp::options::HttpHeader(headers)); - request.setOpt(new curlpp::Options::Post(true)); - - // Perform request - request.perform(); + return impl_->sendPhoto(chat, file, caption); } - -