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"
40 this->api_url =
API_URL + token +
"/";
54 this->command_map[cmd.
command] = cmd;
67 std::map<std::string, std::string> params;
70 this->apiRequest(
"setWebhook", params);
85 if (entities.size() && (entities[0]->getType() ==
"bot_command")) {
86 this->sendMessage(this->processCommand(this->msg->getText()), this->msg->getChat()->getId());
88 this->sendMessage(
"Hello " + this->msg->getChat()->getUsername() +
", you told me: '" + msg->getText() +
"'", this->msg->getChat()->getId());
100 std::map<std::string, std::string> params;
102 obj = this->apiRequestJson(
"getMe", params);
117 return(this->sendMessage(message,
SSTR(chat_id)));
130 std::map<std::string, std::string> params;
133 params[
"chat_id"] = chat_id;
134 params[
"text"] = message;
136 obj = this->apiRequestJson(
"sendMessage", params);
151 return(this->sendPhoto(URL,
SSTR(chat_id)));
166 std::map<std::string, std::string> params;
168 params[
"chat_id"] = chat_id;
169 params[
"photo"] = URL;
171 if ((URL.substr(0, 8) ==
"https://") || (URL.substr(0, 7) ==
"http://")) {
172 obj = this->apiRequestJson(
"sendPhoto", params);
175 if (URL.substr(0, 7) ==
"file://") {
176 obj = this->apiRequestFile(URL.substr(7, std::string::npos),
"photo", chat_id);
194 return(this->command_map);
205 std::cout <<
"Content-type: text/html\r\n\r\n" << std::endl;
218 parameters[
"method"] = method;
221 Json::StyledWriter w;
223 for(std::map<std::string, std::string>::iterator it = parameters.begin(); it != parameters.end(); ++it) {
224 jValues[(*it).first] = (*it).second;
229 std::string url = this->api_url + method +
"?" +
http_build_query(parameters);
231 handle.setOpt(cURLpp::Options::Url(url));
232 handle.setOpt(cURLpp::Options::ConnectTimeout(5));
233 handle.setOpt(cURLpp::Options::Timeout(60));
237 code = cURLpp::Infos::ResponseCode::get(handle);
239 catch(cURLpp::LogicError &e) {
242 catch(cURLpp::RuntimeError &e) {
245 catch(std::exception &e) {
260 std::stringstream result;
261 parameters[
"method"] = method;
265 cURLpp::Cleanup cleaner;
267 std::list<std::string> header;
268 header.push_back(
"Content-Type: application/json");
270 handle.setOpt(cURLpp::Options::Url(this->api_url));
271 handle.setOpt(cURLpp::Options::ConnectTimeout(5));
272 handle.setOpt(cURLpp::Options::Timeout(60));
273 handle.setOpt(cURLpp::Options::HttpHeader(header));
274 handle.setOpt(cURLpp::Options::PostFields(
json_encode(parameters)));
275 handle.setOpt(cURLpp::Options::WriteStream(&result));
279 catch(cURLpp::LogicError &e) {
282 catch(cURLpp::RuntimeError &e) {
285 catch(std::exception &e) {
289 Json::Reader jreader;
291 jreader.parse(result.str(), obj);
307 std::stringstream result;
308 cURLpp::Forms formParts;
312 cURLpp::Cleanup cleaner;
314 handle.setOpt(cURLpp::Options::Url(this->api_url +
"send" + type));
317 parts.push_back(
new cURLpp::FormParts::Content(
"chat_id", chat_id));
318 parts.push_back(
new cURLpp::FormParts::File(type, filename));
319 handle.setOpt(cURLpp::Options::HttpPost(parts));
325 catch(cURLpp::LogicError &e) {
328 catch(cURLpp::RuntimeError &e) {
331 catch(std::exception &e) {
335 Json::Reader jreader;
337 jreader.parse(result.str(), obj);
355 std::string sCommand;
357 std::vector<std::string> vCmdLine =
split(cmd,
' ');
359 if((vCmdLine.size() > 0) && (this->command_map.find(vCmdLine[0]) != this->command_map.end())) {
360 sCommand = vCmdLine[0];
361 vCmdLine.erase(vCmdLine.begin());
362 TCommand cs = this->command_map[sCommand];
363 sResult = (*cs.
callback)(
this, vCmdLine);
365 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)
Json::Value apiRequestFile(std::string, std::string, 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)