mirror of
https://github.com/stjornleysi/telegram_glpi.git
synced 2026-04-29 00:59:55 +00:00
init
This commit is contained in:
38
lib/const.js
Normal file
38
lib/const.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const { Markup } = require('telegraf');
|
||||
exports.colors = ['Чёрный', 'Синий', 'Жёлтый', "Розовый"];
|
||||
exports.printers = ['Canon', 'Kyocera', 'Epson', 'SHARP', 'HP', "Brother", 'Lexmark']
|
||||
|
||||
exports.keyboards = {
|
||||
main: Markup.keyboard(
|
||||
[['Принтеры', 'Сброс пароля'], ["Физические устройства", "Сеть"], ['Программное обеспечение', "Отменить заявку"]]
|
||||
).resize(),
|
||||
printers: Markup.keyboard(
|
||||
[['Замена картриджа', 'Настройка принтера'], ['Другие проблемы', "Назад"]]
|
||||
).resize(),
|
||||
applications: Markup.keyboard(
|
||||
[['Загрузка сайтов', 'Локальное ПО'], ['Виртуальные машины', 'Назад']]
|
||||
).resize(),
|
||||
back: Markup.keyboard(
|
||||
[['Назад']]
|
||||
).resize(),
|
||||
colors: Markup.keyboard(
|
||||
[['Чёрный', 'Синий'], ['Жёлтый', "Розовый", 'Назад']]
|
||||
).resize(),
|
||||
printModels: Markup.keyboard(
|
||||
[['Canon', 'Kyocera', 'Epson', 'SHARP'], ['HP', "Brother", 'Lexmark', 'Назад']]
|
||||
).resize(),
|
||||
final: Markup.keyboard(
|
||||
[['Отправить заявку', 'Назад', 'Отменить заявку']]
|
||||
).resize(),
|
||||
start: Markup.keyboard([['Подать заявку']]).resize()
|
||||
};
|
||||
|
||||
exports.inlineKeyboards = {
|
||||
open: [[{text: '✅', callback_data: 'CloseTicket'}, {text: '💬', callback_data: 'AddComment'}]],
|
||||
close: [[{text: '✔', callback_data: 'OpenTicket'}, {text: '💬', callback_data: 'AddComment'}]],
|
||||
confirmOpen: [[{text: 'Открыть заявку', callback_data: 'ConfirmOpen'}, {text: 'Отмена', callback_data: 'RefreshStatus'}]],
|
||||
confirmClose: [[{text: 'Закрыть заявку', callback_data: 'ConfirmClose'}, {text: 'Отмена', callback_data: 'RefreshStatus'}]],
|
||||
userAddComment: [[{text: '❓ Добавить комментарий', callback_data: 'UserAddComment'}]],
|
||||
threadPin: [[{text: 'Закрыть тему', callback_data: 'CloseThread'}]],
|
||||
threadPinConfirm: [[{text: 'Подтвердить', callback_data: 'ConfirmCloseThread'}, {text: 'Отмена', callback_data: 'CancelCloseThread'}]]
|
||||
};
|
||||
152
lib/glpm.js
Normal file
152
lib/glpm.js
Normal file
@@ -0,0 +1,152 @@
|
||||
const GlpiApi = require('glpi-api');
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
|
||||
const conf = JSON.parse(fs.readFileSync(__dirname + "/../data/conf.json"));
|
||||
const glpi = new GlpiApi(conf.glpiConfig);
|
||||
|
||||
exports.createTicket = async (title, description) => {
|
||||
try{
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(conf.glpiConfig.apiurl + '/Ticket', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
},
|
||||
data: JSON.stringify({
|
||||
input: {
|
||||
name: title,
|
||||
content: description
|
||||
}
|
||||
})
|
||||
});
|
||||
return response.data.id;
|
||||
}catch(error){
|
||||
console.error('Failed to create ticket:', error);
|
||||
}
|
||||
}
|
||||
|
||||
exports.changeStatusTicket = async (ticketId, statusId) => {
|
||||
try{
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(conf.glpiConfig.apiurl + '/Ticket', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
},
|
||||
data: JSON.stringify({
|
||||
input: {
|
||||
id: ticketId,
|
||||
status: statusId
|
||||
}
|
||||
})
|
||||
});
|
||||
return response;
|
||||
}catch(error){
|
||||
console.error('Failed to create ticket:', error);
|
||||
}
|
||||
}
|
||||
|
||||
exports.getItem = async(item, id) => { // ITILFollowup = комментарий
|
||||
try{
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(`${conf.glpiConfig.apiurl}/${item}/${id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
}catch(error){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
exports.getAllItems = async(item, cnt) => {
|
||||
try{
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(`${conf.glpiConfig.apiurl}/${item}?order=DESC&range=0-${cnt}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
}
|
||||
});
|
||||
return response.data;
|
||||
}catch(error){
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
exports.addComment = async(ticketId, comment) => {
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(conf.glpiConfig.apiurl + '/ITILFollowup/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
},
|
||||
data: JSON.stringify({
|
||||
input: {
|
||||
items_id: ticketId,
|
||||
itemtype: "Ticket",
|
||||
content: comment
|
||||
}
|
||||
})
|
||||
}).catch((err) => console.log(err));
|
||||
return response.data;
|
||||
}
|
||||
|
||||
exports.removeComment = async(ticketId, commentId) => {
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(conf.glpiConfig.apiurl + '/ITILFollowup/' + commentId, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
},
|
||||
data: JSON.stringify({
|
||||
input: {
|
||||
items_id: ticketId,
|
||||
itemtype: "Ticket"
|
||||
}
|
||||
})
|
||||
}).catch((err) => console.log(err));
|
||||
return response.data;
|
||||
}
|
||||
|
||||
exports.getUsers = async(ticketId) => {
|
||||
const session = await glpi.initSession();
|
||||
let token = session.data.session_token;
|
||||
const response = await axios(`${conf.glpiConfig.apiurl}/Ticket/${ticketId}/Ticket_User`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'App-Token': conf.glpiConfig.app_token,
|
||||
'Authorization': conf.glpiConfig.user_token,
|
||||
'Session-Token': token
|
||||
}
|
||||
}).catch((err) => console.log(err));
|
||||
return response.data;
|
||||
}
|
||||
63
lib/utils.js
Normal file
63
lib/utils.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const he = require('he');
|
||||
const html = require('html-to-text');
|
||||
const fs = require('fs');
|
||||
|
||||
const conf = JSON.parse(fs.readFileSync(__dirname + "/../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];
|
||||
}
|
||||
}
|
||||
let messageText = '';
|
||||
for(let k in textArray){
|
||||
if(textArray[k][0] != '>' && textArray[k].trim() && 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>`;
|
||||
text = message.text.split('\n');
|
||||
messageText = `${silentInfo}${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) => {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user