AutoCatBackend/data_providers/tgprovider.js

121 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TGClient } from './tgclient.js';
import Utils from '../utils/utils.js';
import Vin01Provider from './vin01.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, vin01token) {
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);
let report = this.parseReport(text);
if(vin01token && report.generalInfo.vin) {
console.log('Found vin: ', report.generalInfo.vin);
let vin01report = await Vin01Provider.getReportVin(report.generalInfo.vin, null, vin01token);
if(vin01report.ownershipPeriods?.length > 0) {
report.ownershipPeriods = vin01report.ownershipPeriods;
}
}
return report;
} 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 };