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 
88 
89  Json::Value obj;
90  std::map<std::string, std::string> params;
91 
92  obj = this->apiRequestJson("getMe", params);
93 
94  return(new Telegram::User(obj["result"]));
95 }
96 
100 Telegram::Message *Telegram::TelegramBot::sendMessage(std::string message, Json::Int64 chat_id) {
101 
102  return(this->sendMessage(message, SSTR(chat_id)));
103 }
104 
108 Telegram::Message* Telegram::TelegramBot::sendMessage(std::string message, std::string chat_id) {
109 
110  std::map<std::string, std::string> params;
111  Json::Value obj;
112 
113  params["chat_id"] = chat_id;
114  params["text"] = message;
115 
116  obj = this->apiRequestJson("sendMessage", params);
117 
118  return(new Telegram::Message(obj["result"]));
119 }
120 
124 Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, Json::Int64 chat_id) {
125 
126  return(this->sendPhoto(URL, SSTR(chat_id)));
127 }
128 
132 Telegram::Message* Telegram::TelegramBot::sendPhoto(std::string URL, std::string chat_id) {
133 
134  Json::Value obj;
135 
136  std::map<std::string, std::string> params;
137 
138  params["chat_id"] = chat_id;
139  params["photo"] = URL;
140 
141  obj = this->apiRequestJson("sendPhoto", params);
142 
143  return(new Telegram::Message(obj["result"]));
144 }
145 
147 
148  return(this->msg);
149 }
150 
152 
153  return(this->command_map);
154 }
155 
156 // Private methods
158 
159  std::cout << "Content-type: text/html\r\n\r\n" << std::endl;
160 }
161 
165 void Telegram::TelegramBot::apiRequest(std::string method, std::map<std::string, std::string> parameters) {
166 
167  parameters["method"] = method;
168 
169  Json::Value jValues;
170  Json::StyledWriter w;
171 
172  for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
173  jValues[(*it).first] = (*it).second;
174  }
175 
176  try {
177  cURLpp::Easy handle;
178  std::string url = this->api_url + method + "?" + http_build_query(parameters);
179 
180  handle.setOpt(cURLpp::Options::Url(url));
181  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
182  handle.setOpt(cURLpp::Options::Timeout(60));
183  handle.perform(); // Do the curl request
184 
185  int code;
186  code = cURLpp::Infos::ResponseCode::get(handle);
187  }
188  catch(cURLpp::LogicError &e) {
189  Log(e.what());
190  }
191  catch(cURLpp::RuntimeError &e) {
192  Log(e.what());
193  }
194  catch(std::exception &e) {
195  Log(e.what());
196  }
197 }
198 
202 Json::Value Telegram::TelegramBot::apiRequestJson(std::string method, std::map<std::string, std::string> parameters) {
203 
204  std::stringstream result; // Stores the result of the api call
205  parameters["method"] = method;
206 
207  try {
208  cURLpp::Easy handle;
209  std::list<std::string> header;
210  header.push_back("Content-Type: application/json");
211 
212  handle.setOpt(cURLpp::Options::Url(this->api_url));
213  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
214  handle.setOpt(cURLpp::Options::Timeout(60));
215  handle.setOpt(cURLpp::Options::HttpHeader(header));
216  handle.setOpt(cURLpp::Options::PostFields(json_encode(parameters)));
217  handle.setOpt(cURLpp::Options::WriteStream(&result));
218 
219  handle.perform(); // Do the curl request
220  }
221  catch(cURLpp::LogicError &e) {
222  Log(e.what());
223  }
224  catch(cURLpp::RuntimeError &e) {
225  Log(e.what());
226  }
227  catch(std::exception &e) {
228  Log(e.what());
229  }
230 
231  Json::Reader jreader;
232  Json::Value obj;
233  jreader.parse(result.str(), obj);
234 
235  return (obj);
236 }
237 
238 std::string Telegram::TelegramBot::processCommand(std::string cmd) {
239 
240  // Process command
241  std::string sCommand;
242  std::string sResult;
243  std::vector<std::string> vCmdLine = split(cmd, ' ');
244 
245  if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
246  sCommand = vCmdLine[0];
247  vCmdLine.erase(vCmdLine.begin()); // First value is the command itself
248  TCommand cs = this->command_map[sCommand];
249  sResult = (*cs.callback)(this, vCmdLine);
250  } else {
251  sResult = "Unknown command";
252  }
253 
254  return(sResult);
255 }
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:87
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:165
std::map< std::string, TCommand > getCommandMap()
Definition: TelegramBot.cc:151
std::string processCommand(std::string)
Definition: TelegramBot.cc:238
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:44
Telegram::Message * getMessage()
Definition: TelegramBot.cc:146
std::vector< Telegram::MessageEntity * > TMessageEntities
Definition: MessageEntity.h:23
Json::Value apiRequestJson(std::string, std::map< std::string, std::string >)
Definition: TelegramBot.cc:202
std::string http_build_query(std::map< std::string, std::string > data)
Telegram::Message * sendPhoto(std::string, Json::Int64)
Definition: TelegramBot.cc:124
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:100
std::string api_url
Definition: TelegramBot.h:55