added telegram api support to send message on ip update
This commit is contained in:
83
src/api/API.cpp
Normal file
83
src/api/API.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#include "API.h"
|
||||
#include "../Hashmap.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <curl/curl.h>
|
||||
|
||||
|
||||
std::string API::request(std::string myurl) {
|
||||
Hashmap<std::string,std::string> map;
|
||||
std::vector<std::string> str;
|
||||
|
||||
return request(std::move(myurl),false,map,str);
|
||||
}
|
||||
|
||||
std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::string> &map, std::vector<std::string> &headers) {
|
||||
CURL *curl;
|
||||
CURLcode res;
|
||||
|
||||
|
||||
curl = curl_easy_init();
|
||||
|
||||
struct curl_slist *list = nullptr;
|
||||
for (int j = 0; j < headers.size(); ++j) {
|
||||
list = curl_slist_append(list, headers.at(j).c_str());
|
||||
}
|
||||
|
||||
std::string readString;
|
||||
if (curl) {
|
||||
curl_easy_setopt(curl, CURLOPT_URL, myurl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
||||
|
||||
|
||||
//ssl verification
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
|
||||
if(post){
|
||||
std::stringstream poststring;
|
||||
poststring << "{";
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
poststring << "\""+map.getKey(i) << "\": \""+map.getValue(i) << "\"";
|
||||
if(i < map.size()-1){
|
||||
poststring << ", ";
|
||||
}
|
||||
}
|
||||
poststring << "}";
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
|
||||
}else{
|
||||
std::string getstring;
|
||||
for(int i =0; i< map.size();i++){
|
||||
getstring+=map.getKey(i)+"="+map.getValue(i);
|
||||
if(i < map.size()-1){
|
||||
getstring+="&";
|
||||
}
|
||||
}
|
||||
|
||||
myurl+="?"+getstring;
|
||||
}
|
||||
|
||||
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
return readString;
|
||||
}
|
||||
|
||||
size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) {
|
||||
((std::string*)stream)->append((char*)contents, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
22
src/api/API.h
Normal file
22
src/api/API.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#ifndef IPREFRESHER_API_H
|
||||
#define IPREFRESHER_API_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "../Hashmap.h"
|
||||
|
||||
class API {
|
||||
public:
|
||||
std::string request(std::string myurl);
|
||||
std::string request(std::string myurl, bool post, Hashmap<std::string,std::string> &map,std::vector<std::string> &headers);
|
||||
|
||||
private:
|
||||
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);
|
||||
};
|
||||
|
||||
|
||||
#endif //IPREFRESHER_API_H
|
10
src/api/TelegramAPI.cpp
Normal file
10
src/api/TelegramAPI.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by lukas on 08.05.19.
|
||||
//
|
||||
|
||||
#include "TelegramAPI.h"
|
||||
|
||||
void TelegramAPI::sendMessage(std::string text) {
|
||||
std::string reply = request("https://api.telegram.org/bot"+ apikey + "/sendmessage?chat_id="+chatid+"&text="+text);
|
||||
std::cout << "[DEBUG] " <<reply << std::endl;
|
||||
}
|
22
src/api/TelegramAPI.h
Normal file
22
src/api/TelegramAPI.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by lukas on 08.05.19.
|
||||
//
|
||||
|
||||
#ifndef IPREFRESHER_TELEGRAMAPI_H
|
||||
#define IPREFRESHER_TELEGRAMAPI_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "API.h"
|
||||
|
||||
class TelegramAPI : API{
|
||||
public:
|
||||
void sendMessage(std::string text);
|
||||
|
||||
private:
|
||||
std::string apikey = "717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4";
|
||||
std::string chatid = "618154204";
|
||||
};
|
||||
|
||||
|
||||
#endif //IPREFRESHER_TELEGRAMAPI_H
|
Reference in New Issue
Block a user