-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglpi.coffee
243 lines (213 loc) · 9.07 KB
/
glpi.coffee
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Description:
# Allows creation of GLPI tickets and listens for GLPI ticket references, queries the GLPI API and retrieves details to post back to the Slack channel.
#
# Dependencies:
# "hubot-env": "0.0.2",
# "hubot-slack": ">=4.4.0",
#
# Configuration:
# HUBOT_GLPI_HOST
# HUBOT_GLPI_USER_TOKEN
#
# Commands:
# <GLPI ticket URL> - Looks up and responds with information regarding the specified GLPI ticket
# hubot ticket - Adds a new GLPI ticket with the Slack user as the requester. Uses first line of the message as the ticket title and all subsequent lines as the ticket description.
#
# Author:
# Dave Jennings (@davejennings)
module.exports = (robot) ->
https = require 'https'
request = require 'request'
dateFormat = require 'dateformat'
glpi_host = process.env.HUBOT_GLPI_HOST
user_token = process.env.HUBOT_GLPI_USER_TOKEN
robot.hear /front\/ticket\.form.php\?id=(\d+)/i, (msg) ->
# base64 encoded string of - username:password e.g. johnsmith:password123
# 'Authorization': 'Basic abcdefghijklmnopqrstuvwxyz12'
#
# Or API token from user preferences - but bug with GLPI means you can't login with your regular credentials
# after you use this token once without resetting your account
# 'Authorization': 'user_token qQk2aMg2fVGAlxieC0vYRHbLOYobMHmYBQ1caMPS'
headersObj =
'Content-Type': 'application/json'
'Authorization': 'user_token ' + user_token
options =
host: glpi_host
port: 443
path: '/apirest.php/initSession/'
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
sessionDetails = JSON.parse(data)
headersObj =
'Content-Type': 'application/json'
'Session-Token': sessionDetails.session_token
options =
host: glpi_host
port: 443
path: "/apirest.php/Ticket/#{msg.match[1]}"
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
ticketDetails = JSON.parse(data)
options =
host: glpi_host
port: 443
path: "/apirest.php/Ticket/" + ticketDetails.id + "/Ticket_User"
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
userDetails = JSON.parse(data)
reporter = ""
assignee = ""
for user of userDetails
if userDetails[user].type == 1
reporterID = userDetails[user].users_id
if userDetails[user].type == 2
assigneeID = userDetails[user].users_id
options =
host: glpi_host
port: 443
path: "/apirest.php/User/" + reporterID
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
reporterDetails = JSON.parse(data)
options =
host: glpi_host
port: 443
path: "/apirest.php/User/" + assigneeID
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
assigneeDetails = JSON.parse(data)
options =
host: glpi_host
port: 443
path: "/apirest.php/killSession/"
headers: headersObj
https.get options, (res) ->
dateReported = new Date(ticketDetails.date)
dueDate = new Date(ticketDetails.time_to_resolve)
attachment =
attachments:[
title: ticketDetails.name
fallback: ticketDetails.name
title_link: 'https://' + glpi_host + '/front/ticket.form.php?id=' + ticketDetails.id
text: ticketDetails.content
fields:[
{ title: "Reported By", value: reporterDetails.firstname + " " + reporterDetails.realname, short: "true" }
{ title: "Date Reported", value: dateFormat(dateReported, "dd/mm/yyyy HH:MM"), short: "true" }
{ title: "Assigned To", value: assigneeDetails.firstname + " " + assigneeDetails.realname, short: "true" }
{ title: "Due Date", value: dateFormat(dueDate, "dd/mm/yyyy HH:MM"), short: "true" }
]
]
robot.send room: msg.envelope.room, attachment
robot.respond /ticket (.+)\n+((.|\s)+)$/i, (msg) ->
# base64 encoded string of - username:password e.g. johnsmith:password123
# 'Authorization': 'Basic abcdefghijklmnopqrstuvwxyz12'
#
# Or API token from user preferences - but bug with GLPI means you can't login with your regular credentials
# after you use this token once without resetting your account
# 'Authorization': 'user_token qQk2aMg2fVGAlxieC0vYRHbLOYobMHmYBQ1caMPS'
headersObj =
'Content-Type': 'application/json'
'Authorization': 'user_token ' + user_token
options =
host: glpi_host
port: 443
path: '/apirest.php/initSession/'
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
sessionDetails = JSON.parse(data)
headersObj =
'Content-Type': 'application/json'
'Session-Token': sessionDetails.session_token
options =
host: glpi_host
port: 443
path: "/apirest.php/search/User?criteria[0][field]=5&criteria[0][searchtype]=contains&criteria[0][value]=#{msg.envelope.user.profile.email}&uid_cols=true&withindexes=true"
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
userDetails = JSON.parse(data)
if !userDetails.data
msg.send "Ticket not created: GLPI user with email address \"#{msg.envelope.user.profile.email}\" not found."
return
userId = Object.keys(userDetails.data)
postData = input: [ {
'name': "#{msg.match[1]}"
'content': "#{msg.match[2]}"
'status': 1
'urgency': 3
'impact': 3
'priority': 1
'_users_id_requester': userId[0]
'requesttypes_id': 6
} ]
postHeadersObj =
'Content-Type': 'multipart/form-data'
'Session-Token': sessionDetails.session_token
options =
uri: "https://" + glpi_host + "/apirest.php/Ticket"
port: 443
method: 'POST'
headers: postHeadersObj
form: postData
request options, (err, res, body) ->
if err
console.log(err)
newTicket = JSON.parse(body)
options =
host: glpi_host
port: 443
path: "/apirest.php/Ticket/" + newTicket[0].id + "/Ticket_User"
headers: headersObj
https.get options, (res) ->
data = ""
res.on 'data', (chunk) ->
data += chunk.toString()
res.on 'end', () ->
userDetails = JSON.parse(data)
for user of userDetails
if userDetails[user].type == 2
ticketUserID = userDetails[user].id
# Delete default assignment to bot user
options =
uri: "https://" + glpi_host + "/apirest.php/Ticket_User/" + ticketUserID
port: 443
method: 'DELETE'
headers: headersObj
form: postData
request options, (err, res, body) ->
if err
console.log(err)
options =
host: glpi_host
port: 443
path: "/apirest.php/killSession/"
headers: headersObj
https.get options, (res) ->
msg.send "Ticket created - https://" + glpi_host + "/front/ticket.form.php?id=" + newTicket[0].id