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,6 +54,7 @@ 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 {
if (map.size() > 0) {
std::string getstring; std::string getstring;
for (int i = 0; i < map.size(); i++) { for (int i = 0; i < map.size(); i++) {
getstring += map.getKey(i) + "=" + map.getValue(i); getstring += map.getKey(i) + "=" + map.getValue(i);
@ -65,6 +65,9 @@ API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &ma
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);

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;
} }