yatbcpp  0.0.1
Yet another Telegram Bot CPP Library
telegram_methodJSON.h
Go to the documentation of this file.
1 #ifndef YATBCPP_TELEGRAM_METHODJSON_H
2 #define YATBCPP_TELEGRAM_METHODJSON_H
3 
4 #include "curl/curl.h"
5 #include <jsoncpp/json/json.h>
6 #include "../exceptions/telegram_api_error.h"
7 #include "../exceptions/curl_error.h"
8 #include "../types/telegram_type.h"
9 #include "../exceptions/essential_key_missing.h"
10 #include "../bot/Token.h"
11 
12 namespace yatbcpp{
13  template <typename T> Json::Value toJson(T Obj);
14 
19  template <class RETURNTYPE> class telegram_methodJSON{
20  public:
21 
22  telegram_methodJSON(std::string functionname) : functionname(move(functionname)){
23  //ok das iwie noch meh
24  //static_assert(std::is_base_of<RETURNTYPE,telegram_method>::value, "Derived RETURNTYPE is not derived from BaseClass, fromJSON can not be guaranteed");
25  }
26 
27  static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *stringptr){
28  using namespace std;
29  ((string*)stringptr)->append((char*)contents, size * nmemb);
30  return size * nmemb;
31  }
32 
33 
34 
35  static RETURNTYPE perform_requestJSON(Token T, telegram_methodJSON<RETURNTYPE> &method_body){
36  using namespace std;
37  string curl_payload( method_body.toJson().toStyledString() );
38  string api_url ( "https://api.telegram.org/bot" + T.getToken() + "/" + method_body.getFunctionname() );
39  string readBuffer;
40  Json::Reader reader;
41  Json::Value Response;
42  struct curl_slist * curl_header_list =NULL;
43  curl_header_list = curl_slist_append( curl_header_list , "Content-Type: application/json" );
44  CURL* curl = curl_easy_init();
45 
46  curl_easy_setopt(curl,CURLOPT_URL,api_url.c_str());
47  curl_easy_setopt(curl,CURLOPT_POSTFIELDS,curl_payload.c_str());
48  curl_easy_setopt(curl,CURLOPT_HTTPHEADER,curl_header_list);
49  curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteCallback);
50  curl_easy_setopt(curl,CURLOPT_WRITEDATA,&readBuffer);
51 
52  CURLcode res = curl_easy_perform(curl);
53  reader.parse(readBuffer,Response);
54  if(res!=CURLE_OK){
55  throw curl_error(res,curl_easy_strerror(res));
56  }
57  if(Response["ok"].asBool()){
58  curl_slist_free_all(curl_header_list);
59  curl_easy_cleanup(curl);
60  return yatbcpp::fromJson<RETURNTYPE>(Response["result"]);
61  }
62  else{
63  curl_easy_cleanup(curl);
64  curl_slist_free_all(curl_header_list);
65  throw telegram_api_error(Response["error_code"].asInt(),Response["description"].asString());
66  }
67 
68 
69  }
74  virtual Json::Value toJson(){}
75 
80  const std::string &getFunctionname() const {
81  return functionname;
82  }
83 
84  protected:
85  std::string functionname;
86  };
87 
88 }
89 
90 
91 #endif //YATBCPP_TELEGRAM_METHODJSON_H
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *stringptr)
const std::string & getFunctionname() const
Definition: Bot.h:27
telegram_methodJSON(std::string functionname)
static RETURNTYPE perform_requestJSON(Token T, telegram_methodJSON< RETURNTYPE > &method_body)
virtual Json::Value toJson()
Json::Value toJson(T Obj)
const std::string getToken() const
Definition: Token.cc:23