From 27f4281ce3772982cf40d57e5ec2691e07cf84fe Mon Sep 17 00:00:00 2001 From: Max Kusatz Date: Thu, 12 Nov 2020 15:43:54 +0100 Subject: fixed attachement bug --- config.toml | 2 +- main.cpp | 29 ++++++++++++++++------------- readme.md | 2 +- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/config.toml b/config.toml index 41195a9..b6b7765 100644 --- a/config.toml +++ b/config.toml @@ -2,6 +2,6 @@ apikey="742711802:AAGjnwP4o3Tq39fqdx-v-xjEzLR_AczKH48" [n_core] -adress="localhost:18080" +address="localhost:18080" port="18080" diff --git a/main.cpp b/main.cpp index 03cbf0d..9e0d4a4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,6 @@ #include #include "include/toml.hpp" #include "include/json.hpp" -#include #include #include @@ -14,17 +13,15 @@ int main() { TgBot::Bot bot(config["telegram"]["apikey"].value_or("")); - bot.getEvents().onAnyMessage([&bot, &config](TgBot::Message::Ptr message){ + bot.getEvents().onAnyMessage([&bot, &config](TgBot::Message::Ptr const &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) == '/')){ + + if(commandtmp.at(0) != '/'){ return 0; } else{ command = commandtmp.substr(1, commandtmp.length()-1); @@ -36,10 +33,11 @@ int main() { request["session"] = std::to_string(message->from->id); - cpr::Response r = cpr::Post(cpr::Url{config["n_core"]["adress"].value_or("localhost:18080")}, + cpr::Response r = cpr::Post(cpr::Url{config["n_core"]["address"].value_or("localhost:18080")}, cpr::Body{request.dump()}); if(r.status_code != 200) { - std::cerr << "Error " << r.status_code << " aborting\n"; + std::cerr << "Server gave back error " << r.status_code << ".\n"; + bot.getApi().sendMessage(message->chat->id, "The Server encountered an Error.\nWe are working on a Fix"); return 1; } @@ -51,29 +49,34 @@ int main() { if(jreply["success"] == true) { - for (auto x : jreply["reply"].items()) { + for (auto const &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") { + } else if (x.value().at("annotations").at(0).at("type") == "underline") { 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(""); + outMessage.append("").append(x.value().at("annotations").at(0).at("extra").at("annotations").at("text").dump()).append(""); } else if (x.value().at("annotations").at(0).at("type") == "command") { outMessage.append("/").append(x.value().at("text")); + } else if (x.value().at("annotations").at(0).at("type") == "attachment") { + outMessage =+ "Attachments are unfortunately not supported"; } } - bot.getApi().sendMessage(message->chat->id, outMessage, false, 0, nullptr, "HTML"); + if(!outMessage.empty()) { + bot.getApi().sendMessage(message->chat->id, outMessage, false, 0, nullptr, "HTML"); + } } else { - bot.getApi().sendMessage(message->chat->id, "Command was not successfully executed."); + outMessage.append("Command was not successfully executed.\n").append(jreply["reply"].at(0).at("text")); + bot.getApi().sendMessage(message->chat->id, outMessage); } return 0; }); diff --git a/readme.md b/readme.md index 7d90011..cf73af6 100644 --- a/readme.md +++ b/readme.md @@ -10,7 +10,7 @@ You need [CPR](https://github.com/whoshuu/cpr) and [tgbot-cpp](https://github.co make -j6 ##Config -Your config.toml file should look somthing like this: +Your config.toml file should look something like this: [telegram] apikey="insert Key here" -- cgit v1.2.3-54-g00ecf