diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/main.cpp b/src/main.cpp index c704f39..42a642a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,19 +11,22 @@ #define NO_SUCH_COMMAND std::string("no_such_command") typedef crow::json::wvalue json; -typedef std::function<json(std::string const &arguments, std::string const &session, void *payload)> handler_func; +typedef std::function<json(std::string const &arguments, std::string const &session, void *payload)> handler_function; struct CommandHandler { [[nodiscard]] json exec(std::string const &arguments, std::string const &session) const { return func(arguments, session, payload); } - handler_func func; + handler_function func{nullptr}; + std::string description{""}; void *payload{nullptr}; }; /* ------------------------------------------------------------------------------------------------------------------ */ +json helpHandler(std::string const &arguments, std::string const &session, void *payload); + json stopHandler(std::string const &arguments, std::string const &session, void *payload); json wikiHandler(std::string const &arguments, std::string const &session, void *payload); @@ -38,6 +41,7 @@ json create_annotation(Reply::AnnotationType annotation = Reply::AnnotationType: std::string const &extra = EMPTY_STRING); json create_text(std::string const &text); + json create_text(std::string const &text, std::vector<json> &&annotations); json create_response(std::vector<json> &&reply, std::string const &session = NULL_STRING, bool success = true); @@ -48,15 +52,18 @@ int main() { crow::SimpleApp app; // command --> handler - static std::unordered_map<std::string, CommandHandler> const commands{ - {"stop", {stopHandler, &app}}, - {"wiki", {wikiHandler}}, - {"klinger", {klingerHandler}}, + std::unordered_map<std::string, CommandHandler> commands{ + {"stop", {.func = stopHandler, .description = "Stops the bot", .payload = &app}}, + {"wiki", {.func = wikiHandler, .description = "Send you to wikipedia"}}, + {"klinger", {.func = klingerHandler, .description = "Greats in french. Bonjour!"}}, }; + commands.insert({"help", + {.func = helpHandler, .description = "This is my holy manual.", .payload = &commands}}); + CROW_ROUTE(app, "/") .methods("POST"_method) - ([&app](crow::request const &request) { + ([&commands](crow::request const &request) { auto data = crow::json::load(request.body); if (!data) @@ -104,6 +111,36 @@ json wikiHandler(std::string const &arguments, std::string const &session, void return create_response(std::move(reply_vec), session, true); } +json helpHandler(std::string const &arguments, std::string const &session, void *payload) { + auto commands = *(std::unordered_map<std::string, CommandHandler> *) payload; + + std::vector<json> reply_vec; + reply_vec.push_back(std::move(create_text( + "Reading from the gospel according to saint N,\n" + "And you shall feast upon the animals intestines as I do in the offering for they will make you strong.\n" + "\n" + "Dear my fellow users, these are my official commands, which are offered to my believers.\n" + "\n"))); + + for (auto const &itor : commands) { + std::vector<json> commandAnnotations; + commandAnnotations.push_back(std::move(create_annotation(Reply::AnnotationType::italics))); + commandAnnotations.push_back(std::move(create_annotation(Reply::AnnotationType::underline))); + + reply_vec.push_back(std::move(create_text("- "))); + reply_vec.push_back(std::move(create_text(itor.first, std::move(commandAnnotations)))); + reply_vec.push_back(std::move(create_text(" " + itor.second.description + "\n"))); + } + + reply_vec.push_back(std::move(create_text( + "\n" + "Thanks for using me. Takk skal du ha.\n" + "Bussy\n" + "N"))); + + return create_response(std::move(reply_vec), session, true); +} + json stopHandler(std::string const &arguments, std::string const &session, void *payload) { auto app = (crow::SimpleApp *) payload; app->stop(); |