5 #include <curlpp/cURLpp.hpp>
6 #include <curlpp/Easy.hpp>
7 #include <curlpp/Infos.hpp>
8 #include <curlpp/Options.hpp>
17 #define API_URL "https://api.telegram.org/bot"
35 this->api_url =
API_URL + token +
"/";
49 this->command_map[cmd.
command] = cmd;
62 std::map<std::string, std::string> params;
65 this->apiRequest(
"setWebhook", params);
80 if (entities.size() && (entities[0]->getType() ==
"bot_command")) {
81 this->sendMessage(this->processCommand(this->msg->getText()), this->msg->getChat()->getId());
83 this->sendMessage(
"Hello " + this->msg->getChat()->getUsername() +
", you told me: '" + msg->getText() +
"'", this->msg->getChat()->getId());
90 std::map<std::string, std::string> params;
92 obj = this->apiRequestJson(
"getMe", params);
102 return(this->sendMessage(message,
SSTR(chat_id)));
110 std::map<std::string, std::string> params;
113 params[
"chat_id"] = chat_id;
114 params[
"text"] = message;
116 obj = this->apiRequestJson(
"sendMessage", params);
126 return(this->sendPhoto(URL,
SSTR(chat_id)));
136 std::map<std::string, std::string> params;
138 params[
"chat_id"] = chat_id;
139 params[
"photo"] = URL;
141 obj = this->apiRequestJson(
"sendPhoto", params);
153 return(this->command_map);
159 std::cout <<
"Content-type: text/html\r\n\r\n" << std::endl;
167 parameters[
"method"] = method;
170 Json::StyledWriter w;
172 for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
173 jValues[(*it).first] = (*it).second;
178 std::string url = this->api_url + method +
"?" +
http_build_query(parameters);
180 handle.setOpt(cURLpp::Options::Url(url));
181 handle.setOpt(cURLpp::Options::ConnectTimeout(5));
182 handle.setOpt(cURLpp::Options::Timeout(60));
186 code = cURLpp::Infos::ResponseCode::get(handle);
188 catch(cURLpp::LogicError &e) {
191 catch(cURLpp::RuntimeError &e) {
194 catch(std::exception &e) {
204 std::stringstream result;
205 parameters[
"method"] = method;
209 std::list<std::string> header;
210 header.push_back(
"Content-Type: application/json");
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));
221 catch(cURLpp::LogicError &e) {
224 catch(cURLpp::RuntimeError &e) {
227 catch(std::exception &e) {
231 Json::Reader jreader;
233 jreader.parse(result.str(), obj);
241 std::string sCommand;
243 std::vector<std::string> vCmdLine =
split(cmd,
' ');
245 if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
246 sCommand = vCmdLine[0];
247 vCmdLine.erase(vCmdLine.begin());
248 TCommand cs = this->command_map[sCommand];
249 sResult = (*cs.
callback)(
this, vCmdLine);
251 sResult =
"Unknown command";
std::string command
The command, e.g. /help.
std::vector< std::string > split(const std::string &text, char sep)
Telegram::User * getMe(void)
void setWebhook(std::string)
void processMessage(std::string)
void apiRequest(std::string, std::map< std::string, std::string >)
std::map< std::string, TCommand > getCommandMap()
std::string processCommand(std::string)
CommandCallback callback
Pointer to the callback function.
void Log(std::string msg)
std::map< std::string, TCommand > TCommandMap
Map that stores the known commands.
void addCommand(TCommand)
Telegram::Message * getMessage()
std::vector< Telegram::MessageEntity * > TMessageEntities
Json::Value apiRequestJson(std::string, std::map< std::string, std::string >)
std::string http_build_query(std::map< std::string, std::string > data)
Telegram::Message * sendPhoto(std::string, Json::Int64)
std::string json_encode(std::map< std::string, std::string > parameters)
Telegram::Message * sendMessage(std::string, Json::Int64)