-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
695 lines (614 loc) · 27.3 KB
/
index.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import TelegramBot from "node-telegram-bot-api"; // Telegram, Time, HuggingFace API, File Managing
import cron from "node-cron";
import { Client } from "@gradio/client";
import fs from "fs";
import { config } from "./config.js"; // Digneurobot Token
import { textData, buttonData, errorData, databaseBackup } from "./watcher.js"; // Surround Watcher (debugging)
const bot = new TelegramBot(config.Tokens[0], { polling: true }); // bot setup
const FedotID = 870204479; // developer ID
let usersData = [];
// bot menu commands
bot.setMyCommands([
{ command: "/start", description: "Перезапуск" },
{ command: "/reset", description: "Сброс контекста" },
{ command: "/profile", description: "Профиль" },
]);
// start message
async function intro(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
await bot.sendMessage(chatId, `Добрo пожаловать в <b>Нейросетивичок</b>. Чтобы задать вопрос, напишите его в чате.\n\n<b>Команды:</b>\n<blockquote>/start - Перезапуск\n/reset - Сброс контекста\n/profile - Профиль</blockquote>`, {
parse_mode: `HTML`,
disable_web_page_preview: true,
});
dataAboutUser.textContext = [];
dataAboutUser.userAction = "regular";
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// profile message
async function profile(chatId, sectionType = `profile`) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
switch (sectionType) {
case `send`:
await bot.sendMessage(chatId, "ㅤ").then((message) => {
dataAboutUser.profileMessageId = message.message_id;
profile(chatId);
});
break;
case `profile`:
await bot.editMessageText(`👤 <b><i>Профиль</i> • </b><code>${dataAboutUser.chatId}</code> 🔍\n\n<b>Информация о себе для Нейросети:</b><blockquote>${dataAboutUser.userInfoText ? `${dataAboutUser.userInfoText.slice(0, 200)}${dataAboutUser.userInfoText.length > 200 ? `..` : ``}\n\n<a href="https://t.me/digneurobot/?start=userInfo"><b>Изменить..</b></a>` : `<a href="https://t.me/digneurobot/?start=userInfo"><b>Добавить..</b></a>`}</blockquote>\n\n<b>Какой ответ вы хотели бы получить:</b><blockquote>${dataAboutUser.answerTypeText ? `${dataAboutUser.answerTypeText.slice(0, 200)}${dataAboutUser.answerTypeText.length > 200 ? `..` : ``}\n\n<a href="https://t.me/digneurobot/?start=answerType"><b>Изменить..</b></a>` : `<a href="https://t.me/digneurobot/?start=answerType"><b>Добавить..</b></a>`}</blockquote>`, {
parse_mode: `HTML`,
chat_id: chatId,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[{ text: `${chatId == FedotID ? `Управление 🔥` : ``}`, callback_data: `adminStart` }],
[
{ text: `❕О боте`, callback_data: `about` },
{ text: `digfusion❔`, callback_data: `digfusion` },
],
[
{
text: `Поддержка 💭`,
url: `https://t.me/digsupport`,
},
],
],
},
});
break;
case `userInfo`:
await bot.editMessageText(`👤 <b><i>Профиль</i> • О себе 🔍</b>\n\n<b>Информация для Нейросети:</b>${dataAboutUser.userInfoText ? `<blockquote><code>${dataAboutUser.userInfoText}</code></blockquote>\n\n<i>Напишите текст в чате, чтобы изменить..</i>` : `<blockquote><i>Напишите текст в чате, чтобы добавить..</i></blockquote>`}`, {
parse_mode: `HTML`,
chat_id: chatId,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{ text: `⬅️ Назад`, callback_data: `profile` },
{ text: `${dataAboutUser.userInfoText ? `Сбросить ♻️` : ``}`, callback_data: `userInfoDelete` },
],
],
},
});
break;
case `answerType`:
await bot.editMessageText(`👤 <b><i>Профиль</i> • Тип ответа 🔍</b>\n\n<b>Какой ответ вы хотели бы получить:</b>${dataAboutUser.answerTypeText ? `<blockquote><code>${dataAboutUser.answerTypeText}</code></blockquote>\n\n<i>Напишите текст в чате, чтобы изменить..</i>` : `<blockquote><i>Напишите текст в чате, чтобы добавить..</i></blockquote>`}`, {
parse_mode: `HTML`,
chat_id: chatId,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{ text: `⬅️ Назад`, callback_data: `profile` },
{ text: `${dataAboutUser.answerTypeText ? `Сбросить ♻️` : ``}`, callback_data: `answerTypeDelete` },
],
],
},
});
break;
}
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// about bot message
async function about(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
await bot.editMessageText(`<b>Что такое Нейросетивичок?</b><blockquote><b>Бот</b>, разработанный компанией <b>digfusion</b> с использованием <b>Hugging Face API.</b></blockquote>\n\n<b>Главные преимущества:</b><blockquote><b>• Быстрые ответы</b>\nМощный искусственный интеллект способен отвечать на вопросы с <b>невероятной скоростью.</b>\n\n<b>• Неограниченные запросы</b>\nОтсутствие лимитов на все функции открывает доступ к <b>безграничному пользованию.</b>\n\n<b>• Абсолютно бесплатно</b>\nВзаимодействие с ботом <b>не требует подписки</b> и других платных услуг.</blockquote>\n\nЧто такое <b>Контекст?</b><blockquote><b>Бот</b> умеет запоминать <b>историю сообщений</b> при <b>текстовых запросах.</b> Это помогает вести и дополнять диалог в рамках <b>одной темы.</b></blockquote>`, {
parse_mode: `HTML`,
chat_id: chatId,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [[{ text: `⬅️Назад`, callback_data: `profile` }]],
},
});
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// digfusion info message
async function digfusion(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
await bot.editMessageText(`<b><i>❔digfusion • О нас</i></b>\n<blockquote>Компания <b><i>digfusion</i></b> - <b>начинающий стартап,</b> разрабатывающий <b>свои приложения</b> и предоставляющий услуги по <b>созданию чат-ботов</b> различных типов!\n\nПросмотреть все <b>наши проекты, реальные отзывы, каталог услуг</b> и <b>прочую информацию о компании</b> можно в нашем <b>Telegram канале</b> и <b>боте-консультанте!</b>\n\n<i>Это приложение разработано <b>digfusion</b> с душой 🤍</i></blockquote>\n\n<b><a href="https://t.me/digfusion">digfusion | инфо</a> • <a href="https://t.me/digfusionbot">digfusion | услуги</a></b>`, {
parse_mode: "HTML",
chat_id: chatId,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [[{ text: `⬅️Назад`, callback_data: `profile` }]],
},
});
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// progressive text message output
async function showResponseText(chatId, progressOutput, userMessage) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
bot.sendChatAction(chatId, "typing");
let outputSpeed = 200;
`${progressOutput.length > 500 ? outputSpeed == 1500 : ``}`;
// showing text by sliced chunks
try {
for (let i = 0; i < progressOutput.length; i += 3800) {
let chunkMessage = progressOutput.slice(i, i + 3800);
let changingText = chunkMessage[0];
await bot
.sendMessage(chatId, `${changingText} ⚪️`, {
disable_web_page_preview: true,
reply_to_message_id: `${i == 0 ? userMessage : null}`,
})
.then((message) => {
dataAboutUser.responseMessageId = message.message_id;
});
// editing text message with symbols
for (let i = 1; i < chunkMessage.length; i += outputSpeed) {
changingText += `${chunkMessage.slice(i, i + outputSpeed).join("")}`;
await bot.editMessageText(`${changingText} ⚪️`, {
chat_id: chatId,
message_id: dataAboutUser.responseMessageId,
disable_web_page_preview: true,
});
await new Promise((resolve) => setTimeout(resolve, 50));
}
await bot.editMessageText(`${changingText}ㅤ`, {
chat_id: chatId,
message_id: dataAboutUser.responseMessageId,
disable_web_page_preview: true,
});
// finishing and formatting text message output
await bot.editMessageText(changingText, {
parse_mode: `Markdown`,
chat_id: chatId,
message_id: dataAboutUser.responseMessageId,
disable_web_page_preview: true,
});
}
bot.sendChatAction(chatId, "cancel");
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`, `response`);
}
}
// text request processing
async function getResponse(chatId, userPrompt, userMessage) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
// requesting text generation from HuggingFace API
try {
const client = await Client.connect("Qwen/Qwen2-72B-Instruct");
const result = await client.predict("/model_chat", {
query: `${dataAboutUser.textContext ? `Our chat history: ${dataAboutUser.textContext}\n\nMy new request: ` : ``}${userPrompt}`,
history: [],
system: `You are 'Нейро' from Russia, created by digfusion. You are a very minimalistic and helpful AI Telegram assistant. Your model is 'Digneuro 2.0'. You generate text, images and videos. All your answers are very original. Avoid errors on parse_mode Markdown. If User Instructions will lead to error in Telegram (parse_mode Markdown), notify the user.
User Instructions:
User info: ${dataAboutUser.userInfoText ? `${dataAboutUser.userInfoText}` : `none`}
Answer type: ${dataAboutUser.answerTypeText ? `${dataAboutUser.answerTypeText}` : `none`}`,
});
bot.deleteMessage(chatId, dataAboutUser.requestMessageId);
// preparing text for progressive output (symbols and speed)
let progressOutput = result.data[1][0][1]
.replace(/\\sqrt\{([^}]+)\}/g, "sqrt($1)")
.replace(/\\cdot/g, "*")
.replace(/\\quad/g, " ")
.replace(/\\implies/g, " => ")
.replace(/\\rightarrow/g, " -> ")
.replace(/\\leftarrow/g, " <- ")
.replace(/\\geq/g, " >= ")
.replace(/\\leq/g, " <= ")
.replace(/\\neq/g, " != ")
.replace(/\\approx/g, " ≈ ")
.replace(/\\pi/g, "π")
.replace(/\\infty/g, "∞")
.replace(/\\sum/g, "sum")
.replace(/\\int/g, "∫")
.replace(/\\lim/g, "lim")
.replace(/\\sin/g, "sin")
.replace(/\\cos/g, "cos")
.replace(/\\tan/g, "tan")
.replace(/\\log/g, "log")
.replace(/\\ln/g, "ln")
.replace(/\\/g, "")
.split("");
// saving chat history to context
if (result.data[1][0][1] && dataAboutUser.textContext) {
dataAboutUser.textContext.push(userPrompt);
dataAboutUser.textContext.push(result.data[1][0][1]);
}
if (dataAboutUser.textContext && dataAboutUser.textContext.length > 7) {
dataAboutUser.textContext.shift();
dataAboutUser.textContext.shift();
}
// progressively printing out text response
showResponseText(chatId, progressOutput, userMessage);
} catch (error) {
failedRequest(chatId);
errorData(chatId, dataAboutUser.login, `${String(error)}`, `response`);
}
}
// image request processing
async function getImage(chatId, userPrompt, userMessage) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
// requesting image generation from HuggingFace API
try {
/*
const client = await Client.connect("Zhofang/FLUX.1-Dev-Serverless-darn");
const result = await client.predict("/query", {
prompt: `${userPrompt ? userPrompt : 'White sand beach with palm trees and hot sun'}`,
is_negative: `(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos, unrealistic, bad looking, low quality`,
steps: 35,
cfg_scale: 0.7,
sampler: "DPM++ 2M Karras",
seed: -1,
strength: 0.7,
huggingface_api_key: "",
use_dev: false,
}); */
const client = await Client.connect("doevent/FLUX.1-merged");
const result = await client.predict("/infer", {
prompt: `${userPrompt ? userPrompt : "White sand beach with palm trees and hot sun"}`,
seed: 0,
randomize_seed: true,
width: 1024,
height: 1024,
guidance_scale: 8,
num_inference_steps: 8,
});
bot.sendChatAction(chatId, "upload_photo");
bot.deleteMessage(chatId, dataAboutUser.requestMessageId);
await bot.sendPhoto(chatId, result.data[0].url, {
reply_to_message_id: userMessage,
});
// saving chat history to context
if (result.data[0] && dataAboutUser.textContext) {
dataAboutUser.textContext.push(userPrompt);
dataAboutUser.textContext.push(`Created image by user prompt: ${userPrompt}`);
}
if (dataAboutUser.textContext && dataAboutUser.textContext.length > 7) {
dataAboutUser.textContext.shift();
dataAboutUser.textContext.shift();
}
} catch (error) {
failedRequest(chatId);
errorData(chatId, dataAboutUser.login, `${String(error)}`, `image`);
}
}
// video request processing
async function getVideo(chatId, userPrompt, userMessage) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
// requesting video generation from HuggingFace API
try {
const client = await Client.connect("TIGER-Lab/T2V-Turbo-V2");
const result = await client.predict("/predict", {
prompt: `${userPrompt ? userPrompt : "White sand beach with palm trees and hot sun"}`,
guidance_scale: 7.5,
percentage: 0.5,
num_inference_steps: 16,
num_frames: 16,
seed: 1968164510,
randomize_seed: true,
param_dtype: "bf16",
});
bot.sendChatAction(chatId, "upload_video");
bot.deleteMessage(chatId, dataAboutUser.requestMessageId);
await bot.sendVideo(chatId, result.data[0].video.url, {
reply_to_message_id: userMessage,
});
// saving chat history to context
if (result.data[0] && dataAboutUser.textContext) {
dataAboutUser.textContext.push(userPrompt);
dataAboutUser.textContext.push(`Created video by user request: ${userPrompt}`);
}
if (dataAboutUser.textContext && dataAboutUser.textContext.length > 7) {
dataAboutUser.textContext.shift();
dataAboutUser.textContext.shift();
}
} catch (error) {
serverOverload(chatId);
errorData(chatId, dataAboutUser.login, `${String(error)}`, `video`);
}
}
// request processing message
async function processingRequest(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
await bot
.sendMessage(chatId, `Ваш запрос обрабатывается...`, {
parse_mode: `HTML`,
disable_web_page_preview: true,
})
.then((message) => {
dataAboutUser.requestMessageId = message.message_id;
});
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// request error message
async function failedRequest(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
bot.deleteMessage(chatId, dataAboutUser.requestMessageId);
try {
await bot.sendMessage(chatId, `Возникла техническая ошибка ❌<blockquote><b>Пожалуйста, попробуйте снова.</b></blockquote>`, {
parse_mode: `HTML`,
disable_web_page_preview: true,
});
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// server overload message (video)
async function serverOverload(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
bot.deleteMessage(chatId, dataAboutUser.requestMessageId);
try {
await bot.sendMessage(chatId, `Сервер перегружен ❌<blockquote><b>Попробуйте позже.</b></blockquote>`, {
parse_mode: `HTML`,
disable_web_page_preview: true,
});
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// text context reset + message
async function resetTextChat(chatId) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
await bot.sendMessage(chatId, `Контекст успешно сброшен ✅<blockquote><b>Напишите свой вопрос в чате.</b></blockquote>`, {
parse_mode: `HTML`,
disable_web_page_preview: true,
});
dataAboutUser.textContext = [];
dataAboutUser.userAction = "regular";
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
// processing and reformatting user request
async function changeMode(chatId, userPrompt, userMessage) {
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
// requesting text generation from HuggingFace API
try {
const client = await Client.connect("Qwen/Qwen2-72B-Instruct");
const result = await client.predict("/model_chat", {
query: `${dataAboutUser.textContext ? `Our chat history: ${dataAboutUser.textContext}\n\nMy new request: ` : ``}${userPrompt}`,
history: [],
system: `You have to respond to user requests based on their type and chat history. Never create explicit content. Follow these rules strictly:
1. For standard information requests or tasks (e.g., 'solve,' 'who is'), respond with only one word and nothing else: 'text'.
2. For image generation requests (e.g., 'draw,' 'create an image of', 'now add'), respond with 'image' and what user wants to get as a result with enhanced prompt (divide with '@').
3. For video generation requests (e.g., 'video with,' 'create a video', 'change'), respond with 'video' and what user wants to get as a result with enhanced prompt in english translated (divide with '@').
4. If the prompt is empty (e.g., 'image' 'video'), respond accordingly with 'image' or 'video' and random cool prompt from you (divide with '@').
5. If the request doesn't fit any of these categories or seems nonsensical, respond with: text.`,
});
let promptDecision = result.data[1][0][1].split("@");
// user request recognition (text, image, video)
if (promptDecision[0] == `text`) {
bot.sendChatAction(chatId, "typing");
getResponse(chatId, userPrompt, userMessage);
} else if (promptDecision[0] == `image`) {
bot.sendChatAction(chatId, "upload_photo");
getImage(chatId, promptDecision[1], userMessage);
} else if (promptDecision[0] == `video`) {
bot.sendChatAction(chatId, "record_video");
getVideo(chatId, promptDecision[1], userMessage);
}
} catch (error) {
failedRequest(chatId);
errorData(chatId, dataAboutUser.login, `${String(error)}`, `response`);
}
}
// admin bot control
async function adminControl(startNextSend = `start`) {
const dataAboutUser = usersData.find((obj) => obj.chatId == FedotID);
switch (startNextSend) {
case `start`:
await bot.editMessageText(`Введите текст общего сообщения..`, {
parse_mode: `HTML`,
chat_id: FedotID,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [[{ text: `⬅️Назад`, callback_data: `profile` }]],
},
});
break;
case `next`:
await bot.editMessageText(dataAboutUser.userAction, {
parse_mode: `HTML`,
chat_id: FedotID,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{ text: `⬅️Назад`, callback_data: `adminStart` },
{ text: `Отправить ✅`, callback_data: `adminSend` },
],
],
},
});
break;
case `send`:
for (let i = 0; i < usersData.length; i++) {
try {
await bot.sendMessage(usersData[i].chatId, dataAboutUser.userAction, {
parse_mode: "HTML",
disable_web_page_preview: true,
});
} catch (error) {
errorData(usersData[i].chatId, usersData[i].login, `${String(error)}`);
continue;
}
}
await bot.editMessageText(`Готово ✅<blockquote><b>Общее сообщение отправлено</b></blockquote>`, {
parse_mode: `HTML`,
chat_id: FedotID,
message_id: dataAboutUser.profileMessageId,
disable_web_page_preview: true,
});
dataAboutUser.userAction = `regular`;
break;
}
}
// master function
async function StartAll() {
// getting data from DB.json
if (fs.readFileSync("DB.json") != "[]" && fs.readFileSync("DB.json") != "") {
let dataFromDB = JSON.parse(fs.readFileSync("DB.json"));
usersData = dataFromDB.usersData || null;
}
// user message recognition
bot.on(`text`, async (message) => {
let chatId = message.chat.id;
let text = message.text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
let userMessage = message.message_id;
// adding variables for new users
try {
if (!usersData?.find((obj) => obj.chatId == chatId)) {
usersData.push({
chatId: chatId,
login: message.from.first_name,
profileMessageId: null,
requestMessageId: null,
responseMessageId: null,
textContext: [],
userAction: `regular`,
userInfoText: ``,
answerTypeText: ``,
});
}
const dataAboutUser = usersData?.find((obj) => obj.chatId == chatId);
if (dataAboutUser) {
switch (text) {
case `/start`:
intro(chatId);
break;
case `/reset`:
resetTextChat(chatId);
break;
case `/profile`:
dataAboutUser.userAction = `regular`;
profile(chatId, `send`);
break;
case `/start userInfo`:
bot.deleteMessage(chatId, userMessage);
dataAboutUser.userAction = `userInfoInput`;
profile(chatId, `userInfo`);
break;
case `/start answerType`:
bot.deleteMessage(chatId, userMessage);
dataAboutUser.userAction = `answerTypeInput`;
profile(chatId, `answerType`);
break;
}
// answering to request and saving user instructions
if (text && Array.from(text)[0] != "/") {
switch (dataAboutUser.userAction) {
case `regular`:
processingRequest(chatId);
changeMode(chatId, text, userMessage);
break;
case `userInfoInput`:
bot.deleteMessage(chatId, userMessage);
dataAboutUser.userInfoText = `${text.length <= 750 ? text : text.slice(0, 750)}`;
profile(chatId, `userInfo`);
break;
case `answerTypeInput`:
bot.deleteMessage(chatId, userMessage);
dataAboutUser.answerTypeText = `${text.length <= 750 ? text : text.slice(0, 750)}`;
profile(chatId, `answerType`);
break;
case `adminInput`:
bot.deleteMessage(chatId, userMessage);
dataAboutUser.userAction = message.text;
adminControl(`next`);
break;
}
}
}
// Surround Watcher (text)
textData(chatId, dataAboutUser.login, text);
} catch (error) {
errorData(chatId, message.from.first_name, `${String(error)}`);
}
});
// pressed button recognition
bot.on(`callback_query`, async (query) => {
let chatId = query.message.chat.id;
let data = query.data;
const dataAboutUser = usersData?.find((obj) => obj.chatId == chatId);
if (dataAboutUser) {
try {
switch (data) {
case `profile`:
dataAboutUser.userAction = `regular`;
profile(chatId);
break;
case `digfusion`:
digfusion(chatId);
break;
case `about`:
about(chatId);
break;
case `adminStart`:
dataAboutUser.userAction = `adminInput`;
adminControl(`start`);
break;
case `adminBack`:
adminControl(`start`);
break;
case `adminSend`:
adminControl(`send`);
break;
case `userInfoDelete`:
dataAboutUser.userInfoText = ``;
profile(chatId, `userInfo`);
break;
case `answerTypeDelete`:
dataAboutUser.answerTypeText = ``;
profile(chatId, `answerType`);
break;
}
// Surround Watcher (button)
buttonData(chatId, dataAboutUser.login, data);
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
}
});
// photo recognition
bot.on(`photo`, async (photo) => {
let chatId = photo.chat.id;
let photoId = photo.photo[2].file_id;
let photoCaption = photo.caption;
const dataAboutUser = usersData.find((obj) => obj.chatId == chatId);
try {
// Surround Watcher (photo)
textData(chatId, dataAboutUser.login, photoCaption);
} catch (error) {
errorData(chatId, dataAboutUser.login, `${String(error)}`);
}
});
// backup DB.json
cron.schedule(`0 */12 * * *`, function () {
try {
// Surround Watcher (backup)
databaseBackup(usersData);
} catch (error) {}
});
// saving DB.json
cron.schedule(`*/30 * * * *`, function () {
try {
fs.writeFileSync("DB.json", JSON.stringify({ usersData }, null, 2));
} catch (error) {}
});
}
StartAll();