From 0489aa74e25ce7af92e5a9f2f127aa97f1b19d98 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 29 Mar 2018 13:50:44 +0200 Subject: [PATCH] refactored message handling to separated class --- SimpleBot/CMakeLists.txt | 1 + SimpleBot/MessageHandler.cpp | 36 ++++++++++++++++++++++++ SimpleBot/MessageHandler.h | 19 +++++++++++++ SimpleBot/SimpleBot.cpp | 53 ++++++------------------------------ telegram-bot-api | 2 +- 5 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 SimpleBot/MessageHandler.cpp create mode 100644 SimpleBot/MessageHandler.h diff --git a/SimpleBot/CMakeLists.txt b/SimpleBot/CMakeLists.txt index ac7cfb4..51f9fc6 100644 --- a/SimpleBot/CMakeLists.txt +++ b/SimpleBot/CMakeLists.txt @@ -6,6 +6,7 @@ project(SimpleBot LANGUAGES CXX) add_executable(${PROJECT_NAME} SimpleBot.cpp + MessageHandler.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/SimpleBot/MessageHandler.cpp b/SimpleBot/MessageHandler.cpp new file mode 100644 index 0000000..d2b5d19 --- /dev/null +++ b/SimpleBot/MessageHandler.cpp @@ -0,0 +1,36 @@ +#include "MessageHandler.h" + +#include + +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 : "") << 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()); +} + diff --git a/SimpleBot/MessageHandler.h b/SimpleBot/MessageHandler.h new file mode 100644 index 0000000..e30ab60 --- /dev/null +++ b/SimpleBot/MessageHandler.h @@ -0,0 +1,19 @@ +#pragma once + +#include "tgbot/bot.h" + +#include +#include + +class MessageHandler +{ + private: + const std::set 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); +}; + diff --git a/SimpleBot/SimpleBot.cpp b/SimpleBot/SimpleBot.cpp index 24b64c2..cca7135 100644 --- a/SimpleBot/SimpleBot.cpp +++ b/SimpleBot/SimpleBot.cpp @@ -1,3 +1,4 @@ +#include "MessageHandler.h" #include "tgbot/bot.h" #include @@ -6,8 +7,6 @@ #include #include -static const std::set knownUsers = { "mandlm" }; - struct ProgramSettings { std::string token; @@ -37,48 +36,6 @@ ProgramSettings getProgramSettings(int argc, char **argv) return ProgramSettings{ configuredOptions["token"].as() }; } -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 : "") << 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) diff --git a/telegram-bot-api b/telegram-bot-api index 286e252..1197ede 160000 --- a/telegram-bot-api +++ b/telegram-bot-api @@ -1 +1 @@ -Subproject commit 286e252e99a882b2a77960132da19a12d2222341 +Subproject commit 1197ede8ebee43aaaa0caf9c54fc7b1bd74447c0