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 "Telegram/http_build_query.h"
11 #include "Telegram/json_encode.h"
12 #include "Telegram/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 
95 void Telegram::TelegramBot::sendMessage(std::string message, std::string chat_id) {
96 
97  std::map<std::string, std::string> params;
98  params["chat_id"] = chat_id;
99  params["text"] = message;
100 
101  this->apiRequestJson("sendMessage", params);
102 }
103 
105 
106  return(this->msg);
107 }
108 
110 
111  return(this->command_map);
112 }
113 
114 // Private methods
116 
117  std::cout << "Content-type: text/html\r\n\r\n" << std::endl;
118 }
119 
123 void Telegram::TelegramBot::apiRequest(std::string method, std::map<std::string, std::string> parameters) {
124 
125  parameters["method"] = method;
126 
127  Json::Value jValues;
128  Json::StyledWriter w;
129 
130  for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
131  jValues[(*it).first] = (*it).second;
132  }
133 
134  try {
135  cURLpp::Easy handle;
136  std::string url = this->api_url + method + "?" + http_build_query(parameters);
137 
138  handle.setOpt(cURLpp::Options::Url(url));
139  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
140  handle.setOpt(cURLpp::Options::Timeout(60));
141  handle.perform(); // Do the curl request
142 
143  int code;
144  code = cURLpp::Infos::ResponseCode::get(handle);
145  }
146  catch(cURLpp::LogicError &e) {
147  Log(e.what());
148  }
149  catch(cURLpp::RuntimeError &e) {
150  Log(e.what());
151  }
152  catch(std::exception &e) {
153  Log(e.what());
154  }
155 }
156 
160 void Telegram::TelegramBot::apiRequestJson(std::string method, std::map<std::string, std::string> parameters) {
161 
162  parameters["method"] = method;
163 
164  try {
165  cURLpp::Easy handle;
166  std::list<std::string> header;
167  header.push_back("Content-Type: application/json");
168 
169  handle.setOpt(cURLpp::Options::Url(this->api_url));
170  handle.setOpt(cURLpp::Options::ConnectTimeout(5));
171  handle.setOpt(cURLpp::Options::Timeout(60));
172  handle.setOpt(cURLpp::Options::HttpHeader(header));
173  handle.setOpt(cURLpp::Options::PostFields(json_encode(parameters)));
174  handle.perform(); // Do the curl request
175  }
176  catch(cURLpp::LogicError &e) {
177  Log(e.what());
178  }
179  catch(cURLpp::RuntimeError &e) {
180  Log(e.what());
181  }
182  catch(std::exception &e) {
183  Log(e.what());
184  }
185 }
186 
187 std::string Telegram::TelegramBot::processCommand(std::string cmd) {
188 
189  // Process command
190  std::string sCommand;
191  std::string sResult;
192  std::vector<std::string> vCmdLine = split(cmd, ' ');
193 
194  if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
195  sCommand = vCmdLine[0];
196  vCmdLine.erase(vCmdLine.begin()); // First value is the command itself
197  TCommand cs = this->command_map[sCommand];
198  sResult = (*cs.callback)(this, vCmdLine);
199  } else {
200  sResult = "Unknown command";
201  }
202 
203  return(sResult);
204 }
std::string command
The command, e.g. /help.
Definition: TelegramBot.h:26
#define SSTR(x)
Definition: std.h:10
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:123
std::map< std::string, TCommand > getCommandMap()
Definition: TelegramBot.cc:109
std::string processCommand(std::string)
Definition: TelegramBot.cc:187
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:104
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:160
std::string json_encode(std::map< std::string, std::string > parameters)
Definition: json_encode.cc:3
std::string api_url
Definition: TelegramBot.h:48