used Hashmap for telegram api request

This commit is contained in:
Lukas-Heiligenbrunner 2019-05-10 08:30:08 +02:00
parent a890cf687e
commit b599626bbf
2 changed files with 18 additions and 9 deletions

View File

@ -33,7 +33,6 @@ API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &ma
std::string readString; std::string readString;
if (curl) { if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, myurl.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
@ -55,17 +54,21 @@ API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &ma
curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str()); curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
} else { } else {
std::string getstring; if (map.size() > 0) {
for (int i = 0; i < map.size(); i++) { std::string getstring;
getstring += map.getKey(i) + "=" + map.getValue(i); for (int i = 0; i < map.size(); i++) {
if (i < map.size() - 1) { getstring += map.getKey(i) + "=" + map.getValue(i);
getstring += "&"; if (i < map.size() - 1) {
getstring += "&";
}
} }
}
myurl += "?" + getstring; myurl += "?" + getstring;
}
} }
curl_easy_setopt(curl, CURLOPT_URL, myurl.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);

View File

@ -5,6 +5,12 @@
#include "TelegramAPI.h" #include "TelegramAPI.h"
void TelegramAPI::sendMessage(std::string text) { void TelegramAPI::sendMessage(std::string text) {
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage?chat_id=" + chatid + "&text=" + text); Hashmap<std::string, std::string> args;
args.add("chat_id", chatid);
args.add("text", text);
std::vector<std::string> headers;
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers);
std::cout << "[DEBUG] " << reply << std::endl; std::cout << "[DEBUG] " << reply << std::endl;
} }