slowriot / libtelegram

Fast, efficient, header-only C++ Telegram bot API library using polling or FastCGI.
MIT License
129 stars 24 forks source link

File uploading addition #7

Open vlabella opened 5 years ago

vlabella commented 5 years ago

libTelegram is working fine for me on windows. Sends messages ok. I was uploading files as well by sending the URL and letting the telegram server pull the file from my server. However now I want to directly upload the image file. It seems the library does not support this yet. Can you give me some pointers on how to implement. I am experienced C++ programmer. Thanks

for example I have tried with no luck

telegram::sender sender(MyToken);
telegram::types::file f; // slurp the file into file_id?? std::ifstream t("image.png"); f.file_id = ""; t.seekg(0, std::ios::end);
f.file_id.reserve(t.tellg()); t.seekg(0, std::ios::beg); f.file_id.assign((std::istreambuf_iterator(t)),std::istreambuf_iterator()); sender.send_photo(123456789,f,"this is your image");

slowriot commented 5 years ago

You would need to upload the file manually, using multipart form data, as per https://core.telegram.org/bots/api#inputfile, and then reference that in your send_photo call. I haven't yet built this functionality into the library, so you'd need to make an API call to telegram yourself outside of libtelegram, using something like https://github.com/yhirose/cpp-httplib#multipartform-data-post-data. The httplib library is included at present as the default way of making poll requests, so you don't need to include anything else to implement this.

I'll update this issue when I get round to adding the upload logic.