yatbcpp  0.0.1
Yet another Telegram Bot CPP Library
telegram_methodMultipart.h
Go to the documentation of this file.
1 #ifndef YATBCPP_TELEGRAM_METHODMULTIPART_H
2 #define YATBCPP_TELEGRAM_METHODMULTIPART_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{
17  template <class RETURNTYPE> class telegram_methodMultipart{
18  public:
19 
20  telegram_methodMultipart(std::string functionname) : functionname(move(functionname)){}
21 
22  static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *stringptr){
23  using namespace std;
24  ((string*)stringptr)->append((char*)contents, size * nmemb);
25  return size * nmemb;
26  }
27 
28 
29 
31  using namespace std;
32  string api_url ( "https://api.telegram.org/bot" + T.getToken() + "/" + method_body.getFunctionname() );
33  string readBuffer;
34  Json::Reader reader;
35  Json::Value Response;
36  struct curl_slist * curl_header_list =NULL;
37  curl_header_list = curl_slist_append( curl_header_list , "Content-Type: multipart/form-data" );
38 
39  struct curl_httppost *uploadPost=NULL;
40  struct curl_httppost *endptr=NULL;
41 
42  method_body.add_to_post(&uploadPost,&endptr);
43 
44  CURL* curl = curl_easy_init();
45 
46  curl_easy_setopt(curl,CURLOPT_URL,api_url.c_str());
47  curl_easy_setopt(curl,CURLOPT_HTTPHEADER,curl_header_list);
48  curl_easy_setopt(curl,CURLOPT_HTTPPOST,uploadPost);
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  curl_formfree(uploadPost);
61  return yatbcpp::fromJson<RETURNTYPE>(Response["result"]);
62  }
63  else{
64  curl_formfree(uploadPost);
65  curl_easy_cleanup(curl);
66  curl_slist_free_all(curl_header_list);
67  throw telegram_api_error(Response["error_code"].asInt(),Response["description"].asString());
68  }
69 
70 
71  }
77  virtual void add_to_post(struct curl_httppost **start,struct curl_httppost **end){}
78 
83  const std::string &getFunctionname() const {
84  return functionname;
85  }
86 
87  protected:
88  std::string functionname;
89  };
90 
91 }
92 
93 
94 #endif //YATBCPP_TELEGRAM_METHODMULTIPART_H
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *stringptr)
static RETURNTYPE perform_requestMultipart(Token T, telegram_methodMultipart< RETURNTYPE > &method_body)
telegram_methodMultipart(std::string functionname)
Definition: Bot.h:27
virtual void add_to_post(struct curl_httppost **start, struct curl_httppost **end)
const std::string & getFunctionname() const
const std::string getToken() const
Definition: Token.cc:23