mirror of
https://github.com/stjornleysi/telegram_glpi.git
synced 2025-12-02 01:53:09 +00:00
Telegram-glpi 3.0
This commit is contained in:
116
lib/utils.js
116
lib/utils.js
@@ -1,63 +1,113 @@
|
||||
const he = require('he');
|
||||
const html = require('html-to-text');
|
||||
const fs = require('fs');
|
||||
const cns = require('./const.js');
|
||||
|
||||
const conf = JSON.parse(fs.readFileSync(__dirname + "/../data/conf.json"));
|
||||
const dir = __dirname;
|
||||
const conf = JSON.parse(fs.readFileSync(dir + "/../data/conf.json"));
|
||||
const glpiUrl = conf.glpiConfig.apiurl.replace("apirest.php", "");
|
||||
|
||||
exports.htmlToText = async(text) => {
|
||||
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];
|
||||
}
|
||||
}
|
||||
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('cellpadding="0"') == -1){
|
||||
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, '');
|
||||
}
|
||||
|
||||
exports.parseMessageText = async(message, addSilentInfo) => {
|
||||
let color = '🟢';
|
||||
if(message.hasOwnProperty('status')){
|
||||
switch(message.status){
|
||||
case 1: color = '🟢'; break;
|
||||
case 2: color = '🔵'; break;
|
||||
case 4: color = '🟠'; break;
|
||||
case 6: color = '⚫'; break;
|
||||
default: color = '⚫';
|
||||
}
|
||||
}
|
||||
let ticketId = message.text.split('\n')[0].split('№')[1];
|
||||
if(!addSilentInfo){
|
||||
addSilentInfo = '';
|
||||
}else addSilentInfo = '/' + addSilentInfo;
|
||||
let silentInfo = `<a href="${message.entities[0].url}${addSilentInfo}">​</a>`;
|
||||
exports.parseMessageText = async (message, messageData, ticketId) => {
|
||||
let color = await this.getTicketColor(messageData.data[ticketId].status);
|
||||
text = message.text.split('\n');
|
||||
messageText = `${silentInfo}${color} <b>ЗАЯВКА <a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">№${ticketId}</a></b>\n\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`;
|
||||
}
|
||||
for(let i in message.entities){
|
||||
if(message.entities[i].type == 'text_link' && message.entities[i].url.indexOf(glpiUrl + 'front/document.send.php?docid=') > 0){
|
||||
messageText = messageText.substring(0, messageText.length - 1).replace('screen', '');
|
||||
messageText += `<a href="${message.entities[i].url}">screen</a>`;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
exports.sleep = async(ms) => {
|
||||
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));
|
||||
}
|
||||
|
||||
exports.closeThread = async (bot, messageData, ticketId) => {
|
||||
await bot.telegram.deleteForumTopic(conf.supportChatId, messageData.data[ticketId].threadId);
|
||||
delete messageData.data[ticketId].threadId;
|
||||
delete messageData.data[ticketId].pinMessageId;
|
||||
let jsonData = JSON.stringify(messageData, null, 3);
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", jsonData);
|
||||
}
|
||||
|
||||
exports.getTicketColor = async (status) => {
|
||||
switch(status){
|
||||
case 1: color = '🟢'; break;
|
||||
case 2: color = '🔵'; break;
|
||||
case 3: color = '🔵'; break;
|
||||
case 4: color = '🟠'; break;
|
||||
case 6: color = '⚫'; break;
|
||||
default: color = '⚫';
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
exports.getKeyboardFromStatus = async (status) => {
|
||||
let keyboard = cns.inlineKeyboards.open;
|
||||
if(status == 5 || status == 6){
|
||||
keyboard = cns.inlineKeyboards.close;
|
||||
}
|
||||
return keyboard;
|
||||
}
|
||||
|
||||
exports.editMessageText = async (bot, messageId, messageText, keyboard) => {
|
||||
try{
|
||||
await bot.telegram.editMessageText(conf.supportChatId, messageId, undefined, messageText, {
|
||||
parse_mode: 'HTML',
|
||||
reply_markup: {inline_keyboard: keyboard}
|
||||
});
|
||||
}catch(e){
|
||||
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify(e, null, 3));
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
exports.sleep = async (ms) => {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user