Moved message handling to CommandHandler class

This commit is contained in:
Michael Mandl 2018-05-10 12:43:49 +02:00
parent 60ea2b8572
commit 84b087ba4d
4 changed files with 81 additions and 44 deletions

View file

@ -1,4 +1,5 @@
#include "settings.h" #include "settings.h"
#include "commandhandler.h"
#include "tgbot/bot.h" #include "tgbot/bot.h"
#include "dokuwiki.h" #include "dokuwiki.h"
@ -11,8 +12,7 @@ int main(int argc, char **argv)
{ {
Settings settings(argc, argv); Settings settings(argc, argv);
DokuWiki wiki(settings.wikiUrl + "/lib/exe/xmlrpc.php", CommandHandler commandHandler(settings);
settings.wikiUser, settings.wikiPassword);
tgbot::LongPollBot bot(settings.telegramToken); tgbot::LongPollBot bot(settings.telegramToken);
@ -35,48 +35,11 @@ int main(int argc, char **argv)
}, },
"/help"); "/help");
bot.callback([&wiki, &settings](const tgbot::types::Message message, bot.callback([&commandHandler](const tgbot::types::Message message,
const tgbot::methods::Api &api) { const tgbot::methods::Api &api) {
if (message.text != nullptr && message.from != nullptr commandHandler.handle(message, api);
&& 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());
}
}
}
}); });
bot.start(); bot.start();
} }
catch (std::runtime_error &e) catch (std::runtime_error &e)

View file

@ -4,9 +4,10 @@ project(BeezleBot LANGUAGES CXX)
find_package(Boost 1.55.0 COMPONENTS program_options REQUIRED) find_package(Boost 1.55.0 COMPONENTS program_options REQUIRED)
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
BeezleBot.cpp BeezleBot.cpp
settings.cpp settings.cpp
commandhandler.cpp
) )
set_target_properties(${PROJECT_NAME} set_target_properties(${PROJECT_NAME}
@ -14,7 +15,7 @@ set_target_properties(${PROJECT_NAME}
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
PRIVATE PRIVATE
Boost::program_options Boost::program_options
xxtelebot xxtelebot
DokuWiki DokuWiki

View file

@ -0,0 +1,56 @@
#include "commandhandler.h"
#include <sstream>
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());
}
}

View file

@ -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);
};