Ternick
@Ternick

Как отправить фото в телеграм?

Всегда в виде ответа получаю это:
{"ok":false,"error_code":400,"description":"Bad Request: there is no photo in the request"}

КОД:
#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;
}

ААА, ПОМОГИТЕ :)
Пытался подражать запросам python, но не вышло.
  • Вопрос задан
  • 237 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы