-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.js
More file actions
153 lines (135 loc) · 4.83 KB
/
handler.js
File metadata and controls
153 lines (135 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const util = require('util');
var imaps = require('imap-simple');
var email = require('mailparser');
var request = require('request');
var imapconnection = {};
//trello variables, please insert your variables
var trello_key = 'xxxx'; //your trello API key
var trello_token = 'xxxx'; //your trello API token
var trello_list_id = 'xxxx'; //your trello default list where cards will be inserted
var config = {
imap: {
user: '', //your imap user
password: '', //your imap password
host: '', //imap host
port: 993,
tls: true,
tlsOptions: {
rejectUnauthorized: false
},
authTimeout: 3000
}
};
imaps.connect(config).then(function (connection) {
imapconnection = connection;
setInterval(controlla, 30000);
});
//controlla
//scheduled function, checking for unread ticket emails
function controlla() {
console.log('check..');
imapconnection.openBox('Inbox').then(function () {
var searchCriteria = [
'UNSEEN',
['HEADER', 'SUBJECT', '#TSE']
];
var fetchOptions = {
bodies: ['HEADER', 'TEXT'],
markSeen: false
};
return imapconnection.search(searchCriteria, fetchOptions).then(function (results) {
var subjects = results.map(function (res) {
return res.parts.filter(function (part) {
return part.which === 'HEADER';
})[0].body.subject[0];
});
if (results.length > 0) {
//insert multiple unread ticket emails
for (var y = 0; y < results.length; y++) {
var content = results[y];
var numeroTicket = parseNumeroTicket(content.parts[0].body.subject[0]);
console.log('ticket', numeroTicket);
controllaTicketEsistente(content, numeroTicket, function (trovato, content, numeroTicket) {
try {
if (!trovato) {
console.log('ticket not found', numeroTicket);
email.simpleParser(content.parts[1].body, function (err, mail) {
if (err) {
console.error(err);
} else {
console.log(mail.text);
createCard(numeroTicket, mail.body);
}
});
} else {
console.log('ticket exists!');
}
} catch (ex) {
console.error(ex);
}
});
}
}
});
});
}
//controllaTicketEsistente
//check if card exists, return callback with first argument true if found, otherwise false
function controllaTicketEsistente(content, ticket, callback) {
var ticketsAddr = 'https://api.trello.com/1/lists/' + trello_list_id + '/cards?key=' + trello_key + '&token=' + trello_token;
request(ticketsAddr, function (error, response, body) {
if (error) console.error(error);
else {
body = JSON.parse(body);
var trovato = false;
for (var x = 0; x < body.length; x++) {
var card = body[x];
console.log('card name', card.name);
if (card.name.indexOf(ticket) > -1) {
trovato = true;
break;
}
}
callback(trovato, content, ticket);
}
});
}
//createCard
//create card on trello into the specified list
function createCard(ticketNumber, ticketBody) {
var creaCard = {
method: 'POST',
url: 'https://api.trello.com/1/cards?key=' + trello_key + '&token=' + trello_token,
qs: {
idList: trello_list_id,
name: ticketNumber,
desc: ticketBody
}
};
request(creaCard, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
}
//parseNumeroTicket
//parse email subject extracting only the ticket number
//example: [WEBAPP] #TSE0001002 - application crash
//return: #TSE0001002
function parseNumeroTicket(subject) {
var ticketRegex = /#TSE([0-9]+)/g;
var res = ticketRegex.exec(subject);
if (res.length > 0)
return res[0];
return subject;
}
//getDescrizione
//return text of email after the 'Messaggio:' string (you can change)
function getDescrizione(body) {
console.log('getDescrizione', body);
var ricerca = 'Messaggio:<';
var idx = body.indexOf(ricerca);
if (idx > -1) {
body = body.substring((idx + ricerca.length));
}
return body;
}