#include #include "include/toml.hpp" #include "include/json.hpp" #include #include #include using json = nlohmann::json; int main() { auto config = toml::parse_file("../config.toml"); TgBot::Bot bot(config["telegram"]["apikey"].value_or("")); bot.getEvents().onAnyMessage([&bot, &config](TgBot::Message::Ptr message){ std::istringstream msg(message->text); std::string command, commandtmp, arguments; std::cout << "incomming Message: " << message->text << " from " << message->chat->username << "\n"; std::getline(msg, commandtmp, ' '); std::getline(msg, arguments); //std::cout << "command: " << commandtmp << "\n"; if(!(commandtmp.at(0) == '/')){ return 0; } else{ command = commandtmp.substr(1, commandtmp.length()-1); } json request; request["command"] = command; request["arguments"] = arguments; request["session"] = std::to_string(message->from->id); cpr::Response r = cpr::Post(cpr::Url{config["n_core"]["adress"].value_or("localhost:18080")}, cpr::Body{request.dump()}); if(r.status_code != 200) { std::cerr << "Error " << r.status_code << " aborting\n"; return 1; } auto jreply = json::parse(r.text); std::string outMessage; if(jreply["success"] == true) { for (auto x : jreply["reply"].items()) { if (x.value().at("annotations").at(0).at("type") == "none") { outMessage += x.value().at("text"); } else if (x.value().at("annotations").at(0).at("type") == "bold") { outMessage.append("").append(x.value().at("text")).append(""); } else if (x.value().at("annotations").at(0).at("type") == "link") { outMessage.append("").append(x.value().at("text")).append(""); } else if (x.value().at("annotations").at(0).at("type") == "italic") { outMessage.append("").append(x.value().at("text")).append(""); } else if (x.value().at("annotations").at(0).at("type") == "strikethrough") { outMessage.append("").append(x.value().at("text")).append(""); } else if (x.value().at("annotations").at(0).at("type") == "link") { outMessage.append("").append(x.value().at("text")).append(""); } else if (x.value().at("annotations").at(0).at("type") == "command") { outMessage.append("/").append(x.value().at("text")); } } bot.getApi().sendMessage(message->chat->id, outMessage, false, 0, nullptr, "HTML"); } else { bot.getApi().sendMessage(message->chat->id, "Command was not successfully executed."); } return 0; }); try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); TgBot::TgLongPoll longPoll(bot); while (true) { //printf("Long poll started\n"); longPoll.start(); } } catch (TgBot::TgException& e) { printf("error: %s\n", e.what()); } return 0; }