tictactoebot/src/main.cpp

92 lines
1.9 KiB
C++

#include <nlohmann/json.hpp>
#include <string>
#include <fstream>
#include <functional>
#include <signal.h>
#include "TAPIInlineQuery.h"
#include "TAPIMarkup.h"
#include "TAPIManager.h"
#include "Log.h"
#include "bot.h"
#include "config.h"
namespace tapi = TelegramAPI;
class CommandStart : public TelegramAPI::Command {
public:
CommandStart(Bot& bot) : TelegramAPI::Command("/start", "Start the Bot"), bot(bot) {}
virtual bool process(TelegramAPI::Manager* m, const TelegramAPI::Message& msg) {
return bot.handleStart(m, msg);
}
private:
Bot& bot;
};
static bool run = true;
void sig_handler(int sig_num) {
Log::info << "signalHandler triggered";
run = false;
(void) sig_num;
}
static bool loadConfig(Conf& out) {
std::ifstream conf("tictactoebot.conf");
if(!conf) {
Log::error << "Could not open config file! tictactoebot.conf";
return false;
}
while(conf && !conf.eof()) {
std::string key, value;
std::getline(conf, key, '=');
std::getline(conf, value);
if(key == "token") {
out.token = value;
} else if(key == "apiurl") {
out.apiurl = value;
} else {
Log::info << "unused config value: \"" << key << "\": \"" << value << '"';
}
}
conf.close();
return true;
}
int main(int argv, char** argc) {
Log::init();
Log::setConsoleLogLevel(Log::Level::TRACE);
Log::setColoredOutput(true);
Conf conf;
if(!loadConfig(conf)) return 1;
TelegramAPI::Manager tmgr(conf.token);
Bot bot(&tmgr, conf);
Log::info << "Starting.";
tmgr.registerCommand(std::make_unique<CommandStart>(bot));
namespace pl = std::placeholders;
tmgr.setMessageHandler(std::bind(&Bot::handleMessage, &bot, pl::_1, pl::_2));
tmgr.setCallbackQueryHandler(std::bind(&Bot::handleCallback, &bot, pl::_1, pl::_2));
tmgr.setMyCommands();
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
while (run) {
tmgr.getUpdates();
}
bot.stop();
Log::stop();
return 0;
}