5 #include <curlpp/cURLpp.hpp>
6 #include <curlpp/Easy.hpp>
7 #include <curlpp/Infos.hpp>
8 #include <curlpp/Options.hpp>
10 #include "Telegram/http_build_query.h"
11 #include "Telegram/json_encode.h"
12 #include "Telegram/std.h"
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());
92 this->sendMessage(message,
SSTR(chat_id));
97 std::map<std::string, std::string> params;
98 params[
"chat_id"] = chat_id;
99 params[
"text"] = message;
101 this->apiRequestJson(
"sendMessage", params);
111 return(this->command_map);
117 std::cout <<
"Content-type: text/html\r\n\r\n" << std::endl;
125 parameters[
"method"] = method;
128 Json::StyledWriter w;
130 for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
131 jValues[(*it).first] = (*it).second;
136 std::string url = this->api_url + method +
"?" +
http_build_query(parameters);
138 handle.setOpt(cURLpp::Options::Url(url));
139 handle.setOpt(cURLpp::Options::ConnectTimeout(5));
140 handle.setOpt(cURLpp::Options::Timeout(60));
144 code = cURLpp::Infos::ResponseCode::get(handle);
146 catch(cURLpp::LogicError &e) {
149 catch(cURLpp::RuntimeError &e) {
152 catch(std::exception &e) {
162 parameters[
"method"] = method;
166 std::list<std::string> header;
167 header.push_back(
"Content-Type: application/json");
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)));
176 catch(cURLpp::LogicError &e) {
179 catch(cURLpp::RuntimeError &e) {
182 catch(std::exception &e) {
190 std::string sCommand;
192 std::vector<std::string> vCmdLine =
split(cmd,
' ');
194 if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
195 sCommand = vCmdLine[0];
196 vCmdLine.erase(vCmdLine.begin());
197 TCommand cs = this->command_map[sCommand];
198 sResult = (*cs.
callback)(
this, vCmdLine);
200 sResult =
"Unknown command";
std::string command
The command, e.g. /help.
void sendMessage(std::string, Json::Int64)
std::vector< std::string > split(const std::string &text, char sep)
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
std::string http_build_query(std::map< std::string, std::string > data)
void apiRequestJson(std::string, std::map< std::string, std::string >)
std::string json_encode(std::map< std::string, std::string > parameters)