#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
#include "crow.h"
#include "Handler.hpp"
#include "Response.hpp"
/* BEGIN Handlers */
#include "GetEssen.hpp"
#include "SimpleHandlers.hpp"
#include "RelationshipHandler.hpp"
/* END Handlers */
int main() {
crow::SimpleApp app;
auto createPlainDescriptionFromText = [] (std::string const &text) -> std::queue<crow::json::wvalue>* {
auto reply_queue = new std::queue<crow::json::wvalue>;
reply_queue->emplace(Response::create_text(text));
return reply_queue;
};
// command --> handler
std::unordered_map<std::string, Handler::CommandHandler> commands{
{"wiki", {.func = Handler::wikiHandler, .description = createPlainDescriptionFromText("Sends you to Wikipedia!")}},
{"stop", {.func = Handler::stopHandler, .description = createPlainDescriptionFromText("Stops the bot"), .payload = &app}},
{"mensa", {.func = Handler::mensaHandler, .description = createPlainDescriptionFromText("{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")}},
{"klinger", {.func = Handler::klingerHandler, .description = createPlainDescriptionFromText("Greats in french. Bonjour!")}},
{"relation", {.func = Handler::relationShipHandler, .description = createPlainDescriptionFromText("[name1] [name2] Shows the result of an odd astrological religious pseudo-algorithm, based on the fact how much blessing a relationship receives by the glorious N.")}},
};
commands.insert({"help",
{.func = Handler::helpHandler, .description = createPlainDescriptionFromText("This is my holy manual."), .payload = &commands}});
CROW_ROUTE(app, "/")
.methods("POST"_method)
([&commands](crow::request const &request) {
auto data = crow::json::load(request.body);
if (!data)
return crow::response(400);
if (!data.count("command"))
return crow::response(400, "malformed request: missing `command` field\n");
std::string command = (data.count("command") ? data["command"].s() : std::string(
"No such command!"));
std::string arguments = (data.count("arguments") ? data["arguments"].s() : std::string(""));
std::string session = (data.count("session") ? data["session"].s() : std::string("null"));
if (auto itor = commands.find(command); itor != commands.end()) {
auto handler = itor->second;
return crow::response{handler.exec(arguments, session)};
}
return crow::response{Response::simple_response("No such command!", "null", false)};
});
app.port(18080).multithreaded().run();
std::cout << "Stopped successfully" << std::endl;
}