diff --git a/include/Telegram/TelegramBot.h b/include/Telegram/TelegramBot.h index 6c23145..32c1216 100644 --- a/include/Telegram/TelegramBot.h +++ b/include/Telegram/TelegramBot.h @@ -60,6 +60,7 @@ namespace Telegram { void init(); void apiRequest(std::string, std::map); Json::Value apiRequestJson(std::string, std::map); + Json::Value apiRequestFile(std::string, std::string, std::string); std::string processCommand(std::string); }; } diff --git a/src/TelegramBot.cc b/src/TelegramBot.cc index 16e1f89..0820e7c 100644 --- a/src/TelegramBot.cc +++ b/src/TelegramBot.cc @@ -138,7 +138,13 @@ Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, std::string params["chat_id"] = chat_id; params["photo"] = URL; - obj = this->apiRequestJson("sendPhoto", params); + if ((URL.substr(0, 8) == "https://") || (URL.substr(0, 7) == "http://")) { + obj = this->apiRequestJson("sendPhoto", params); + } + + if (URL.substr(0, 7) == "file://") { + obj = this->apiRequestFile(URL.substr(7, std::string::npos), "photo", chat_id); + } return(new Telegram::Message(obj["result"])); } @@ -206,6 +212,8 @@ Json::Value Telegram::TelegramBot::apiRequestJson(std::string method, std::map header; header.push_back("Content-Type: application/json"); @@ -235,6 +243,51 @@ Json::Value Telegram::TelegramBot::apiRequestJson(std::string method, std::mapapi_url + "send" + type)); + { + cURLpp::Forms parts; + parts.push_back(new cURLpp::FormParts::Content("chat_id", chat_id)); + parts.push_back(new cURLpp::FormParts::File(type, filename)); + handle.setOpt(cURLpp::Options::HttpPost(parts)); + } + + handle.perform(); + + } + catch(cURLpp::LogicError &e) { + Log(e.what()); + } + catch(cURLpp::RuntimeError &e) { + Log(e.what()); + } + catch(std::exception &e) { + Log(e.what()); + } + + Json::Reader jreader; + Json::Value obj; + jreader.parse(result.str(), obj); + + return (obj);} + std::string Telegram::TelegramBot::processCommand(std::string cmd) { // Process command