-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdxdiag.js
More file actions
298 lines (251 loc) · 9.28 KB
/
dxdiag.js
File metadata and controls
298 lines (251 loc) · 9.28 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
process.env["NTBA_FIX_319"] = 1;
process.env["NTBA_FIX_350"] = 1;
process.on('uncaughtException', function (error) {
console.log("\x1b[31m", "Exception: ", error, "\x1b[0m");
});
process.on('unhandledRejection', function (error, p) {
console.log("\x1b[31m","Error: ", error.message, "\x1b[0m");
});
var config = require('./config.js');
var TelegramBot = require('node-telegram-bot-api');
var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var mysql_sync = require('sync-mysql');
console.log('Connecting bot...');
var token = config.dxdiagtoken;
var bot = new TelegramBot(token);
var app = express();
var path = "/dxdiag/bot" + token;
var port = 25012;
bot.setWebHook('https://fenixweb.net:8443' + path);
app.listen(port);
app.use(bodyParser.json());
app.post(path, function(req, res) {
bot.processUpdate(req.body);
res.sendStatus(200);
});
console.log('Starting bot...');
var connection = mysql.createConnection({
host: config.dbhost,
user: config.dbuser_dxdiag,
password: config.dbpassword_dxdiag,
database: config.dbdatabase_dxdiag
});
connection.connect();
var connection_sync = new mysql_sync({
host: config.dbhost,
user: config.dbuser_dxdiag,
password: config.dbpassword_dxdiag,
database: config.dbdatabase_dxdiag
});
setInterval(function () {
connection.query('SELECT 1');
connection_sync.query('SELECT 1');
}, 60000);
process.on('SIGINT', function() {
console.log("Shutting down bot...");
process.exit();
});
process.on('SIGTERM', function() {
console.log("Shutting down bot...");
process.exit();
});
var validLang = ["en", "it"];
var lang_main = [];
var lang_admin = [];
var lang_too_large = [];
var lang_not_txt = [];
var lang_uploaded = [];
var lang_share = [];
var lang_publish = [];
var lang_uploaded_at = [];
var lang_by = [];
var lang_show_count = [];
var lang_show_count_2 = [];
var lang_resolution = [];
lang_main["en"] = "<b>Welcome to Dxdiag Reader Bot!</b>\n\nSend me the dxdiag.txt file and share with others with inline functions!";
lang_main["it"] = "<b>Benvenuto nel Dxdiag Reader Bot!</b>\n\nInviami il file dxdiag.txt e condividilo con altri con le funzioni inline!";
lang_admin["en"] = "You must be administrator to use this command";
lang_admin["it"] = "Devi essere amministratore per utilizzare questo comando";
lang_too_large["en"] = "Your file is too big, max size: 200kb";
lang_too_large["it"] = "Il file che stai caricando supera la dimensione massima pari a: 200kb";
lang_not_txt["en"] = "Your file extension is not valid, should be .txt";
lang_not_txt["it"] = "L'estensione del file non è valida, dovrebbe essere .txt";
lang_uploaded["en"] = "File successfully uploaded! You can see it by using this code: ";
lang_uploaded["it"] = "File caricato con successo! Puoi visualizzarlo utilizzando questo codice: ";
lang_share["en"] = "Show & Share";
lang_share["it"] = "Visualizza & Condividi";
lang_publish["en"] = "Publish configuration";
lang_publish["it"] = "Pubblica configurazione";
lang_uploaded_at["en"] = "Uploaded at ";
lang_uploaded_at["it"] = "Caricato il ";
lang_by["en"] = " by ";
lang_by["it"] = " da ";
lang_show_count["en"] = "showed ";
lang_show_count["it"] = "visualizzato ";
lang_show_count_2["en"] = " times";
lang_show_count_2["it"] = " volte";
lang_resolution["en"] = "Resolution"
lang_resolution["it"] = "Risoluzione";
bot.onText(/^\/start/i, function (message) {
if (message.chat.id < 0)
return;
var no_preview = {
parse_mode: "HTML",
disable_web_page_preview: true
};
var lang = "en";
if (message.from.language_code != undefined){
if (validLang.indexOf(message.from.language_code) != -1)
lang = message.from.language_code;
}
bot.sendMessage(message.chat.id, lang_main[lang], no_preview);
});
bot.on('message', function (message, match) {
if (message.document == undefined)
return;
var lang = "en";
if (message.from.language_code != undefined){
if (validLang.indexOf(message.from.language_code) != -1)
lang = message.from.language_code;
}
const document = message.document;
const fname = "tmp.txt";
if (document.file_size > 200000) { // 200 kb
bot.sendMessage(message.chat.id, lang_too_large[lang]);
return;
}
if (document.file_name.substr(document.file_name.length-4, document.file_name.length) != ".txt") {
bot.sendMessage(message.chat.id, lang_not_txt[lang]);
return;
}
if (document.mime_type != "text/plain") {
bot.sendMessage(message.chat.id, lang_not_txt[lang]);
return;
}
bot.getFileLink(document.file_id).then(function (tgurl) {
const file = fs.createWriteStream(fname);
const request = https.get(tgurl, function(response) {
response.pipe(file);
fs.readFile(fname, "utf8", function (err, data) {
if (err) return console.log(err);
const file_data = Buffer.from(data).toString('base64');
const query_id = generateQueryId();
connection.query("INSERT INTO files (account_id, file_content, upload_time, query_id) VALUES (" + message.from.id + ", '" + file_data + "', NOW(), '" + query_id + "')", function (err, rows) {
if (err) throw err;
var iKeys = [];
iKeys.push([{
text: lang_share[lang],
switch_inline_query: query_id
}]);
bot.sendMessage(message.chat.id, lang_uploaded[lang] + query_id, {
parse_mode: 'Markdown',
reply_markup: {
inline_keyboard: iKeys
}
});
console.log("Added new from " + message.from.id);
fs.unlink(fname,function(err) {
if (err) return console.log(err);
});
});
});
});
});
});
bot.on("inline_query", function (message) {
const query = message.query;
if (query.length == 8) {
var lang = "en";
if (message.from.language_code != undefined){
if (validLang.indexOf(message.from.language_code) != -1)
lang = message.from.language_code;
}
connection.query("SELECT * FROM files WHERE query_id = " + connection.escape(query), function (err, rows) {
if (err) throw err;
const file_id = rows[0].id;
const account_id = rows[0].account_id;
const file_content = Buffer.from(rows[0].file_content, 'base64').toString('utf8');
const upload_time = rows[0].upload_time;
const show_count = rows[0].show_count;
const message_text = extractInfo(lang, file_content);
console.log("Requested " + query + " from " + message.from.id);
bot.answerInlineQuery(message.id, [{
id: '0',
type: 'article',
title: lang_publish[lang],
description: lang_uploaded_at[lang] + toDate(lang, upload_time) + lang_by[lang] + account_id + ", " + lang_show_count[lang] + formatNumber(show_count) + lang_show_count_2[lang],
message_text: message_text,
parse_mode: "HTML"
}], {cache_time: 0}).then(function (tgurl) {
connection.query("UPDATE files SET show_count = show_count+1 WHERE id = " + file_id, function (err, rows) {
if (err) throw err;
});
});
});
}
});
// Functions
function extractInfo(lang, content) {
const lines = content.split("\r\n");
const system_line = lines.indexOf("System Information");
const operating_system = lines[system_line+5].match(/: (.+)\(/)[1];
const cpu = lines[system_line+10].match(/: (.+)/)[1].replace(/ +/g, " ");
const ram = Math.round(lines[system_line+11].match(/: (\d+)/)[1]/1024) + " GB";
const display_line = lines.indexOf("Display Devices");
const gpu = lines[display_line+2].match(/: (.+)/)[1] + " (" + Math.round(lines[display_line+12].match(/: (\d+)/)[1]/1024) + " GB)";
const resolution = lines[display_line+14].match(/: (.+)/)[1];
const result = "<b>OS</b>: " + operating_system + "\n<b>CPU</b>: " + cpu + "\n<b>RAM</b>: " + ram + "\n<b>GPU</b>: " + gpu + "\n<b>" + lang_resolution[lang] + "</b>: " + resolution;
return result;
}
function generateQueryId() {
var str;
while (1) {
str = Math.random().toString(36).slice(-8);
var rows = connection_sync.query("SELECT query_id FROM files WHERE query_id = '" + str + "'");
if (Object.keys(rows).length == 0)
break;
}
return str;
}
function getNow(lang, obj) {
var d = new Date();
obj = typeof obj !== 'undefined' ? obj : false;
var datetime;
if (lang == "it")
datetime = addZero(d.getDate()) + "/" + addZero(d.getMonth() + 1) + "/" + d.getFullYear() + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
else if (lang == "en")
datetime = d.getFullYear() + "-" + addZero(d.getMonth() + 1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
else
datetime = "Error";
if (obj == true)
datetime = new Date(datetime);
return datetime;
}
function toDate(lang, date) {
var d = new Date(date);
if (typeof date == "object")
d = date;
var datetime = "";
if (lang == "it")
datetime = addZero(d.getMonth() + 1) + "/" + addZero(d.getDate()) + "/" + d.getFullYear() + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
else if (lang == "en")
datetime = d.getFullYear() + "-" + addZero(d.getMonth() + 1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
else
datetime = "Error";
return datetime;
}
function formatNumber(num) {
return ("" + num).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, function ($1) {
return $1 + "."
});
}
function addZero(i) {
if (i < 10)
i = "0" + i;
return i;
}