From 602e9b62d85e627a4888bec4f456c1320c5f123d Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 18 Nov 2025 17:30:43 +0300 Subject: [PATCH] Started creating structure of bot interface --- modules/bot/front/.gitignore | 1 + modules/bot/front/CMakeLists.txt | 23 ++++++++++++++++ modules/bot/front/include/KeyboardFactory.hpp | 7 +++++ modules/bot/front/include/constants.hpp | 14 ++++++++++ modules/bot/front/include/front.hpp | 26 +++++++++++++++++++ modules/bot/front/src/KeyboardFactory.cpp | 15 +++++++++++ modules/bot/front/src/front.cpp | 21 +++++++++++++++ modules/bot/front/src/main.cpp | 15 +++++++++++ 8 files changed, 122 insertions(+) create mode 100644 modules/bot/front/.gitignore create mode 100644 modules/bot/front/CMakeLists.txt create mode 100644 modules/bot/front/include/KeyboardFactory.hpp create mode 100644 modules/bot/front/include/constants.hpp create mode 100644 modules/bot/front/include/front.hpp create mode 100644 modules/bot/front/src/KeyboardFactory.cpp create mode 100644 modules/bot/front/src/front.cpp create mode 100644 modules/bot/front/src/main.cpp diff --git a/modules/bot/front/.gitignore b/modules/bot/front/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/modules/bot/front/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/modules/bot/front/CMakeLists.txt b/modules/bot/front/CMakeLists.txt new file mode 100644 index 0000000..206c47f --- /dev/null +++ b/modules/bot/front/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.10.2) +project(AnimeBot) + +file(GLOB SOURCES "src/*.cpp") +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") +set(Boost_USE_MULTITHREADED ON) + +find_package(Threads REQUIRED) +find_package(OpenSSL REQUIRED) +find_package(Boost COMPONENTS system REQUIRED) +find_package(CURL) +include_directories(/usr/local/include ${OPENSSL_INCLUDE_DIR} ${Boost_INCLUDE_DIR}) +include_directories(include/) +if (CURL_FOUND) + include_directories(${CURL_INCLUDE_DIRS}) + add_definitions(-DHAVE_CURL) +endif() + +add_executable(AnimeBot ${SOURCES}) + +target_link_libraries(AnimeBot /usr/local/lib/libTgBot.a ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES}) diff --git a/modules/bot/front/include/KeyboardFactory.hpp b/modules/bot/front/include/KeyboardFactory.hpp new file mode 100644 index 0000000..9e15982 --- /dev/null +++ b/modules/bot/front/include/KeyboardFactory.hpp @@ -0,0 +1,7 @@ +#include + +class KeyboardFactory { +public: + /// Create keyboard for main menu + static TgBot::InlineKeyboardMarkup::Ptr createMainMenu(); +}; diff --git a/modules/bot/front/include/constants.hpp b/modules/bot/front/include/constants.hpp new file mode 100644 index 0000000..212a88f --- /dev/null +++ b/modules/bot/front/include/constants.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +namespace BotConstants { + namespace Button { + const std::string FIND_ANIME = "Найти аниме"; + const std::string MY_TITLES = "Мои тайтлы"; + } + namespace Callback { + const std::string FIND_ANIME = "action:find_anime"; + const std::string MY_TITLES = "navigation:my_titles"; + } +} diff --git a/modules/bot/front/include/front.hpp b/modules/bot/front/include/front.hpp new file mode 100644 index 0000000..f2c6485 --- /dev/null +++ b/modules/bot/front/include/front.hpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#include + +class AnimeBot { +private: + std::string token; + TgBot::Bot bot; + +public: + /// Init Bot + AnimeBot(const std::string& token); + + /// Main menu + void setupHandlers(); + + /// Get TgBot::Bot + TgBot::Bot& getBot(); + + /// Function creates main menu and sends it to user with chatId + void sendMainMenu(int64_t chatId); +}; diff --git a/modules/bot/front/src/KeyboardFactory.cpp b/modules/bot/front/src/KeyboardFactory.cpp new file mode 100644 index 0000000..1bd2cab --- /dev/null +++ b/modules/bot/front/src/KeyboardFactory.cpp @@ -0,0 +1,15 @@ +#include "KeyboardFactory.hpp" +#include "constants.hpp" + +TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMainMenu() { + auto keyboard = std::make_shared(); + TgBot::InlineKeyboardButton::Ptr button1(new TgBot::InlineKeyboardButton); + button1->text = BotConstants::Button::FIND_ANIME; + button1->callbackData = BotConstants::Callback::FIND_ANIME; + TgBot::InlineKeyboardButton::Ptr button2(new TgBot::InlineKeyboardButton); + button2->text = BotConstants::Button::MY_TITLES; + button2->callbackData = BotConstants::Callback::MY_TITLES; + + keyboard->inlineKeyboard = {{button1, button2}}; + return keyboard; +} diff --git a/modules/bot/front/src/front.cpp b/modules/bot/front/src/front.cpp new file mode 100644 index 0000000..d6a0b88 --- /dev/null +++ b/modules/bot/front/src/front.cpp @@ -0,0 +1,21 @@ +#include "front.hpp" +#include "KeyboardFactory.hpp" + +AnimeBot::AnimeBot(const std::string& token) : bot(token) { + setupHandlers(); +} + +void AnimeBot::setupHandlers() { + bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message) { + sendMainMenu(message->chat->id); + }); +} + +void AnimeBot::sendMainMenu(int64_t chatId) { + auto keyboard = KeyboardFactory::createMainMenu(); + bot.getApi().sendMessage(chatId, "Главное меню", nullptr, nullptr, keyboard); +} + +TgBot::Bot& AnimeBot::getBot() { + return bot; +} diff --git a/modules/bot/front/src/main.cpp b/modules/bot/front/src/main.cpp new file mode 100644 index 0000000..9632360 --- /dev/null +++ b/modules/bot/front/src/main.cpp @@ -0,0 +1,15 @@ +#include + +int main() { + AnimeBot bot(getenv("TOKEN")); + TgBot::TgLongPoll longPoll(bot.getBot()); + + while (true) { + try { + longPoll.start(); + } catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + } + } + return 0; +}