LibTgBotPP
 All Classes Namespaces Files Functions Variables Typedefs Macros
TelegramBot.cc
Go to the documentation of this file.
1 #include "Telegram/TelegramBot.h"
2 
3 #include <iostream>
4 
5 #include <curlpp/cURLpp.hpp>
6 #include <curlpp/Easy.hpp>
7 #include <curlpp/Infos.hpp>
8 #include <curlpp/Options.hpp>
9 
10 #include "http_build_query.h"
11 #include "json_encode.h"
12 #include "std.h"
13 
14 #include "Telegram/Message.h"
15 #include "Telegram/MessageEntity.h"
16 
17 #define API_URL "https://api.telegram.org/bot"
18 
25 
26  this->api_url = "";
27  this->init();
28 }
29 
39 
40  this->api_url = API_URL + token + "/";
41  this->init();
42 }
43 
50 
51  if (cmd.command[0] != '/') {
52  cmd.command = "/" + cmd.command;
53  }
54  this->command_map[cmd.command] = cmd;
55 }
56 
65 void Telegram::TelegramBot::setWebhook(std::string url) {
66 
67  std::map<std::string, std::string> params;
68 
69  params["url"] = url;
70  this->apiRequest("setWebhook", params);
71 }
72 
79 void Telegram::TelegramBot::processMessage(std::string message) {
80 
81  this->msg = new Telegram::Message(message);
82 
83  Telegram::TMessageEntities entities = this->msg->getEntities();
84 
85  if (entities.size() && (entities[0]->getType() == "bot_command")) {
86  this->sendMessage(this->processCommand(this->msg->getText()), this->msg->getChat()->getId());
87  } else {
88  this->sendMessage("Hello " + this->msg->getChat()->getUsername() + ", you told me: '" + msg->getText() + "'", this->msg->getChat()->getId());
89  }
90 }
91 
98 
99  Json::Value obj;
100  std::map<std::string, std::string> params;
101 
102  obj = this->apiRequestJson("getMe", params);
103 
104  return(new Telegram::User(obj["result"]));
105 }
106 
115 Telegram::Message *Telegram::TelegramBot::sendMessage(std::string message, Json::Int64 chat_id) {
116 
117  return(this->sendMessage(message, SSTR(chat_id)));
118 }
119 
128 Telegram::Message* Telegram::TelegramBot::sendMessage(std::string message, std::string chat_id) {
129 
130  std::map<std::string, std::string> params;
131  Json::Value obj;
132 
133  params["chat_id"] = chat_id;
134  params["text"] = message;
135 
136  obj = this->apiRequestJson("sendMessage", params);
137 
138  return(new Telegram::Message(obj["result"]));
139 }
140 
149 Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, Json::Int64 chat_id) {
150 
151  return(this->sendPhoto(URL, SSTR(chat_id)));
152 }
153 
162 Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, std::string chat_id) {
163 
164  Json::Value obj;
165 
166  std::map<std::string, std::string> params;
167 
168  params["chat_id"] = chat_id;
169  params["photo"] = URL;
170 
171  if ((URL.substr(0, 8) == "https://") || (URL.substr(0, 7) == "http://")) {
172  obj = this->apiRequestJson("sendPhoto", params);
173  }
174 
175  if (URL.substr(0, 7) == "file://") {
176  obj = this->apiRequestFile(URL.substr(7, std::string::npos), "photo", chat_id);
177  }
178 
179  return(new Telegram::Message(obj["result"]));
180 }
181 
183 
184  return(this->msg);
185 }
186 
193 
194  return(this->command_map);
195 }
196 
197 // Private methods
198 
204 
205  std::cout << "Content-type: text/html\r\n\r\n" << std::endl;
206 }
207 
216 void Telegram::TelegramBot::apiRequest(std::string method, std::map<std::string, std::string> parameters) {
217 
218  parameters["method"] = method;
219 
220  Json::Value jValues;
221  Json::StyledWriter w;
222 
223  for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
224  jValues[(*it).first] = (*it).second;
225  }
226 
227  try {
228  cURLpp::Easy handle;
229  std::string url = this->api_url + method + "?" + http_build_query(parameters);
230 
231  handle.setOpt(cURLpp::Options::Url(url));
232  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
233  handle.setOpt(cURLpp::Options::Timeout(60));
234  handle.perform(); // Do the curl request
235 
236  int code;
237  code = cURLpp::Infos::ResponseCode::get(handle);
238  }
239  catch(cURLpp::LogicError &e) {
240  Log(e.what());
241  }
242  catch(cURLpp::RuntimeError &e) {
243  Log(e.what());
244  }
245  catch(std::exception &e) {
246  Log(e.what());
247  }
248 }
249 
258 Json::Value Telegram::TelegramBot::apiRequestJson(std::string method, std::map<std::string, std::string> parameters) {
259 
260  std::stringstream result; // Stores the result of the api call
261  parameters["method"] = method;
262 
263  try {
264  cURLpp::Easy handle;
265  cURLpp::Cleanup cleaner;
266 
267  std::list<std::string> header;
268  header.push_back("Content-Type: application/json");
269 
270  handle.setOpt(cURLpp::Options::Url(this->api_url));
271  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
272  handle.setOpt(cURLpp::Options::Timeout(60));
273  handle.setOpt(cURLpp::Options::HttpHeader(header));
274  handle.setOpt(cURLpp::Options::PostFields(json_encode(parameters)));
275  handle.setOpt(cURLpp::Options::WriteStream(&result));
276 
277  handle.perform(); // Do the curl request
278  }
279  catch(cURLpp::LogicError &e) {
280  Log(e.what());
281  }
282  catch(cURLpp::RuntimeError &e) {
283  Log(e.what());
284  }
285  catch(std::exception &e) {
286  Log(e.what());
287  }
288 
289  Json::Reader jreader;
290  Json::Value obj;
291  jreader.parse(result.str(), obj);
292 
293  return (obj);
294 }
295 
305 Json::Value Telegram::TelegramBot::apiRequestFile(std::string filename, std::string type, std::string chat_id) {
306 
307  std::stringstream result; // Stores the result of the api call
308  cURLpp::Forms formParts; // Parts to be sent
309 
310  try {
311  cURLpp::Easy handle;
312  cURLpp::Cleanup cleaner;
313 
314  handle.setOpt(cURLpp::Options::Url(this->api_url + "send" + type));
315  {
316  cURLpp::Forms parts;
317  parts.push_back(new cURLpp::FormParts::Content("chat_id", chat_id));
318  parts.push_back(new cURLpp::FormParts::File(type, filename));
319  handle.setOpt(cURLpp::Options::HttpPost(parts));
320  }
321 
322  handle.perform();
323 
324  }
325  catch(cURLpp::LogicError &e) {
326  Log(e.what());
327  }
328  catch(cURLpp::RuntimeError &e) {
329  Log(e.what());
330  }
331  catch(std::exception &e) {
332  Log(e.what());
333  }
334 
335  Json::Reader jreader;
336  Json::Value obj;
337  jreader.parse(result.str(), obj);
338 
339  return (obj);
340 }
341 
352 std::string Telegram::TelegramBot::processCommand(std::string cmd) {
353 
354  // Process command
355  std::string sCommand;
356  std::string sResult;
357  std::vector<std::string> vCmdLine = split(cmd, ' ');
358 
359  if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
360  sCommand = vCmdLine[0];
361  vCmdLine.erase(vCmdLine.begin()); // First value is the command itself
362  TCommand cs = this->command_map[sCommand];
363  sResult = (*cs.callback)(this, vCmdLine);
364  } else {
365  sResult = "Unknown command";
366  }
367 
368  return(sResult);
369 }
std::string command
The command, e.g. /help.
Definition: TelegramBot.h:27
#define SSTR(x)
Definition: example01.cc:35
std::vector< std::string > split(const std::string &text, char sep)
Definition: std.cc:42
Telegram::User * getMe(void)
Definition: TelegramBot.cc:97
void setWebhook(std::string)
Definition: TelegramBot.cc:65
Json::Value apiRequestFile(std::string, std::string, std::string)
Definition: TelegramBot.cc:305
void processMessage(std::string)
Definition: TelegramBot.cc:79
void apiRequest(std::string, std::map< std::string, std::string >)
Definition: TelegramBot.cc:216
std::map< std::string, TCommand > getCommandMap()
Definition: TelegramBot.cc:192
std::string processCommand(std::string)
Definition: TelegramBot.cc:352
CommandCallback callback
Pointer to the callback function.
Definition: TelegramBot.h:28
void Log(std::string msg)
Definition: std.cc:91
std::map< std::string, TCommand > TCommandMap
Map that stores the known commands.
Definition: TelegramBot.h:31
#define API_URL
Definition: TelegramBot.cc:17
void addCommand(TCommand)
Definition: TelegramBot.cc:49
Telegram::Message * getMessage()
Definition: TelegramBot.cc:182
std::vector< Telegram::MessageEntity * > TMessageEntities
Definition: MessageEntity.h:23
Json::Value apiRequestJson(std::string, std::map< std::string, std::string >)
Definition: TelegramBot.cc:258
std::string http_build_query(std::map< std::string, std::string > data)
Telegram::Message * sendPhoto(std::string, Json::Int64)
Definition: TelegramBot.cc:149
std::string json_encode(std::map< std::string, std::string > parameters)
Definition: json_encode.cc:3
Telegram::Message * sendMessage(std::string, Json::Int64)
Definition: TelegramBot.cc:115
std::string api_url
Definition: TelegramBot.h:55