firstAttemt

This commit is contained in:
2024-02-24 22:48:35 +03:00
commit f78a4dcf7b
4 changed files with 117 additions and 0 deletions

15
CMakeLists.txt Normal file
View File

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

69
bot.cpp Normal file
View File

@@ -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<std::mutex> 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<std::mutex> 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);
}
}

25
bot.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <TgBot/TgBot.h>
#include <filesystem>
#include <vector>
#include <mutex>
#include <future>
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<std::string> fileCache;
std::mutex cacheMutex;
};

8
main.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include "bot.h"
int main() {
const std::string token = "YOUR_BOT_TOKEN";
Bot bot(token);
bot.start();
return 0;
}