Бот получает файл и возвращает его отправителю, tgbot-cpp от reo7sp

Уже на протяжении месяца мучаюсь с этим, можете написать или указать в чем проблема, бот тупо не принимает файл (не качает его)

#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <string>

#include <tgbot/tgbot.h>

using namespace std;
using namespace TgBot;

void handleDocument(TgBot::Bot& bot, TgBot::Message::Ptr message) {
    if (!message->document) {
        bot.getApi().sendMessage(message->chat->id, "Please send a document file.");
        return;
    }

    auto docx = message->document;
    uint64_t fileSize = docx->fileSize;

    // Download the document
    std::string localFilePath = "ЗДЕСЬ СТОЯЛ ПУТЬ НА СОХРАНЕНИЕ";
    TgBot::File::Ptr file;
    try {
        file = bot.getApi().getFile(docx->fileId);
    }
    catch (TgBot::TgException& ex) {
        std::cerr << "Failed to get file: " << ex.what() << std::endl;
        bot.getApi().sendMessage(message->chat->id, "Failed to get file.");
        return;
    }
    const std::string fileId = message->document->fileId;
    const std::string filePath = "downloads/" + message->document->fileName;

    // Send the document back
    try {
        bot.getApi().sendDocument(message->chat->id, TgBot::InputFile::fromFile(localFilePath, "application/octet-stream"), "", "", 0);
    }
    catch (TgBot::TgException& ex) {
        std::cerr << "Failed to send file: " << ex.what() << std::endl;
        bot.getApi().sendMessage(message->chat->id, "Failed to send file.");
        return;
    }

    std::cout << "File sent successfully!" << std::endl;
}

int main() {
    TgBot::Bot bot("Я СПЕЦИАЛЬНО УБРАЛ АПИ КЛЮЧ");
    bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
        bot.getApi().sendMessage(message->chat->id, "Hi!");
        handleDocument(bot, message);
        });
    signal(SIGINT, [](int s) {
        printf("SIGINT got\n");
        exit(0);
        });

    try {
        printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
        bot.getApi().deleteWebhook();

        TgLongPoll longPoll(bot);
        while (true) {
            printf("Long poll started\n");
            longPoll.start();
        }
    }
    catch (exception& e) {
        printf("error: %s\n", e.what());
    }

    return 0;
}


Ответы (1 шт):

Автор решения: pryanik

Автор библиотеки в документации пишет, что сначала тебе нужно получить информацию о файле, который ты хочешь скачать, при помощи метода getFile().

Затем в переменную типа string записывается содержимое файла (при помощи метода downloadFile()).

Эту информацию ты записываешь в файл, который создаешь с помощью ofstream.

TgBot::File::Ptr file = bot.getApi().getFile(message->photo[3]->fileId);
std::string StringPhoto = bot.getApi().downloadFile(file->filePath);

std::ofstream PhotoFile;
PhotoFile.open("photo.jpg");
PhotoFile << StringPhoto;
PhotoFile.close();
→ Ссылка