refactored message handling to separated class

This commit is contained in:
Michael Mandl 2018-03-29 13:50:44 +02:00
parent 2983f0302d
commit 0489aa74e2
5 changed files with 65 additions and 46 deletions

View file

@ -6,6 +6,7 @@ project(SimpleBot LANGUAGES CXX)
add_executable(${PROJECT_NAME}
SimpleBot.cpp
MessageHandler.cpp
)
target_link_libraries(${PROJECT_NAME}

View file

@ -0,0 +1,36 @@
#include "MessageHandler.h"
#include <sstream>
void MessageHandler::replyUserUnknown(const tgbot::types::Message &message, const tgbot::methods::Api &api)
{
api.sendMessage(std::to_string(message.chat.id), "Sorry, I don't know who you are.");
}
void MessageHandler::handle(const tgbot::types::Message &message, const tgbot::methods::Api &api)
{
if (message.from == nullptr || knownUsers.find(*message.from->username) == knownUsers.cend())
{
replyUserUnknown(message, api);
return;
}
std::cout << message.from->firstName
<< " (" << *message.from->username << "): "
<< (message.text != nullptr ? *message.text : "<no text>") << std::endl;
std::ostringstream reply;
reply << "You said";
if (message.text != nullptr)
{
reply << ": '" << *message.text << "'";
}
else
{
reply << " illegible things";
}
api.sendMessage(std::to_string(message.chat.id), reply.str());
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "tgbot/bot.h"
#include <string>
#include <set>
class MessageHandler
{
private:
const std::set<std::string> knownUsers = { "mandlm" };
private:
void replyUserUnknown(const tgbot::types::Message &message, const tgbot::methods::Api &api);
public:
void handle(const tgbot::types::Message &message, const tgbot::methods::Api &api);
};

View file

@ -1,3 +1,4 @@
#include "MessageHandler.h"
#include "tgbot/bot.h"
#include <boost/program_options.hpp>
@ -6,8 +7,6 @@
#include <string>
#include <iostream>
static const std::set<std::string> knownUsers = { "mandlm" };
struct ProgramSettings
{
std::string token;
@ -37,48 +36,6 @@ ProgramSettings getProgramSettings(int argc, char **argv)
return ProgramSettings{ configuredOptions["token"].as<std::string>() };
}
std::string fullName(const tgbot::types::User &user)
{
std::ostringstream fullName;
fullName << user.firstName;
if (user.lastName != nullptr)
{
fullName << " " << *user.lastName;
}
return fullName.str();
}
void replyUserUnknown(const tgbot::types::Message &message, const tgbot::methods::Api &api)
{
api.sendMessage(std::to_string(message.chat.id), "Sorry, I don't know who you are.");
}
void handleMessage(const tgbot::types::Message message, const tgbot::methods::Api &api)
{
if (message.from == nullptr || knownUsers.find(*message.from->username) == knownUsers.cend())
{
replyUserUnknown(message, api);
return;
}
std::cout << fullName(*message.from)
<< " (" << *message.from->username << "): "
<< (message.text != nullptr ? *message.text : "<no text>") << std::endl;
api.sendMessage(std::to_string(message.chat.id), "You said:");
if (message.text != nullptr)
{
api.sendMessage(std::to_string(message.chat.id), *message.text);
}
else
{
api.sendMessage(std::to_string(message.chat.id), "illegible things");
}
}
int main(int argc, char **argv)
{
try
@ -91,7 +48,13 @@ int main(int argc, char **argv)
std::cout << "connected as " << bot.getMe().firstName << std::endl;
bot.callback(handleMessage);
MessageHandler messageHandler;
bot.callback([&messageHandler]
(const tgbot::types::Message message, const tgbot::methods::Api &api)
{
messageHandler.handle(message, api);
});
bot.start();
}
catch (std::runtime_error &e)

@ -1 +1 @@
Subproject commit 286e252e99a882b2a77960132da19a12d2222341
Subproject commit 1197ede8ebee43aaaa0caf9c54fc7b1bd74447c0