Started creating structure of bot interface

This commit is contained in:
Kirill 2025-11-18 17:30:43 +03:00
parent 879a7981cd
commit 602e9b62d8
8 changed files with 122 additions and 0 deletions

1
modules/bot/front/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
build/

View file

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

View file

@ -0,0 +1,7 @@
#include <tgbot/tgbot.h>
class KeyboardFactory {
public:
/// Create keyboard for main menu
static TgBot::InlineKeyboardMarkup::Ptr createMainMenu();
};

View file

@ -0,0 +1,14 @@
#pragma once
#include <string>
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";
}
}

View file

@ -0,0 +1,26 @@
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <string>
#include <tgbot/tgbot.h>
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);
};

View file

@ -0,0 +1,15 @@
#include "KeyboardFactory.hpp"
#include "constants.hpp"
TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMainMenu() {
auto keyboard = std::make_shared<TgBot::InlineKeyboardMarkup>();
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;
}

View file

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

View file

@ -0,0 +1,15 @@
#include <front.hpp>
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;
}