Moved message handling to CommandHandler class
This commit is contained in:
parent
60ea2b8572
commit
84b087ba4d
4 changed files with 81 additions and 44 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
56
BeezleBot/commandhandler.cpp
Normal file
56
BeezleBot/commandhandler.cpp
Normal 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());
|
||||
}
|
||||
}
|
17
BeezleBot/commandhandler.h
Normal file
17
BeezleBot/commandhandler.h
Normal 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);
|
||||
};
|
Loading…
Reference in a new issue