T
T
Ternick2020-03-01 14:00:00
C++ / C#
Ternick, 2020-03-01 14:00:00

How to send a photo to telegram?

I always get this as a response:

{"ok":false,"error_code":400,"description":"Bad Request: there is no photo in the request"}

THE CODE:
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

#pragma comment(lib, "wininet")

string postRequest(string url, string path, string postData) {
  HINTERNET hInternet, hConnect, hRequest;
  hInternet = InternetOpen(TEXT("TERNICK OWL"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  if (hInternet != NULL) {
    hConnect = InternetConnect(hInternet, TEXT(url.c_str()), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1u);
    if (hConnect != NULL) {
      hRequest = HttpOpenRequest(hConnect, TEXT("POST"), path.c_str(), NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD, 1);
      if (hRequest != NULL) {
        string headers = "Content-Type: multipart/form-data; boundary=bf113e6d618f47f7b56337cafdf3c036";
        BOOL bSend = HttpSendRequest(hRequest, headers.c_str(), headers.size(), (LPVOID)postData.data(), (DWORD)postData.size());
        if (bSend) {
          DWORD dwSize = 0;
          string result;
          char *buff = new char[256];
          do
          {
            memset(buff, 0, 256);
            InternetReadFile(hRequest, buff, sizeof(buff) - 1, &dwSize);
            result.append(buff, dwSize);
          } while (dwSize != 0);
          delete[] buff;
          InternetCloseHandle(hRequest);
          InternetCloseHandle(hConnect);
          InternetCloseHandle(hInternet);
          return result;
        }
        else {
          InternetCloseHandle(hRequest);
          InternetCloseHandle(hConnect);
          InternetCloseHandle(hInternet);
        }
      }
      else {
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hInternet);
      }
    }
    else {
      InternetCloseHandle(hConnect);
      InternetCloseHandle(hInternet);
    }
  }
  else {
    InternetCloseHandle(hInternet);
  }
  return "";
}

string slurp(string path) {
  stringstream sstr;
  ifstream in(path, ios::out | ios::binary);
  sstr << in.rdbuf();
  in.close();
  return sstr.str();
}
int main() {
  string response = postRequest("api.telegram.org", "bot<TUTTOKEN>/sendPhoto?chat_id=429037951&caption=CAP", "--bf113e6d618f47f7b56337cafdf3c036\nContent-Disposition: form-data; name=\"photo\"; filename=\"photo\"\n\n" + slurp("screen.bmp")) + "--bf113e6d618f47f7b56337cafdf3c036--";
  cout << response << endl;
  system("pause");
  return 0;
}

AAA HELP :)
Tried to imitate python requests but didn't work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question