From c67424862339c7eeeb0d810f8a88fd1447cb93a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hauke=20Z=C3=BChl?= Date: Mon, 5 Jun 2017 19:36:14 +0200 Subject: [PATCH] apiRequestJson now returns Message --- include/Telegram/TelegramBot.h | 6 +-- src/TelegramBot.cc | 67 +++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/include/Telegram/TelegramBot.h b/include/Telegram/TelegramBot.h index 8e48f73..ca422dd 100644 --- a/include/Telegram/TelegramBot.h +++ b/include/Telegram/TelegramBot.h @@ -41,8 +41,8 @@ namespace Telegram { void sendMessage(std::string, Json::Int64); void sendMessage(std::string, std::string); - void sendPhoto(std::string, Json::Int64); - void sendPhoto(std::string, std::string); + Telegram::Message *sendPhoto(std::string, Json::Int64); + Telegram::Message *sendPhoto(std::string, std::string); Telegram::Message *getMessage(); std::map getCommandMap(); @@ -55,7 +55,7 @@ namespace Telegram { void init(); void apiRequest(std::string, std::map); - void apiRequestJson(std::string, std::map); + Telegram::Message *apiRequestJson(std::string, std::map); std::string processCommand(std::string); }; } diff --git a/src/TelegramBot.cc b/src/TelegramBot.cc index 73961ff..dac4402 100644 --- a/src/TelegramBot.cc +++ b/src/TelegramBot.cc @@ -107,22 +107,22 @@ void Telegram::TelegramBot::sendMessage(std::string message, std::string chat_id /** * Sends a picture from the internet to a chat */ -void Telegram::TelegramBot::sendPhoto(std::string URL, Json::Int64 chat_id) { +Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, Json::Int64 chat_id) { - this->sendPhoto(URL, SSTR(chat_id)); + return(this->sendPhoto(URL, SSTR(chat_id))); } /** * Sends a picture from the internet to a chat */ -void Telegram::TelegramBot::sendPhoto(std::string URL, std::string chat_id) { +Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, std::string chat_id) { std::map params; params["chat_id"] = chat_id; params["photo"] = URL; - this->apiRequestJson("sendPhoto", params); + return(this->apiRequestJson("sendPhoto", params)); } Telegram::Message *Telegram::TelegramBot::getMessage() { @@ -181,31 +181,40 @@ void Telegram::TelegramBot::apiRequest(std::string method, std::map parameters) { - - parameters["method"] = method; - - try { - cURLpp::Easy handle; - std::list header; - header.push_back("Content-Type: application/json"); - - handle.setOpt(cURLpp::Options::Url(this->api_url)); - handle.setOpt(cURLpp::Options::ConnectTimeout(5)); - handle.setOpt(cURLpp::Options::Timeout(60)); - handle.setOpt(cURLpp::Options::HttpHeader(header)); - handle.setOpt(cURLpp::Options::PostFields(json_encode(parameters))); - handle.perform(); // Do the curl request - } - catch(cURLpp::LogicError &e) { - Log(e.what()); - } - catch(cURLpp::RuntimeError &e) { - Log(e.what()); - } - catch(std::exception &e) { - Log(e.what()); - } +Telegram::Message* Telegram::TelegramBot::apiRequestJson(std::string method, std::map parameters) { + + std::stringstream result; // Stores the result of the api call + parameters["method"] = method; + + try { + cURLpp::Easy handle; + std::list header; + header.push_back("Content-Type: application/json"); + + handle.setOpt(cURLpp::Options::Url(this->api_url)); + handle.setOpt(cURLpp::Options::ConnectTimeout(5)); + handle.setOpt(cURLpp::Options::Timeout(60)); + handle.setOpt(cURLpp::Options::HttpHeader(header)); + handle.setOpt(cURLpp::Options::PostFields(json_encode(parameters))); + handle.setOpt(cURLpp::Options::WriteStream(&result)); + + handle.perform(); // Do the curl request + } + 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 (new Telegram::Message(obj["result"])); } std::string Telegram::TelegramBot::processCommand(std::string cmd) {