Started creating structure of bot interface
This commit is contained in:
parent
879a7981cd
commit
602e9b62d8
8 changed files with 122 additions and 0 deletions
1
modules/bot/front/.gitignore
vendored
Normal file
1
modules/bot/front/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
build/
|
||||
23
modules/bot/front/CMakeLists.txt
Normal file
23
modules/bot/front/CMakeLists.txt
Normal 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})
|
||||
7
modules/bot/front/include/KeyboardFactory.hpp
Normal file
7
modules/bot/front/include/KeyboardFactory.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#include <tgbot/tgbot.h>
|
||||
|
||||
class KeyboardFactory {
|
||||
public:
|
||||
/// Create keyboard for main menu
|
||||
static TgBot::InlineKeyboardMarkup::Ptr createMainMenu();
|
||||
};
|
||||
14
modules/bot/front/include/constants.hpp
Normal file
14
modules/bot/front/include/constants.hpp
Normal 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";
|
||||
}
|
||||
}
|
||||
26
modules/bot/front/include/front.hpp
Normal file
26
modules/bot/front/include/front.hpp
Normal 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);
|
||||
};
|
||||
15
modules/bot/front/src/KeyboardFactory.cpp
Normal file
15
modules/bot/front/src/KeyboardFactory.cpp
Normal 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;
|
||||
}
|
||||
21
modules/bot/front/src/front.cpp
Normal file
21
modules/bot/front/src/front.cpp
Normal 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;
|
||||
}
|
||||
15
modules/bot/front/src/main.cpp
Normal file
15
modules/bot/front/src/main.cpp
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue