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());
92 this->sendMessage(message,
SSTR(chat_id));
100 std::map<std::string, std::string> params;
101 params[
"chat_id"] = chat_id;
102 params[
"text"] = message;
104 this->apiRequestJson(
"sendMessage", params);
112 this->sendPhoto(URL,
SSTR(chat_id));
120 std::map<std::string, std::string> params;
122 params[
"chat_id"] = chat_id;
123 params[
"photo"] = URL;
125 this->apiRequestJson(
"sendPhoto", params);
135 return(this->command_map);
141 std::cout <<
"Content-type: text/html\r\n\r\n" << std::endl;
149 parameters[
"method"] = method;
152 Json::StyledWriter w;
154 for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
155 jValues[(*it).first] = (*it).second;
160 std::string url = this->api_url + method +
"?" +
http_build_query(parameters);
162 handle.setOpt(cURLpp::Options::Url(url));
163 handle.setOpt(cURLpp::Options::ConnectTimeout(5));
164 handle.setOpt(cURLpp::Options::Timeout(60));
168 code = cURLpp::Infos::ResponseCode::get(handle);
170 catch(cURLpp::LogicError &e) {
173 catch(cURLpp::RuntimeError &e) {
176 catch(std::exception &e) {
186 parameters[
"method"] = method;
190 std::list<std::string> header;
191 header.push_back(
"Content-Type: application/json");
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)));
200 catch(cURLpp::LogicError &e) {
203 catch(cURLpp::RuntimeError &e) {
206 catch(std::exception &e) {
214 std::string sCommand;
216 std::vector<std::string> vCmdLine =
split(cmd,
' ');
218 if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
219 sCommand = vCmdLine[0];
220 vCmdLine.erase(vCmdLine.begin());
221 TCommand cs = this->command_map[sCommand];
222 sResult = (*cs.
callback)(
this, vCmdLine);
224 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()
void sendPhoto(std::string, Json::Int64)
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)