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 
23 
24  this->api_url = "";
25  this->init();
26 }
27 
34 
35  this->api_url = API_URL + token + "/";
36  this->init();
37 }
38 
45 
46  if (cmd.command[0] != '/') {
47  cmd.command = "/" + cmd.command;
48  }
49  this->command_map[cmd.command] = cmd;
50 }
51 
60 void Telegram::TelegramBot::setWebhook(std::string url) {
61 
62  std::map<std::string, std::string> params;
63 
64  params["url"] = url;
65  this->apiRequest("setWebhook", params);
66 }
67 
74 void Telegram::TelegramBot::processMessage(std::string message) {
75 
76  this->msg = new Telegram::Message(message);
77 
78  Telegram::TMessageEntities entities = this->msg->getEntities();
79 
80  if (entities.size() && (entities[0]->getType() == "bot_command")) {
81  this->sendMessage(this->processCommand(this->msg->getText()), this->msg->getChat()->getId());
82  } else {
83  this->sendMessage("Hello " + this->msg->getChat()->getUsername() + ", you told me: '" + msg->getText() + "'", this->msg->getChat()->getId());
84  }
85 }
86 
90 void Telegram::TelegramBot::sendMessage(std::string message, Json::Int64 chat_id) {
91 
92  this->sendMessage(message, SSTR(chat_id));
93 }
94 
98 void Telegram::TelegramBot::sendMessage(std::string message, std::string chat_id) {
99 
100  std::map<std::string, std::string> params;
101  params["chat_id"] = chat_id;
102  params["text"] = message;
103 
104  this->apiRequestJson("sendMessage", params);
105 }
106 
110 void Telegram::TelegramBot::sendPhoto(std::string URL, Json::Int64 chat_id) {
111 
112  this->sendPhoto(URL, SSTR(chat_id));
113 }
114 
118 void Telegram::TelegramBot::sendPhoto(std::string URL, std::string chat_id) {
119 
120  std::map<std::string, std::string> params;
121 
122  params["chat_id"] = chat_id;
123  params["photo"] = URL;
124 
125  this->apiRequestJson("sendPhoto", params);
126 }
127 
129 
130  return(this->msg);
131 }
132 
134 
135  return(this->command_map);
136 }
137 
138 // Private methods
140 
141  std::cout << "Content-type: text/html\r\n\r\n" << std::endl;
142 }
143 
147 void Telegram::TelegramBot::apiRequest(std::string method, std::map<std::string, std::string> parameters) {
148 
149  parameters["method"] = method;
150 
151  Json::Value jValues;
152  Json::StyledWriter w;
153 
154  for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
155  jValues[(*it).first] = (*it).second;
156  }
157 
158  try {
159  cURLpp::Easy handle;
160  std::string url = this->api_url + method + "?" + http_build_query(parameters);
161 
162  handle.setOpt(cURLpp::Options::Url(url));
163  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
164  handle.setOpt(cURLpp::Options::Timeout(60));
165  handle.perform(); // Do the curl request
166 
167  int code;
168  code = cURLpp::Infos::ResponseCode::get(handle);
169  }
170  catch(cURLpp::LogicError &e) {
171  Log(e.what());
172  }
173  catch(cURLpp::RuntimeError &e) {
174  Log(e.what());
175  }
176  catch(std::exception &e) {
177  Log(e.what());
178  }
179 }
180 
184 void Telegram::TelegramBot::apiRequestJson(std::string method, std::map<std::string, std::string> parameters) {
185 
186  parameters["method"] = method;
187 
188  try {
189  cURLpp::Easy handle;
190  std::list<std::string> header;
191  header.push_back("Content-Type: application/json");
192 
193  handle.setOpt(cURLpp::Options::Url(this->api_url));
194  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
195  handle.setOpt(cURLpp::Options::Timeout(60));
196  handle.setOpt(cURLpp::Options::HttpHeader(header));
197  handle.setOpt(cURLpp::Options::PostFields(json_encode(parameters)));
198  handle.perform(); // Do the curl request
199  }
200  catch(cURLpp::LogicError &e) {
201  Log(e.what());
202  }
203  catch(cURLpp::RuntimeError &e) {
204  Log(e.what());
205  }
206  catch(std::exception &e) {
207  Log(e.what());
208  }
209 }
210 
211 std::string Telegram::TelegramBot::processCommand(std::string cmd) {
212 
213  // Process command
214  std::string sCommand;
215  std::string sResult;
216  std::vector<std::string> vCmdLine = split(cmd, ' ');
217 
218  if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
219  sCommand = vCmdLine[0];
220  vCmdLine.erase(vCmdLine.begin()); // First value is the command itself
221  TCommand cs = this->command_map[sCommand];
222  sResult = (*cs.callback)(this, vCmdLine);
223  } else {
224  sResult = "Unknown command";
225  }
226 
227  return(sResult);
228 }
std::string command
The command, e.g. /help.
Definition: TelegramBot.h:26
#define SSTR(x)
Definition: example01.cc:35
void sendMessage(std::string, Json::Int64)
Definition: TelegramBot.cc:90
std::vector< std::string > split(const std::string &text, char sep)
Definition: std.cc:42
void setWebhook(std::string)
Definition: TelegramBot.cc:60
void processMessage(std::string)
Definition: TelegramBot.cc:74
void apiRequest(std::string, std::map< std::string, std::string >)
Definition: TelegramBot.cc:147
std::map< std::string, TCommand > getCommandMap()
Definition: TelegramBot.cc:133
void sendPhoto(std::string, Json::Int64)
Definition: TelegramBot.cc:110
std::string processCommand(std::string)
Definition: TelegramBot.cc:211
CommandCallback callback
Pointer to the callback function.
Definition: TelegramBot.h:27
void Log(std::string msg)
Definition: std.cc:91
std::map< std::string, TCommand > TCommandMap
Map that stores the known commands.
Definition: TelegramBot.h:30
#define API_URL
Definition: TelegramBot.cc:17
void addCommand(TCommand)
Definition: TelegramBot.cc:44
Telegram::Message * getMessage()
Definition: TelegramBot.cc:128
std::vector< Telegram::MessageEntity * > TMessageEntities
Definition: MessageEntity.h:23
std::string http_build_query(std::map< std::string, std::string > data)
void apiRequestJson(std::string, std::map< std::string, std::string >)
Definition: TelegramBot.cc:184
std::string json_encode(std::map< std::string, std::string > parameters)
Definition: json_encode.cc:3
std::string api_url
Definition: TelegramBot.h:51