#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::vector<Handler::CommandHandler> commands;
commands.emplace_back(
Handler::CommandHandler{"wiki", Handler::wikiHandler, createPlainDescriptionFromText(
"Sends you to Wikipedia!")});
commands.emplace_back(
Handler::CommandHandler{"stop", Handler::stopHandler, createPlainDescriptionFromText(
"Stops the bot"), &app});
commands.emplace_back(
Handler::CommandHandler{"mensa", Handler::mensaHandler, createPlainDescriptionFromText(
"{adlershof, nord, sued} Shows the daily menu of the mensa at various places.")});
commands.emplace_back(
Handler::CommandHandler{"klinger", Handler::klingerHandler, createPlainDescriptionFromText(
"Greats in french. Bonjour!")});
commands.emplace_back(
Handler::CommandHandler{"relation", Handler::relationShipHandler, 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.emplace_back(Handler::CommandHandler{"help", Handler::helpHandler, createPlainDescriptionFromText(
"This is my holy manual."), &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"));
auto itor = std::find_if(
commands.begin(),
commands.end(),
[command](Handler::CommandHandler const &handler) {
return handler.command == command;
});
if (itor != commands.end()) {
return crow::response{itor->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;
}