-
Notifications
You must be signed in to change notification settings - Fork 21
/
cwslack-incoming.php
503 lines (466 loc) · 20.3 KB
/
cwslack-incoming.php
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
/*
CWSlack-SlashCommands
Copyright (C) 2018 jundis
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//Receive connector for Connectwise Callbacks
ini_set('display_errors', 1); //Display errors in case something occurs
header('Content-Type: application/json'); //Set the header to return JSON, required by Slack
require_once 'config.php'; //Require the config file.
require_once 'functions.php';
$data = json_decode(file_get_contents('php://input')); //Decode incoming body from connectwise callback.
if($data==NULL)
{
die("No ticket data was submitted. This is expected behavior if you are just browsing to this page with a web browser.");
}
$info = json_decode(stripslashes($data->Entity)); //Decode the entity field which contains the JSON data we want.
//Connection kill blocks. Stops things from running if certain conditions are met.
if(empty($_REQUEST['id']) || empty($_REQUEST['action']) || empty($info)) die; //If anything we need doesn't exist, kill connection.
if($_REQUEST['action'] == "updated" && $_REQUEST['srDetailRecId']==0 && $_REQUEST['timeRecId']==0) die; //Kill connection if the update is not a note, and is something like a status change. This will prevent duplicate entries.
if($_REQUEST['isProblemDescription']=="False" && $_REQUEST['isInternalAnalysis']=="False" && $_REQUEST['isResolution']=="False") die; //Die if no actual update.
$badboards = explode("|",$badboard); //Explode with pipe seperator.
$badstatuses = explode("|",$badstatus); //Explode with pipe seperator.
$badcompanies = explode("|",$badcompany); //Explode with pipe seperator.
if (in_array($info->BoardName,$badboards)) die;
if (in_array($info->StatusName,$badstatuses)) die;
if (in_array($info->CompanyName,$badcompanies)) die;
$channel = NULL; //Set channel to NULL for future use.
if (!empty($boardmapping))
{
$explode = explode(",",$boardmapping);
foreach($explode as $item) {
$temp = explode("|",$item);
if(strcasecmp($temp[0],$info->BoardName) == 0) {
$channel = $temp[1];
}
}
}
else if (!empty($_REQUEST['board']))
{
if(strpos($_REQUEST['board'], "-") !== false)
{
$tempboards = explode("-", $_REQUEST['board']);
if(!in_array($info->BoardName, $tempboards))
{
die("Incorrect board");
}
}
else if($_REQUEST['board'] != $info->BoardName)
{
die("Incorrect board");
}
if(!empty($_REQUEST['channel'])) //If using channels in URL is set, and channel is not empty..
{
$channel = $_REQUEST['channel']; //Set $channel to the channel.
}
}
//URL creation
$ticketurl = $connectwise . "/$connectwisebranch/services/system_io/Service/fv_sr100_request.rails?service_recid="; //Set the URL required for ticket links.
$noteurl = $connectwise . "/$connectwisebranch/apis/3.0/service/tickets/" . $_REQUEST['id'] . "/notes?orderBy=id%20desc"; //Set the URL required for cURL requests to ticket note API.
$timeurl = $connectwise . "/$connectwisebranch/apis/3.0/time/entries?conditions=chargeToId=" . $_REQUEST['id'] . "&chargeToType=%27ServiceTicket%27&orderBy=dateEntered%20desc"; //Set the URL required for cURL requests to the time entry API.
$dataTData = array(); //Blank array.
$dataTimeData = array(); //Blank array.
//Set headers for cURL requests. $header_data covers API authentication while $header_data2 covers the Slack output.
$header_data = authHeader($companyname, $apipublickey, $apiprivatekey); // Authorization array. Auto encodes API key for auhtorization.
$header_data2 =array(
"Content-Type: application/json"
);
$skip = 0; //Create variable to skip posting to Slack channel while also allowing follow posts.
$date=strtotime($info->EnteredDateUTC); //Convert date entered JSON result to time.
$dateformat=date('m-d-Y g:i:sa',$date); //Convert previously converted time to a better time string.
$ticket=$_REQUEST['id'];
$usetime = 0; //For posttext internal vs external flag.
$dataarray = NULL; //For internal vs external flag.
$dateformat = "None"; //Just in case!
if($posttext==1) //Block for curl to get latest note
{
$createdby = "Error"; //Create with error just in case.
$notetext = "Error"; //Create with error just in case.
$dataTData = cURL($noteurl, $header_data); //Decode the JSON returned by the CW API.
if($posttext==1) //Verifies no curl error occurred. If one has, ignore $posttext.
{
$dataTimeData = cURL($timeurl, $header_data); //Decode the JSON returned by the CW API.
if($dataTData == NULL && $dataTimeData == NULL)
{
$posttext = 0;
}
if($posttext==1 && ($dataTData[0]->text != NULL || $dataTimeData[0]->text != NULL)) //Verified no curl error occurred as well as makes sure that if both text values == null, then there is no text to post.
{
$createdby = $dataTData[0]->createdBy; //Set $createdby to the ticket note creator.
$text = $dataTData[0]->text; //Set $text to the ticket text.
if (array_key_exists(0, $dataTData) && array_key_exists(0, $dataTimeData)) //Check if arrays exist properly.
{
$timetime = new DateTime($dataTimeData[0]->dateEntered); //Create new time object based on time entry note.
$notetime = new DateTime($dataTData[0]->dateCreated); //Create new datetime object based on ticketnote note.
if ($timetime > $notetime) //If the time entry is newer than latest ticket note.
{
$createdby = $dataTimeData[0]->enteredBy; //Set $createdby to the time entry creator.
$text = $dataTimeData[0]->notes; //
$usetime = 1; //Set time flag.
}
}
}
else
{
$posttext=0; //If text is null, ensure posttext = 0.
}
if ($usetime == 1)
{
$dataarray = $dataTimeData[0];
$notedate = $dataTimeData[0]->dateEntered;
$dateformat2=date('m-d-Y g:i:sa',strtotime($notedate));
}
else
{
$dataarray = $dataTData[0];
$notedate = $dataTData[0]->dateCreated;
$dateformat2=date('m-d-Y g:i:sa',strtotime($notedate));
}
}
}
if($_REQUEST['action'] == "added" && $postadded == 1)
{
if($posttext==0)
{
$postfieldspre = array(
"channel" => ($channel!=NULL ? "#" . $channel : NULL),
"attachments"=>array(array(
"fallback" => (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) ." created #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been created by " . (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Prority / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
else
{
$postfieldspre = array(
"channel" => ($channel!=NULL ? "#" . $channel : NULL),
"attachments"=>array(array(
"fallback" => (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) ." created #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been created by " . (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Prority / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
),
array(
"pretext" => "Latest " . ($dataarray->internalAnalysisFlag == "true" ? "Internal" : "External") . " Note (" . $dateformat2 . ") from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
}
else if($_REQUEST['action'] == "updated" && $postupdated == 1)
{
if($posttext==0)
{
$postfieldspre = array(
"channel" => ($channel!=NULL ? "#" . $channel : NULL),
"attachments"=>array(array(
"fallback" => $info->UpdatedBy . " updated #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been updated by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . $dateformat . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext"
)
))
);
}
else
{
$postfieldspre = array(
"channel" => ($channel!=NULL ? "#" . $channel : NULL),
"attachments"=>array(array(
"fallback" => $info->UpdatedBy . " updated #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been updated by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . $dateformat . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext"
)
),
array(
"pretext" => "Latest " . ($dataarray->internalAnalysisFlag == "true" ? "Internal" : "External") . " Note (" . $dateformat2 . ") from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
}
else
{
$skip=1;
}
if($skip==0)
{
cURLPost($webhookurl, $header_data2, "POST", $postfieldspre);
}
if($followenabled==1)
{
$alerts = array(); //Create a blank array.
if($usedatabase==1)
{
$mysql = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbdatabase); //Connect MySQL
if (!$mysql) //Check for errors
{
die("Connection Error: " . mysqli_connect_error()); //Die with error if error found
}
$val1 = mysqli_real_escape_string($mysql,$ticket);
$sql = "SELECT * FROM `follow` WHERE `ticketnumber`=\"" . $val1 . "\""; //SQL Query to select all ticket number entries
$result = mysqli_query($mysql, $sql); //Run result
if(mysqli_num_rows($result) > 0) //If there were rows matching query
{
while($row = mysqli_fetch_assoc($result)) //While we still have rows to work with
{
$alerts[]=$row["slackuser"]; //Add user to alerts array.
}
}
}
else
{
die();
}
if(!empty($alerts)) {
foreach ($alerts as $username) //For each user in alerts array, set $postfieldspre to the follow message.
{
if ($_REQUEST['action'] == "added")
{
if ($posttext == 0)
{
$postfieldspre = array(
"channel" => "@" . $username,
"attachments" => array(array(
"fallback" => (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) ." created #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: " . $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been created by " . (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Prority / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
} else {
$postfieldspre = array(
"channel" => "@" . $username,
"attachments" => array(array(
"fallback" => (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) ." created #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: " . $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been created by " . (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Prority / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
),
array(
"pretext" => "Latest " . ($dataarray->internalAnalysisFlag == "true" ? "Internal" : "External") . " Note (" . $dateformat2 . ") from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
} else if ($_REQUEST['action'] == "updated") {
if ($posttext == 0) {
$postfieldspre = array(
"channel" => "@" . $username,
"attachments" => array(array(
"fallback" => $info->UpdatedBy . " updated #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: " . $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been updated by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . $dateformat . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext"
)
))
);
} else {
$postfieldspre = array(
"channel" => "@" . $username,
"attachments" => array(array(
"fallback" => $info->UpdatedBy . " updated #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: " . $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been updated by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . $dateformat . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext"
)
),
array(
"pretext" => "Latest " . ($dataarray->internalAnalysisFlag == "true" ? "Internal" : "External") . " Note (" . $dateformat2 . ") from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
)
)
);
}
}
cURLPost($webhookurl, $header_data2, "POST", $postfieldspre);
}
}
}
//Block for if ticket time reaches past X value
if($timeenabled==1 && $info->ActualHours>$timepast)
{
if($_REQUEST['action'] == "added")
{
if($posttext==0)
{
$postfieldspre = array(
"channel"=>$timechan,
"attachments"=>array(array(
"fallback" => (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) ." created #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"color" => "#F0E68C",
"pretext" => "Ticket #" . $ticket . " has been created by " . (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Prority / Status" string
"\n" . $info->Resources . " | Total Hours: *" . $info->ActualHours . "*", //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
else
{
$postfieldspre = array(
"channel"=>$timechan,
"attachments"=>array(array(
"fallback" => (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) ." created #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"color" => "#F0E68C",
"pretext" => "Ticket #" . $ticket . " has been created by " . (strtolower($_REQUEST['memberId'])=="zadmin" ? $info->ContactName : $info->UpdatedBy) . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Prority / Status" string
"\n" . $info->Resources . " | Total Hours: *" . $info->ActualHours . "*", //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
),
array(
"pretext" => "Latest " . ($dataarray->internalAnalysisFlag == "true" ? "Internal" : "External") . " Note (" . $dateformat2 . ") from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
}
else if($_REQUEST['action'] == "updated")
{
if ($posttext == 0) {
$postfieldspre = array(
"channel" => $timechan,
"attachments" => array(array(
"fallback" => $info->UpdatedBy . " updated #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: " . $info->Summary,
"color" => "#F0E68C",
"pretext" => "Ticket #" . $ticket . " has been updated by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . $dateformat . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources . " | Total Hours: *" . $info->ActualHours . "*", //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext"
)
))
);
} else {
$postfieldspre = array(
"channel" => $timechan,
"attachments" => array(array(
"fallback" => $info->UpdatedBy . " updated #" . $ticket . " - " . ($postcompany ? "(" . $info->CompanyName . ") " : "") . $info->Summary,
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: " . $info->Summary,
"color" => "#F0E68C",
"pretext" => "Ticket #" . $ticket . " has been updated by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " | " . $info->ContactName . //Return "Company / Contact" string
"\n" . $dateformat . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources . " | Total Hours: *" . $info->ActualHours . "*", //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext"
)
),
array(
"pretext" => "Latest " . ($dataarray->internalAnalysisFlag == "true" ? "Internal" : "External") . " Note (" . $dateformat2 . ") from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
)
)
);
}
}
cURLPost($webhookurl, $header_data2, "POST", $postfieldspre);
}
?>