This commit is contained in:
atorop
2024-10-22 16:20:56 +03:00
parent 9b9e2d0355
commit 7aae218182
3 changed files with 176 additions and 152 deletions

View File

@@ -8,54 +8,66 @@ const conf = JSON.parse(fs.readFileSync(dir + "/../data/conf.json"));
const glpiUrl = conf.glpiConfig.apiurl.replace("apirest.php", "");
exports.htmlToText = async (text) => {
let temp = html.convert(he.decode(text), {preserveNewlines: true});
textArray = temp.split('\n');
for(let i in textArray){
if(textArray[i].indexOf("[/front/document.send.php?docid=") >= 0){
delete textArray[i-1];
delete textArray[i];
try{
let temp = html.convert(he.decode(text), {preserveNewlines: true});
textArray = temp.split('\n');
for(let i in textArray){
if(textArray[i].indexOf("[/front/document.send.php?docid=") >= 0){
delete textArray[i-1];
delete textArray[i];
}
}
}
let messageText = '';
for(let k in textArray){
if(textArray[k][0] != '>' && textArray[k].trim() && textArray[k].indexOf('ts@krtech.ru писал') == -1 && textArray[k].indexOf('cellpadding="0"') == -1){
messageText += textArray[k].trim().replace(/[<>]/g, '') + ' ';
let messageText = '';
for(let k in textArray){
if(textArray[k][0] != '>' && textArray[k].trim() && textArray[k].indexOf('ts@krtech.ru писал') == -1 && textArray[k].indexOf('cellpadding="0"') == -1){
messageText += textArray[k].trim().replace(/[<>]/g, '') + ' ';
}
}
return messageText.replace(/\[.*?\]/g, '');
}catch(e){
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** htmlToText:\n" + e, null, 3));
}
return messageText.replace(/\[.*?\]/g, '');
}
exports.parseMessageText = async (message, messageData, ticketId) => {
let color = await this.getTicketColor(messageData.data[ticketId].status);
text = message.text.split('\n');
messageText = `${color} <b>ЗАЯВКА <a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">№${ticketId}</a></b>\n\n`;
for(let i = 2; i < text.length; i++){
if(text[i].indexOf(':') >= 0) messageText += `<b>${text[i].replace(':', ':</b>')}\n`;
}
if(text[text.length - 1].indexOf("Читать дальше") >= 0){
messageText = `${messageText.replace('Читать дальше', '')}\n<b><a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">Читать дальше</a></b>`;
}
return messageText;
try{
let color = await this.getTicketColor(messageData.data[ticketId].status);
text = message.text.split('\n');
messageText = `${color} <b>ЗАЯВКА <a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">№${ticketId}</a></b>\n\n`;
for(let i = 2; i < text.length; i++){
if(text[i].indexOf(':') >= 0) messageText += `<b>${text[i].replace(':', ':</b>')}\n`;
}
if(text[text.length - 1].indexOf("Читать дальше") >= 0){
messageText = `${messageText.replace('Читать дальше', '')}\n<b><a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">Читать дальше</a></b>`;
}
return messageText;
}catch(e){
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** parseMessageText:\n" + e, null, 3));
}
}
exports.createThread = async (bot, messageData, ticketId, title) => {
let td = messageData.data[ticketId];
let thread = await bot.telegram.createForumTopic(conf.supportChatId, title, {
icon_custom_emoji_id: '5357315181649076022'
});
let status = td.status;
messageData.data[ticketId].threadId = thread.message_thread_id;
let inKeyboard = await this.getKeyboardFromStatus(status);
await this.editMessageMarkup(bot, td.messageId, inKeyboard);
let msg = await bot.telegram.copyMessage(conf.supportChatId, conf.supportChatId, td.messageId, {
parse_mode: 'HTML',
disable_notification: true,
message_thread_id: thread.message_thread_id,
reply_markup: { inline_keyboard: inKeyboard }
});
await bot.telegram.pinChatMessage(conf.supportChatId, msg.message_id, { disable_notification: true });
messageData.data[ticketId].pinMessageId = msg.message_id;
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
try{
let td = messageData.data[ticketId];
let thread = await bot.telegram.createForumTopic(conf.supportChatId, title, {
icon_custom_emoji_id: '5357315181649076022'
});
let status = td.status;
messageData.data[ticketId].threadId = thread.message_thread_id;
let inKeyboard = await this.getKeyboardFromStatus(status);
await this.editMessageMarkup(bot, td.messageId, inKeyboard);
let msg = await bot.telegram.copyMessage(conf.supportChatId, conf.supportChatId, td.messageId, {
parse_mode: 'HTML',
disable_notification: true,
message_thread_id: thread.message_thread_id,
reply_markup: { inline_keyboard: inKeyboard }
});
await bot.telegram.pinChatMessage(conf.supportChatId, msg.message_id, { disable_notification: true });
messageData.data[ticketId].pinMessageId = msg.message_id;
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
}catch(e){
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** createThread:\n" + e, null, 3));
}
}
exports.closeThread = async (bot, messageData, ticketId) => {
@@ -93,7 +105,7 @@ exports.editMessageText = async (bot, messageId, messageText, keyboard) => {
reply_markup: {inline_keyboard: keyboard}
});
}catch(e){
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify(e, null, 3));
//fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify(e, null, 3));
}
}
@@ -101,7 +113,7 @@ exports.editMessageMarkup = async (bot, messageId, keyboard) => {
try{
await bot.telegram.editMessageReplyMarkup(conf.supportChatId, messageId, undefined, {inline_keyboard: keyboard});
}catch(e){
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify(e, null, 3));
//fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify(e, null, 3));
}
}