1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
| #include <windows.h>
#include <winhttp.h>
#include <iostream>
#include <string>
#pragma comment(lib, "winhttp.lib")
struct InternetHandle {
HINTERNET value{};
explicit InternetHandle(HINTERNET h) : value(h) {}
~InternetHandle() { if (value) WinHttpCloseHandle(value); }
InternetHandle(const InternetHandle&) = delete;
InternetHandle& operator=(const InternetHandle&) = delete;
};
bool PostToSlack(const std::wstring& webhookUrl, const std::string& bodyUtf8) {
URL_COMPONENTS parts{};
parts.dwStructSize = sizeof(parts);
parts.dwSchemeLength = parts.dwHostNameLength = DWORD(-1);
parts.dwUrlPathLength = parts.dwExtraInfoLength = DWORD(-1);
if (!WinHttpCrackUrl(webhookUrl.c_str(), 0, 0, &parts) ||
parts.nScheme != INTERNET_SCHEME_HTTPS) return false;
const std::wstring host(parts.lpszHostName, parts.dwHostNameLength);
if (host != L"hooks.slack.com") return false;
std::wstring path(parts.lpszUrlPath, parts.dwUrlPathLength);
if (parts.dwExtraInfoLength)
path.append(parts.lpszExtraInfo, parts.dwExtraInfoLength);
if (bodyUtf8.size() > MAXDWORD) return false;
InternetHandle session(WinHttpOpen(L"SlackPoster/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0));
if (!session.value) return false;
InternetHandle connection(WinHttpConnect(session.value, host.c_str(), parts.nPort, 0));
if (!connection.value) return false;
InternetHandle request(WinHttpOpenRequest(connection.value, L"POST", path.c_str(),
nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE));
if (!request.value) return false;
const wchar_t* headers = L"Content-Type: application/json; charset=utf-8\r\n";
const DWORD bodySize = static_cast<DWORD>(bodyUtf8.size());
if (!WinHttpSendRequest(request.value, headers, DWORD(-1),
bodyUtf8.empty() ? WINHTTP_NO_REQUEST_DATA : const_cast<char*>(bodyUtf8.data()),
bodySize, bodySize, 0) || !WinHttpReceiveResponse(request.value, nullptr)) return false;
DWORD status = 0, statusSize = sizeof(status);
if (!WinHttpQueryHeaders(request.value,
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX, &status, &statusSize, WINHTTP_NO_HEADER_INDEX) ||
status != 200) return false;
std::string response;
for (;;) {
DWORD available = 0;
if (!WinHttpQueryDataAvailable(request.value, &available)) return false;
if (!available) break;
const size_t oldSize = response.size();
response.resize(oldSize + available);
DWORD bytesRead = 0;
if (!WinHttpReadData(request.value, response.data() + oldSize, available, &bytesRead)) return false;
response.resize(oldSize + bytesRead);
}
return response == "ok";
}
int main() {
wchar_t webhookBuffer[512]{};
const DWORD bufferCount = static_cast<DWORD>(
sizeof(webhookBuffer) / sizeof(webhookBuffer[0]));
const DWORD chars = GetEnvironmentVariableW(
L"SLACK_WEBHOOK_URL", webhookBuffer, bufferCount);
if (chars == 0 || chars >= bufferCount) {
std::cerr << "Set SLACK_WEBHOOK_URL to the webhook URL.\n";
return 1;
}
const std::wstring webhookUrl(webhookBuffer, chars);
const std::string bodyUtf8 = R"({"text":"Hello from C++ :rocket:"})";
if (!PostToSlack(webhookUrl, bodyUtf8)) {
std::cerr << "Slack webhook request failed.\n";
return 1;
}
std::cout << "Slack accepted the message.\n";
return 0;
}
|