mirror of
https://github.com/stjornleysi/telegram_glpi.git
synced 2025-11-29 08:33:11 +00:00
bugfix
This commit is contained in:
133
lib/glpiParse.js
133
lib/glpiParse.js
@@ -13,34 +13,29 @@ const glpiUrl = conf.glpiConfig.apiurl.replace("apirest.php", "");
|
||||
exports.parseTickets = async (bot, messageData) => {
|
||||
let listTickets = await glpm.getAllItems('Ticket', 5);
|
||||
for(let i = 4; i >= 0; i--){
|
||||
let ticketId;
|
||||
try{
|
||||
let ticketId;
|
||||
if(!listTickets[i]) break;
|
||||
ticketId = listTickets[i].id;
|
||||
if(ticketId <= messageData.ticket) continue;
|
||||
if(listTickets[i].users_id_recipient != conf.glpiConfig.user_id){
|
||||
let usersArray = await glpm.getUsers(ticketId);
|
||||
let authorEmail;
|
||||
if(!usersArray[0]) continue;
|
||||
if(usersArray[0].hasOwnProperty('alternative_email') && usersArray[0].alternative_email){
|
||||
authorEmail = usersArray[0].alternative_email;
|
||||
let userRecipient = listTickets[i].users_id_recipient;
|
||||
let ticketAuthor = "Unknown";
|
||||
if(userRecipient != conf.glpiConfig.user_id){
|
||||
if(userRecipient != 0){
|
||||
let temp = await glpm.getItem("User", userRecipient);
|
||||
ticketAuthor = temp.firstname + ' ' + temp.realname;
|
||||
}else{
|
||||
let temp = await glpm.getItem("User", usersArray[0].users_id);
|
||||
authorEmail = temp.firstname + ' ' + temp.realname;
|
||||
}
|
||||
let text = await htmlToText(listTickets[i].content);
|
||||
let messageText = `🟢 <b>ЗАЯВКА <a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">№${ticketId}</a></b>\n\n`;
|
||||
messageText += `<b>Автор заявки: </b>${authorEmail}\n`;
|
||||
messageText += `<b>Проблема: </b>${listTickets[i].name}\n<b>Описание: </b>`;
|
||||
messageText += text;
|
||||
if(messageText.length > 600){
|
||||
messageText = `${messageText.substring(0, 500)} + '\n\n<b><a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">Читать дальше</a></b>`;
|
||||
let usersArray = await glpm.getUsers(ticketId);
|
||||
if(usersArray[0].hasOwnProperty('alternative_email')){
|
||||
ticketAuthor = usersArray[0].alternative_email;
|
||||
}
|
||||
}
|
||||
let messageText = await parseMessageText(listTickets[i], ticketAuthor, "🟢");
|
||||
let messg = await bot.telegram.sendMessage(conf.supportChatId, messageText, {
|
||||
parse_mode: 'HTML',
|
||||
reply_markup: { inline_keyboard: cns.inlineKeyboards.open }
|
||||
});
|
||||
await editMessageText(bot, messg.message_id, messageText, cns.inlineKeyboards.open);
|
||||
//await editMessageText(bot, messg.message_id, messageText, cns.inlineKeyboards.open);
|
||||
messageData.data[ticketId] = {
|
||||
messageId: messg.message_id,
|
||||
status: 1
|
||||
@@ -49,20 +44,21 @@ exports.parseTickets = async (bot, messageData) => {
|
||||
await createThread(bot, messageData, ticketId, title);
|
||||
await sleep(1000);
|
||||
}
|
||||
messageData.ticket = ticketId;
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}catch(e){
|
||||
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** parseTickets:\n" + e, null, 3));
|
||||
fs.appendFileSync(dir + "/../logs/logs.txt", e.stack);
|
||||
}
|
||||
messageData.ticket = ticketId;
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}
|
||||
}
|
||||
|
||||
exports.parseComments = async (bot, messageData) => {
|
||||
let listComments = await glpm.getAllItems('ITILFollowup', 5);
|
||||
for (let i = 4; i >= 0; i--) {
|
||||
let commentId;
|
||||
try{
|
||||
if(!listComments[i]) break;
|
||||
let commentId = listComments[i].id;
|
||||
commentId = listComments[i].id;
|
||||
if(commentId <= messageData.comment) continue;
|
||||
if(listComments[i].users_id != conf.glpiConfig.user_id){
|
||||
let ticketId = listComments[i].items_id;
|
||||
@@ -84,13 +80,12 @@ exports.parseComments = async (bot, messageData) => {
|
||||
messageText = `${messageText.substring(0, 2400)} + '\n\n<b><a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">Читать дальше</a></b>`;
|
||||
}
|
||||
await bot.telegram.sendMessage(conf.supportChatId, messageText, {parse_mode: "HTML", message_thread_id: messageData.data[ticketId].threadId});
|
||||
await sleep(1000);
|
||||
}
|
||||
messageData.comment = commentId;
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}catch(e){
|
||||
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** parseComments:\n" + e, null, 3));
|
||||
fs.appendFileSync(dir + "/../logs/logs.txt", e.stack);
|
||||
}
|
||||
messageData.comment = commentId;
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,76 +93,52 @@ exports.refreshStatus = async (bot, messageData) => {
|
||||
let listTickets = await glpm.getAllItems('Ticket', 100);
|
||||
for(let i = 99; i >= 0; i--){
|
||||
try{
|
||||
let ticketId = listTickets[i].id;
|
||||
if(!listTickets[i] || !messageData.data.hasOwnProperty(ticketId)) continue;
|
||||
let td = messageData.data[ticketId];
|
||||
if(td.status != listTickets[i].status){
|
||||
let usersArray = await glpm.getUsers(ticketId);
|
||||
let message = await htmlToText(listTickets[i].content);
|
||||
let authorEmail;
|
||||
if(!usersArray[0]) authorEmail == "Unknown";
|
||||
else if(usersArray[0].hasOwnProperty('alternative_email') && usersArray[0].alternative_email){
|
||||
authorEmail = usersArray[0].alternative_email;
|
||||
}else if(usersArray[0].users_id == conf.glpiConfig.user_id){
|
||||
await editTicketStatus(bot, messageData, message);
|
||||
continue;
|
||||
}else{
|
||||
let temp = await glpm.getItem("User", usersArray[0].users_id);
|
||||
authorEmail = temp.firstname + ' ' + temp.realname;
|
||||
}
|
||||
let color = await getTicketColor(listTickets[i].status);
|
||||
let messageText = `${color} <b>ЗАЯВКА <a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">№${ticketId}</a></b>\n\n`;
|
||||
messageText += `<b>Автор заявки: </b>${authorEmail}\n`;
|
||||
messageText += `<b>Проблема: </b>${listTickets[i].name}\n<b>Описание: </b>`;
|
||||
messageText += message;
|
||||
if(messageText.length > 600){
|
||||
messageText = `${messageText.substring(0, 500)} + '\n\n<b><a href="${glpiUrl}front/ticket.form.php?id=${ticketId}">Читать дальше</a></b>`;
|
||||
}
|
||||
let inKeyboard = await getKeyboardFromStatus(listTickets[i].status);
|
||||
if(td.hasOwnProperty('threadId')){
|
||||
if(listTickets[i].status == 5 || listTickets[i].status == 6){
|
||||
await closeThread(bot, messageData, ticketId);
|
||||
}else{
|
||||
let title = `${color} ${ticketId} - ${listTickets[i].name}`;
|
||||
await bot.telegram.editForumTopic(conf.supportChatId, td.threadId, { name: title });
|
||||
}
|
||||
}
|
||||
await editMessageText(bot, td.messageId, messageText, inKeyboard);
|
||||
await editMessageText(bot, td.pinMessageId, messageText, inKeyboard);
|
||||
messageData.data[ticketId].status = listTickets[i].status;
|
||||
}else if(td.hasOwnProperty('threadId') && (listTickets[i].status == 5 || listTickets[i].status == 6)){
|
||||
await closeThread(bot, messageData, ticketId);
|
||||
}
|
||||
if(!listTickets[i] || !messageData.data.hasOwnProperty(listTickets[i].id)) continue;
|
||||
await this.editTicketStatus(bot, messageData, listTickets[i].id, true);
|
||||
}catch(e){
|
||||
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** refreshStatus:\n" + e, null, 3));
|
||||
fs.appendFileSync(dir + "/../logs/logs.txt", e.stack);
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}
|
||||
|
||||
exports.editTicketStatus = async (bot, messageData, message) => {
|
||||
exports.editTicketStatus = async (bot, messageData, ticketId, fastRequests) => {
|
||||
try{
|
||||
let ticketId = message.text.split('№')[1].split('\n')[0];
|
||||
let ticket = await glpm.getItem('Ticket', ticketId);
|
||||
let inKeyboard = await getKeyboardFromStatus(ticket.status);
|
||||
if(messageData.data[ticketId].status != ticket.status){
|
||||
messageData.data[ticketId].status = ticket.status;
|
||||
let color = await getTicketColor(ticket.status);
|
||||
let title = ticket.name;
|
||||
if(messageData.data[ticketId].hasOwnProperty('userChatId')){
|
||||
title = `${title.split(' - ')[1]} - ${title.split(' - ')[2]}`;
|
||||
let ticketAuthor = "Unknown";
|
||||
if(ticket.users_id_recipient != 0){
|
||||
let temp = await glpm.getItem("User", ticket.users_id_recipient);
|
||||
ticketAuthor = temp.firstname + ' ' + temp.realname;
|
||||
}else{
|
||||
let usersArray = await glpm.getUsers(ticketId);
|
||||
if(usersArray[0].hasOwnProperty('alternative_email')){
|
||||
ticketAuthor = usersArray[0].alternative_email;
|
||||
}
|
||||
}
|
||||
title = `${color} ${ticketId} - ${title}`;
|
||||
let messageText = await parseMessageText(ticket, ticketAuthor, color);
|
||||
if(messageData.data[ticketId].hasOwnProperty('threadId')){
|
||||
await bot.telegram.editForumTopic(conf.supportChatId, messageData.data[ticketId].threadId, { name: title });
|
||||
if(ticket.status == 5 || ticket.status == 6){
|
||||
await closeThread(bot, messageData, ticketId);
|
||||
}else{
|
||||
let title = `${color} ${ticketId} - ${ticket.name}`;
|
||||
await bot.telegram.editForumTopic(conf.supportChatId, messageData.data[ticketId].threadId, { name: title });
|
||||
await editMessageText(bot, messageData.data[ticketId].pinMessageId, messageText, inKeyboard);
|
||||
}
|
||||
await sleep(1000);
|
||||
}
|
||||
await editMessageText(bot, messageData.data[ticketId].messageId, messageText, inKeyboard);
|
||||
messageData.data[ticketId].status = ticket.status;
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}else if(!fastRequests){
|
||||
await editMessageMarkup(bot, messageData.data[ticketId].messageId, inKeyboard);
|
||||
if(messageData.data[ticketId].hasOwnProperty('threadId')){
|
||||
await editMessageMarkup(bot, messageData.data[ticketId].pinMessageId, inKeyboard);
|
||||
}
|
||||
let messageText = await parseMessageText(message, messageData, ticketId);
|
||||
await editMessageText(bot, messageData.data[ticketId].messageId, messageText, inKeyboard);
|
||||
await editMessageText(bot, messageData.data[ticketId].pinMessageId, messageText, inKeyboard);
|
||||
}
|
||||
await editMessageMarkup(bot, message.message_id, inKeyboard);
|
||||
fs.writeFileSync(dir + "/../data/messageData.json", JSON.stringify(messageData, null, 3));
|
||||
}catch(e){
|
||||
fs.appendFileSync(dir + "/../logs/logs.json", JSON.stringify("*** editTicketStatus:\n" + e, null, 3));
|
||||
fs.appendFileSync(dir + "/../logs/logs.txt", e.stack);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user