110 lines
2.8 KiB
JavaScript
110 lines
2.8 KiB
JavaScript
import { TGClient } from './tgclient.js';
|
||
import Utils from '../utils/utils.js';
|
||
|
||
class TGProvider {
|
||
|
||
constructor() {
|
||
this.tgClient = new TGClient(869171, 'c3731f831b212a314f71172daf233f9d');
|
||
this.checksMap = new Map();
|
||
|
||
this.tgClient.onNewMessage(message => {
|
||
if(message.chat_id == this.chat.id) {
|
||
let messageText = message?.content?.text?.text;
|
||
if(messageText && messageText.includes('#️⃣')) {
|
||
let number = this.findFirst(messageText, /Номер: (.*)\n/);
|
||
if(number) {
|
||
this.checksMap.get(number).resolve({
|
||
id: message.id,
|
||
text: messageText
|
||
});
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
//this.init();
|
||
}
|
||
|
||
findFirst(string, regex) {
|
||
let result = string.match(regex);
|
||
if(result && result.length > 1) {
|
||
return result[1];
|
||
} else {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
async init() {
|
||
await this.tgClient.login();
|
||
this.chat = await this.tgClient.findChat('@l0r3m1psum_bot');
|
||
}
|
||
|
||
async close() {
|
||
await this.tgClient.close();
|
||
}
|
||
|
||
async getReport(number) {
|
||
try {
|
||
let waitResponseTask = Utils.promiseWithResolvers();
|
||
this.checksMap.set(number, waitResponseTask);
|
||
await this.tgClient.sendMessage(this.chat, number);
|
||
setTimeout(() => {
|
||
waitResponseTask.reject(Error('TG report timeout'));
|
||
}, 25000);
|
||
let { id, text } = await waitResponseTask.promise;
|
||
await this.tgClient.viewMessage(id, this.chat.id);
|
||
return this.parseReport(text);
|
||
} finally {
|
||
this.checksMap.delete(number);
|
||
}
|
||
}
|
||
|
||
parseBlock(text) {
|
||
return {
|
||
name: this.findFirst(text, /📋 (.*)\n/),
|
||
vin: this.findFirst(text, /VIN: (.*)\n/),
|
||
plateNumber: this.findFirst(text, /Гос номер: (.*)\n/),
|
||
year: this.findFirst(text, /Год: (.*)\n/),
|
||
color: this.findFirst(text, /Цвет: (.*)\n/),
|
||
sts: this.findFirst(text, /СТС: (.*)\n/),
|
||
stsDate: this.findFirst(text, /Дата СТС: (.*)\n/),
|
||
owner: this.findFirst(text, /Владелец: (.*)\n/),
|
||
kbm: this.findFirst(text, /КБМ: (.*)\n/),
|
||
usageRegion: this.findFirst(text, /Регион: (.*)\n/),
|
||
status: this.findFirst(text, /Статус: (.*)\n/),
|
||
insurant: this.findFirst(text, /Страхователь: (.*)\n/),
|
||
birthday: this.findFirst(text, /День рождения: (.*)\n/),
|
||
number: this.findFirst(text, /Полис: (.*)\n/),
|
||
};
|
||
}
|
||
|
||
parseReport(text) {
|
||
|
||
let result = text.match(/📋((?:.|\n)*?)^\n/gmu);
|
||
if(result && result.length > 0) {
|
||
|
||
let generalInfo = null;
|
||
let insurance = null;
|
||
|
||
for(let item of result) {
|
||
let block = Utils.removeNullFields(this.parseBlock(item));
|
||
if(block.sts) {
|
||
generalInfo = block;
|
||
} else if(block.number) {
|
||
insurance = block;
|
||
insurance.date = Math.floor(Date.now() / 1000);
|
||
insurance.restrictions = '';
|
||
}
|
||
}
|
||
|
||
if(generalInfo || insurance) {
|
||
return { generalInfo, insurance };
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
|
||
export { TGProvider };
|