-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
247 lines (208 loc) · 7.68 KB
/
main.cpp
File metadata and controls
247 lines (208 loc) · 7.68 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <cstdio>
#include <ctime>
#include <sstream>
#include <string>
#include <tchar.h>
#include <windows.h>
#include <winhttp.h>
#include <SDKDDKVer.h>
#include <json/json.h>
std::string getUpdates(const std::string& Token) {
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
std::string fullAddress = "/bot";
fullAddress.append(Token);
fullAddress.append("/getUpdates");
std::wstring stemp = std::wstring(fullAddress.begin(), fullAddress.end());
LPCWSTR sw = stemp.c_str();
//Request body
LPSTR postdata = const_cast<char *>("{'offset': offset, 'timeout': 60}");
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, L"api.telegram.org",
INTERNET_DEFAULT_HTTPS_PORT, 0);
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", sw,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
// Add a request header.
if (hRequest)
bResults = WinHttpAddRequestHeaders(hRequest,
L"content-type:application/json",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
-1L,
postdata, strlen(postdata),
strlen(postdata), 0);
// End the request.
if (bResults) bResults = WinHttpReceiveResponse(hRequest, NULL);
std::string output = "";
// Keep checking for data until there is nothing left.
if (bResults)
{
do
{
// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
printf("Error %lu in WinHttpQueryDataAvailable.\n", GetLastError());
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize = 0;
}
else
{
// Read the data.
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
printf("Error %lu in WinHttpReadData.\n", GetLastError());
else {
std::string str_temp{pszOutBuffer};
if (!str_temp.empty()) output += str_temp;
}
// Free the memory allocated to the buffer.
delete[] pszOutBuffer;
}
} while (dwSize > 0);
}
// Report any errors.
if (!bResults) printf("Error %ld has occurred.\n", GetLastError());
// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return output;
}
void sendMessage(const std::string& mes, const std::string& Token, const std::string& ChatId) {
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
std::string fullAddress = "/bot";
fullAddress.append(Token);
fullAddress.append("/sendMessage");
std::wstring stemp = std::wstring(fullAddress.begin(), fullAddress.end());
LPCWSTR sw = stemp.c_str();
//Request body
std::string message = "{\"text\":\"" + mes +"\", \"chat_id\":\"" + ChatId + "\"}";
LPSTR postdata = const_cast<char *>(message.c_str());
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen(L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
// Specify an HTTP server.
if (hSession)
hConnect = WinHttpConnect(hSession, L"api.telegram.org",
INTERNET_DEFAULT_HTTPS_PORT, 0);
// Create an HTTP request handle.
if (hConnect)
hRequest = WinHttpOpenRequest(hConnect, L"GET", sw,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_SECURE);
// Add a request header.
if (hRequest)
bResults = WinHttpAddRequestHeaders(hRequest,
L"content-type:application/json",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD);
// Send a request.
if (hRequest)
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
-1L,
postdata, strlen(postdata),
strlen(postdata), 0);
// End the request.
if (bResults) bResults = WinHttpReceiveResponse(hRequest, NULL);
// Keep checking for data until there is nothing left.
if (bResults)
{
do
{
// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
printf("Error %lu in WinHttpQueryDataAvailable.\n",
GetLastError());
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize + 1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize = 0;
}
else
{
// Read the data.
ZeroMemory(pszOutBuffer, dwSize + 1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %lu in WinHttpReadData.\n", GetLastError());
// Free the memory allocated to the buffer.
delete[] pszOutBuffer;
}
} while (dwSize > 0);
}
// Report any errors.
if (!bResults) printf("Error %ld has occurred.\n", GetLastError());
// Close any open handles.
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
}
int main() {
std::string json_data = "", UpdataId = "0", TmpId = "", command;
Json::Reader reader;
Json::Value j;
int flag = 0, io = 0;
/* BotのトークンとチャットIDを書き換えましょう */
std::string TOKEN = "0000000000:AAA_AAAAAAAAAAAAAAAAAAAAAAA_AAAAAAA";
std::string ChatId = "0000000000";
while (1) {
try {
json_data = getUpdates(TOKEN);
reader.parse(json_data, j);
const Json::Value& k = j["result"];
TmpId = k[k.size() - 1]["message"]["message_id"].asString();
if (atoi(UpdataId.c_str()) < atoi(TmpId.c_str())) {
UpdataId = TmpId;
flag = 1;
}
if (io == 0) {
flag = 0;
io = 1;
//sendMessage("Bot Started!", TOKEN, ChatId);
printf("Bot Started!");
continue;
}
if (flag) {
flag = 0;
command = k[k.size()-1]["message"]["text"].asString();
sendMessage(command, TOKEN, ChatId);
}
}
catch (...) {}
Sleep(500);
}
return 0;
}