1

I am using TgBot for my bot : https://github.com/egorpugin/tgbot

I am aware of the Telegram Bot API guidelines specifying that the file needs to be posted via a POST request using multipart/form-data in the usual way that files are uploaded via the browser.

My question is, how do I do that in C++? Of course I can use CURL, but TgBot seems to have a class readily available to do that : CurlHttpClient.

Based on other answers for other languages, I am working with this code :

TgBot::CurlHttpClient curlClient;
std::stringstream sstream;
sstream << "https://api.telegram.org/bot" << botToken;
sstream << "sendDocument";

TgBot::Url url(sstream.str());

std::vector<TgBot::HttpReqArg> args;
args.emplace_back("chat_id", chatId);
std::string docString = "@";
docString += filename;
args.emplace_back("document", docString, true);
fileId = curlClient.makeRequest(url, args);

But this fails with

"{"ok":false,"error_code":404,"description":"Not Found"}"

I based by request on this answer. It is based on command line Curl and works fine.

Mickaël C. Guimarães
  • 1,020
  • 2
  • 14
  • 32

1 Answers1

1

I ended up finding another example, and modified my solution based on it. Now it works as expected. Here is the working solution :

TgBot::CurlHttpClient curlClient;
std::stringstream sstream;
sstream << "https://api.telegram.org/bot" << botToken;
sstream << "/sendDocument?";
TgBot::Url url(sstream.str());

std::vector<TgBot::HttpReqArg> args;
args.emplace_back("chat_id", chatId);
args.emplace_back("caption", "MyCaption");
args.emplace_back("document", "attach://file");
args.emplace_back("file", FileTools::read(filename), true);

fileId = curlClient.makeRequest(url, args);
Mickaël C. Guimarães
  • 1,020
  • 2
  • 14
  • 32