Last active
February 12, 2025 18:08
-
-
Save CsgoBotTG/e62e3b03d875d9731c5cfeab12a5f490 to your computer and use it in GitHub Desktop.
telegram bot send messages and files via WinHTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef _WIN32 | |
#include "tgsend.h" | |
// Ôóíêöèÿ äëÿ êîäèðîâàíèÿ ñòðîêè â URL-ôîðìàò | |
int chr_isvalid(uint32_t c) | |
{ | |
if (c <= 0x7F) return 1; | |
if (0xC080 == c) return 1; // Accept 0xC080 as representation for '\0' | |
if (0xC280 <= c && c <= 0xDFBF) return ((c & 0xE0C0) == 0xC080); | |
if (0xEDA080 <= c && c <= 0xEDBFBF) return 0; // Reject UTF-16 surrogates | |
if (0xE0A080 <= c && c <= 0xEFBFBF) return ((c & 0xF0C0C0) == 0xE08080); | |
if (0xF0908080 <= c && c <= 0xF48FBFBF) return ((c & 0xF8C0C0C0) == 0xF0808080); | |
return 0; | |
} | |
std::string UrlEncode(const std::string& str, const bool use_utf8) | |
{ | |
// use_utf8 if you sending utf8 string | |
std::stringstream encoded; | |
for (char c : str) { | |
if (use_utf8 && !chr_isvalid(c)) | |
continue; | |
if (isalnum(c) || c == '-' || c == '_' || c == '.') { | |
encoded << c; | |
} | |
else if (c == ' ') { | |
encoded << "+"; | |
} | |
else { | |
encoded << '%' << std::hex << std::uppercase << (int)(unsigned char)c; | |
} | |
} | |
return encoded.str(); | |
} | |
bool SendFile(HINTERNET& hInternet, HINTERNET& hConnect, const std::vector<char>& fileBuffer, const std::string& filename) | |
{ | |
/* | |
for (const auto& i : fileBuffer) | |
printf("%c", i); | |
printf("\n%d\n", fileBuffer.size()); | |
*/ | |
// Ôîðìèðóåì ìíîãî÷àñòíûé/ôîðìû äàííûå (multipart/form-data) | |
std::string boundary = "---------------------------" + std::to_string(GetTickCount()); // Óíèêàëüíàÿ ãðàíèöà | |
std::string postData; | |
// Äîáàâëÿåì chat_id | |
//postData += "--" + boundary + "\r\n"; | |
//postData += "Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n"; | |
//postData += chatId + "\r\n"; | |
// Äîáàâëÿåì document (ôàéë) | |
postData += "--" + boundary + "\r\n"; | |
postData += "Content-Disposition: form-data; name=\"document\"; filename=\"" + UrlEncode(filename) + "\"\r\n"; | |
postData += "Content-Type: application/octet-stream\r\n\r\n"; | |
// Äîáàâëÿåì äàííûå ôàéëà | |
std::string endBoundary = "\r\n--" + boundary + "--\r\n"; | |
// Ôîðìèðóåì ïîëíûé áóôåð äàííûõ äëÿ îòïðàâêè | |
std::vector<char> fullPostData; | |
fullPostData.insert(fullPostData.end(), postData.begin(), postData.end()); | |
fullPostData.insert(fullPostData.end(), fileBuffer.begin(), fileBuffer.end()); | |
fullPostData.insert(fullPostData.end(), endBoundary.begin(), endBoundary.end()); | |
// Ôîðìèðóåì HTTP-çàïðîñ | |
std::string requestUrl = "/bot" + botToken + "/sendDocument?chat_id=" + chatId; | |
HINTERNET hRequest = HttpOpenRequestA(hConnect, "POST", requestUrl.c_str(), NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD, 0); | |
if (hRequest == NULL) { | |
printf("HttpOpenRequest failed: %d\n", GetLastError()); | |
return false; | |
} | |
// Óñòàíàâëèâàåì çàãîëîâêè | |
std::string contentTypeHeader = "Content-Type: multipart/form-data; boundary=" + boundary; | |
if (!HttpAddRequestHeadersA(hRequest, contentTypeHeader.c_str(), contentTypeHeader.length(), HTTP_ADDREQ_FLAG_ADD)) { | |
printf("HttpSendRequestHeaders failed: %d\n", GetLastError()); | |
InternetCloseHandle(hRequest); | |
return false; | |
} | |
// Îòïðàâëÿåì çàïðîñ | |
if (!HttpSendRequestA(hRequest, NULL, 0, fullPostData.data(), fullPostData.size())) { | |
printf("HttpSendRequest failed: %d\n", GetLastError()); | |
InternetCloseHandle(hRequest); | |
return false; | |
} | |
// ×èòàåì îòâåò | |
std::string response; | |
char buffer[1024]; | |
DWORD bytesRead; | |
while (InternetReadFile(hRequest, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead > 0) { | |
buffer[bytesRead] = 0; | |
response += buffer; | |
} | |
// Âûâîäèì îòâåò (äëÿ îòëàäêè) | |
//printf("Response from Telegram: %s\n", response.c_str()); | |
// Çàêðûâàåì ñîåäèíåíèÿ | |
InternetCloseHandle(hRequest); | |
//InternetCloseHandle(hConnect); | |
//InternetCloseHandle(hInternet); | |
return response.find("ok") != std::string::npos; | |
} | |
bool sendtext(HINTERNET& hInternet, HINTERNET& hConnect, std::string msg) | |
{ | |
//WARNING!!! std::string msg SHOULD BE IN UTF8!!! USE URL_ENCODE(msg, true) | |
msg = "/bot" + botToken + "/sendMessage?chat_id=" + chatId + "text=" + msg; | |
//cout << msg << endl; | |
std::wstring msgwstr(msg.begin(), msg.end()); | |
LPCWSTR msglpcwstr = msgwstr.c_str(); | |
HINTERNET hRequest = | |
::HttpOpenRequestW( | |
hConnect, | |
L"GET", | |
msglpcwstr, | |
NULL, | |
NULL, | |
0, | |
INTERNET_FLAG_KEEP_CONNECTION, | |
1); | |
if (hRequest == NULL) | |
{ | |
printf("HttpOpenRequest failed: %d\n", GetLastError()); | |
return false; | |
} | |
if (!HttpSendRequestA(hRequest, NULL, 0, NULL, 0)) | |
{ | |
printf("HttpSendRequest failed: %d\n", GetLastError()); | |
InternetCloseHandle(hRequest); | |
return false; | |
} | |
std::string response; | |
char buffer[1024]; | |
DWORD bytesRead; | |
while (InternetReadFile(hRequest, buffer, sizeof(buffer) - 1, &bytesRead) && bytesRead > 0) { | |
buffer[bytesRead] = 0; | |
response += buffer; | |
} | |
::InternetCloseHandle(hRequest); | |
return response.find("ok") != std::string::npos; | |
} | |
#endif // _WIN32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifdef _WIN32 | |
#include <strsafe.h> | |
#include <cctype> | |
#include <iomanip> | |
#include <sstream> | |
#include <vector> | |
#include <string> | |
#include <Windows.h> | |
#include <wininet.h> | |
#pragma comment(lib, "wininet") | |
const std::string botToken = "YOURTOKEN"; | |
const std::string chatId = "YOURCHATID"; | |
bool SendFile(HINTERNET& hInternet, HINTERNET& hConnect, const std::vector<char>& fileBuffer, const std::string& filename); | |
bool sendtext(HINTERNET& hInternet, HINTERNET& hConnect, std::string msg); | |
int chr_isvalid(uint32_t c); | |
std::string UrlEncode(const std::string& str, const bool use_utf8); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment