From 84b087ba4d4748df03952659c4c951b5ff9c1e7b Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 10 May 2018 12:43:49 +0200 Subject: [PATCH] Moved message handling to CommandHandler class --- BeezleBot/BeezleBot.cpp | 47 ++++-------------------------- BeezleBot/CMakeLists.txt | 5 ++-- BeezleBot/commandhandler.cpp | 56 ++++++++++++++++++++++++++++++++++++ BeezleBot/commandhandler.h | 17 +++++++++++ 4 files changed, 81 insertions(+), 44 deletions(-) create mode 100644 BeezleBot/commandhandler.cpp create mode 100644 BeezleBot/commandhandler.h diff --git a/BeezleBot/BeezleBot.cpp b/BeezleBot/BeezleBot.cpp index 6dab1de..f2cb05c 100644 --- a/BeezleBot/BeezleBot.cpp +++ b/BeezleBot/BeezleBot.cpp @@ -1,4 +1,5 @@ #include "settings.h" +#include "commandhandler.h" #include "tgbot/bot.h" #include "dokuwiki.h" @@ -11,8 +12,7 @@ int main(int argc, char **argv) { Settings settings(argc, argv); - DokuWiki wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php", - settings.wikiUser, settings.wikiPassword); + CommandHandler commandHandler(settings); tgbot::LongPollBot bot(settings.telegramToken); @@ -35,48 +35,11 @@ int main(int argc, char **argv) }, "/help"); - bot.callback([&wiki, &settings](const tgbot::types::Message message, + bot.callback([&commandHandler](const tgbot::types::Message message, const tgbot::methods::Api &api) { - if (message.text != nullptr && message.from != nullptr - && message.from->username != nullptr) - { - if (settings.telegramUsers.find(*message.from->username) - == settings.telegramUsers.end()) - { - api.sendMessage( - std::to_string(message.chat.id), "Unknown user!"); - } - else - { - std::ostringstream logMessage; - logMessage << message.from->firstName << " (" - << *message.from->username << ")" - << ":\\\\ " << *message.text; - api.getLogger().info(logMessage.str()); - - std::ostringstream wikiMessage; - wikiMessage << "\n" << logMessage.str() << "\n"; - try - { - wiki.appendToPage("beezletest", wikiMessage.str()); - - std::ostringstream pageUrl; - pageUrl << settings.wikiUrl << "/doku.php?id=" - << "beezletest"; - api.sendMessage(std::to_string(message.chat.id), - "Stored to wiki at " + pageUrl.str()); - } - catch (std::runtime_error &e) - { - std::ostringstream reply; - reply << "Error writing to wiki: " << e.what(); - api.getLogger().error(reply.str()); - api.sendMessage( - std::to_string(message.chat.id), reply.str()); - } - } - } + commandHandler.handle(message, api); }); + bot.start(); } catch (std::runtime_error &e) diff --git a/BeezleBot/CMakeLists.txt b/BeezleBot/CMakeLists.txt index 9554a49..3bc3ac7 100644 --- a/BeezleBot/CMakeLists.txt +++ b/BeezleBot/CMakeLists.txt @@ -4,9 +4,10 @@ project(BeezleBot LANGUAGES CXX) find_package(Boost 1.55.0 COMPONENTS program_options REQUIRED) -add_executable(${PROJECT_NAME} +add_executable(${PROJECT_NAME} BeezleBot.cpp settings.cpp + commandhandler.cpp ) set_target_properties(${PROJECT_NAME} @@ -14,7 +15,7 @@ set_target_properties(${PROJECT_NAME} ) target_link_libraries(${PROJECT_NAME} - PRIVATE + PRIVATE Boost::program_options xxtelebot DokuWiki diff --git a/BeezleBot/commandhandler.cpp b/BeezleBot/commandhandler.cpp new file mode 100644 index 0000000..096ac75 --- /dev/null +++ b/BeezleBot/commandhandler.cpp @@ -0,0 +1,56 @@ +#include "commandhandler.h" + +#include + +using tgbot::methods::Api; +using tgbot::types::Message; + +CommandHandler::CommandHandler(const Settings &settings) + : m_settings(settings), m_wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php", + settings.wikiUser, settings.wikiPassword) +{ +} + +void CommandHandler::handle( + const tgbot::types::Message &message, const tgbot::methods::Api &api) +{ + if (message.text == nullptr || message.from == nullptr + || message.from->username == nullptr) + { + return; + } + + if (m_settings.telegramUsers.find(*message.from->username) + == m_settings.telegramUsers.end()) + { + api.sendMessage(std::to_string(message.chat.id), "Unknown user!"); + + return; + } + + std::ostringstream logMessage; + logMessage << message.from->firstName << " (" << *message.from->username + << ")" + << ":\\\\ " << *message.text; + api.getLogger().info(logMessage.str()); + + std::ostringstream wikiMessage; + wikiMessage << "\n" << logMessage.str() << "\n"; + try + { + m_wiki.appendToPage("beezletest", wikiMessage.str()); + + std::ostringstream pageUrl; + pageUrl << m_settings.wikiUrl << "/doku.php?id=" + << "beezletest"; + api.sendMessage(std::to_string(message.chat.id), + "Stored to wiki at " + pageUrl.str()); + } + catch (std::runtime_error &e) + { + std::ostringstream reply; + reply << "Error writing to wiki: " << e.what(); + api.getLogger().error(reply.str()); + api.sendMessage(std::to_string(message.chat.id), reply.str()); + } +} diff --git a/BeezleBot/commandhandler.h b/BeezleBot/commandhandler.h new file mode 100644 index 0000000..0260aec --- /dev/null +++ b/BeezleBot/commandhandler.h @@ -0,0 +1,17 @@ +#pragma once + +#include "settings.h" +#include "dokuwiki.h" +#include "tgbot/bot.h" + +class CommandHandler +{ +private: + Settings m_settings; + DokuWiki m_wiki; + +public: + CommandHandler(const Settings &settings); + + void handle(const tgbot::types::Message &message, const tgbot::methods::Api &api); +};