From f78a4dcf7b22d818f47776f831be07a3e4a902e9 Mon Sep 17 00:00:00 2001 From: Dmitriy Gorshenin Date: Sat, 24 Feb 2024 22:48:35 +0300 Subject: [PATCH] firstAttemt --- CMakeLists.txt | 15 +++++++++++ bot.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ bot.h | 25 ++++++++++++++++++ main.cpp | 8 ++++++ 4 files changed, 117 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 bot.cpp create mode 100644 bot.h create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2b1acf8 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.10) + +project(TelegramBot) + +set(CMAKE_CXX_STANDARD 14) + +find_package(Threads REQUIRED) + +add_executable(${PROJECT_NAME} main.cpp bot.cpp) + +target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + +target_link_libraries(${PROJECT_NAME} PRIVATE TgBot Threads::Threads) +find_package(tgbot-cpp REQUIRED) +target_link_libraries(${PROJECT_NAME} PRIVATE TgBot) \ No newline at end of file diff --git a/bot.cpp b/bot.cpp new file mode 100644 index 0000000..6aff364 --- /dev/null +++ b/bot.cpp @@ -0,0 +1,69 @@ +#include "bot.h" + + +namespace fs = std::filesystem; + +Bot::Bot(const std::string& token) : botToken(token), bot(token) { + // Инициализация бота + bot.getEvents().onCommand("list", [this](TgBot::Message::Ptr message) { + handleCommand(message); + }); + bot.getEvents().onCommand("get", [this](TgBot::Message::Ptr message) { + if (!message->text.empty()) { + sendFile(message, message->text); + } + }); +} + +void Bot::run() { + bot.getApi().deleteWebhook(); + TgBot::TgLongPoll longPoll(bot); + + while (true) { + try { + longPoll.start(); + } catch (const std::exception& e) { + std::cerr << "Exception: " << e.what() << std::endl; + } + } +} + +void Bot::handleCommand(const TgBot::Message::Ptr& message) { + if (message->text == "/list") { + std::async(std::launch::async, &Bot::updateCacheAsync, this); + sendFileList(message); + } +} + +void Bot::updateCacheAsync() { + std::lock_guard lock(cacheMutex); + fileCache.clear(); + + for (const auto& entry : fs::directory_iterator("/home/pi/study")) { + fileCache.push_back(entry.path().filename().string()); + } +} + +void Bot::sendFileList(const TgBot::Message::Ptr& message) { + std::lock_guard lock(cacheMutex); + std::string fileList = "Current files:\n"; + for (const auto& file : fileCache) { + fileList += file + "\n"; + } + bot.getApi().sendMessage(message->chat->id, fileList); +} + + +void Bot::sendFile(const TgBot::Message::Ptr& message, const std::string& fileName) { + std::ifstream file(fileName, std::ios::binary); + if (file) { + // Читаем содержимое файла в строковый поток (stringstream) + std::stringstream fileContents; + fileContents << file.rdbuf(); + + // Отправляем документ, передавая строковый поток в конструктор InputFile::fromFile + bot.getApi().sendDocument(message->chat->id, TgBot::InputFile::fromFile(fileName, fileContents.str())); + } else { + bot.getApi().sendMessage(message->chat->id, "File not found: " + fileName); + } +} \ No newline at end of file diff --git a/bot.h b/bot.h new file mode 100644 index 0000000..d7f75ba --- /dev/null +++ b/bot.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include +#include +#include +#include + +class Bot { +public: + Bot(const std::string& token); + void run(); + +private: + void handleCommand(const TgBot::Message::Ptr& message); + void sendFileList(const TgBot::Message::Ptr& message); + void sendFile(const TgBot::Message::Ptr& message, const std::string& fileName); + void updateCacheAsync(); + +private: + std::string botToken; + TgBot::Bot bot; + std::vector fileCache; + std::mutex cacheMutex; +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cf0cc41 --- /dev/null +++ b/main.cpp @@ -0,0 +1,8 @@ +#include "bot.h" + +int main() { + const std::string token = "YOUR_BOT_TOKEN"; + Bot bot(token); + bot.start(); + return 0; +} \ No newline at end of file