-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcwslack-notes.php
227 lines (197 loc) · 9.33 KB
/
cwslack-notes.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
<?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/>.
*/
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_once 'functions.php';
if(empty($_REQUEST['token']) || ($_REQUEST['token'] != $slacknotestoken)) die("Slack token invalid."); //If Slack token is not correct, kill the connection. This allows only Slack to access the page for security purposes.
if(empty($_REQUEST['text'])) die("No text provided."); //If there is no text added, kill the connection.
$exploded = explode(" ",$_REQUEST['text']); //Explode the string attached to the slash command for use in variables.
//This section checks if the ticket number is not equal to 6 digits (our tickets are in the hundreds of thousands but not near a million yet) and kills the connection if it's not.
if(!is_numeric($exploded[0])) {
//Check to see if the first command in the text array is actually help, if so redirect to help webpage detailing slash command use.
if ($exploded[0]=="help") {
die(json_encode(array("parse" => "full", "response_type" => "in_channel","text" => "Please visit " . $helpurl . " for more help information","mrkdwn"=>true))); //Encode a JSON response with a help URL.
}
else //Else close the connection.
{
echo "Unknown entry for ticket number.";
return;
}
}
//Timeout Fix Block
if($timeoutfix == true)
{
ob_end_clean();
header("Connection: close");
ob_start();
echo ('{"response_type": "in_channel"}');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
session_write_close();
if($sendtimeoutwait==true) {
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral", "text" => "Please wait..."));
}
}
//End timeout fix block
$ticketnumber = $exploded[0]; //Set the ticket number to the first string
$command=NULL; //Create a command variable and set it to Null
$sentence=NULL; //Create a option variable and set it to Null
$urlticketdata = $connectwise . "/$connectwisebranch/apis/3.0/service/tickets/" . $ticketnumber; //Set ticket API url
$notype=false; // For use if they do not specify a type.
//Set URL
$noteurl = $connectwise . "/$connectwisebranch/apis/3.0/service/tickets/" . $ticketnumber . "/notes";
if (array_key_exists(1, $exploded)) //If a second string exists in the slash command array, make it the command.
{
$command = $exploded[1];
if (array_key_exists(2, $exploded)) //If a third string exists in the slash command array, make it the option for the command.
{
unset($exploded[0]);
unset($exploded[1]);
$sentence = implode(" ", $exploded);
}
}
// Authorization array, with extra json content-type used in patch commands to change tickets.
$header_data = postHeader($companyname, $apipublickey, $apiprivatekey);
//Need to create array before hand to ensure no errors occur.
$dataTNotes = array();
$ch = curl_init();
$postfieldspre = NULL; //avoid errors.
if($command == "internal") //If second part of text is internal
{
$postfieldspre = array("internalAnalysisFlag" => "True", "text" => $sentence); //Post ticket as API user
}
else if ($command == "external")//If second part of text is external
{
$postfieldspre = array("detailDescriptionFlag" => "True", "text" => $sentence);
}
else if ($command == "externalemail" || $command == "emailexternal")//If second part of text is external
{
$postfieldspre = array("detailDescriptionFlag" => "True", "processNotifications" => "True", "text" => $sentence);
}
else //If second part of text is neither external or internal
{
$notype = true;
$exploded = explode(" ",$_REQUEST['text']); // Recreate exploded so we reset the unsets.
if (array_key_exists(1, $exploded)) // Assuming any second word exists
{
unset($exploded[0]);
$sentence = implode(" ", $exploded);
}
if($defaultnotetype=="internal")
{
$postfieldspre = array("internalAnalysisFlag" => "True", "text" => $sentence); //Post ticket as API user
}
elseif($defaultnotetype=="external")
{
$postfieldspre = array("detailDescriptionFlag" => "True", "text" => $sentence);
}
elseif($defaultnotetype=="externalemail")
{
$postfieldspre = array("detailDescriptionFlag" => "True", "processNotifications" => "True", "text" => $sentence);
}
else
{
if ($timeoutfix == true) {
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral","text" => "Second part of text must be either internal or external."));
} else {
die("Second part of text must be either internal or external."); //Return error text.
}
die();
}
}
//Username mapping code
if($usedatabase==1)
{
$mysql = mysqli_connect($dbhost, $dbusername, $dbpassword, $dbdatabase); //Connect MySQL
if (!$mysql) //Check for errors
{
if ($timeoutfix == true) { //This should NEVER happen.
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral","text" => "Connection Error: " . mysqli_connect_error()));
} else {
die("Connection Error: " . mysqli_connect_error()); //Post to slack
}
die();
}
$val1 = mysqli_real_escape_string($mysql,$_REQUEST["user_name"]);
$sql = "SELECT * FROM `usermap` WHERE `slackuser`=\"" . $val1 . "\""; //SQL Query to select all ticket number entries
$result = mysqli_query($mysql, $sql); //Run result
$rowcount = mysqli_num_rows($result);
if($rowcount > 1) //If there were too many rows matching query
{
if ($timeoutfix == true) { //This should NEVER happen.
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral","text" => "Error: too many users somehow?"));
} else {
die("Error: too many users somehow?"); //Post to slack
}
die();
}
else if ($rowcount == 1) //If exactly 1 row is found.
{
$row = mysqli_fetch_assoc($result); //Row association.
$postfieldspre["member"] = array("identifier"=>$row["cwname"]); //Return the connectwise name of the row found as the CW member name.
}
else //If no rows are found
{
if($usecwname==1) //If variable enabled
{
$postfieldspre["member"] = array("identifier"=>$_REQUEST['user_name']); //Return the slack username as the user for the ticket note. If the user does not exist in CW, it will use the API username.
}
}
}
else
{
if($usecwname==1)
{
$postfieldspre["member"] = array("identifier"=>$_REQUEST['user_name']);
}
}
$dataTData = cURL($urlticketdata, $header_data); //Decode the JSON returned by the CW API.
if($dataTData==NULL)
{
if ($timeoutfix == true) {
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral","text" => "Array not returned. Please check your connectwise URL variable in config.php and ensure it is accessible via the web at " . $urlticketdata));
} else {
die("Array not returned. Please check your connectwise URL variable in config.php and ensure it is accessible via the web at " . $urlticketdata); //Return properly encoded arrays in JSON for Slack parsing.
}
die();
}
$dataTNotes = cURLPost($noteurl, $header_data, "POST", $postfieldspre);
if(array_key_exists("errors",$dataTNotes)) //If connectwise returned an error.
{
$errors = $dataTNotes->errors; //Make array easier to access.
if ($timeoutfix == true) { //Return CW error
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "ephemeral","text" => "ConnectWise Error: " . $errors[0]->message));
} else {
die("ConnectWise Error: " . $errors[0]->message); //Post to slack
}
die();
}
else //No error
{
if($notype)
{
$command = $defaultnotetype;
}
if ($timeoutfix == true) {
cURLPost($_REQUEST["response_url"], array("Content-Type: application/json"), "POST", array("parse" => "full", "response_type" => "in_channel","text" => "New " . $command . " note created on #" . $ticketnumber . ": " . $dataTData->company->identifier . " / " . $dataTData->summary . "\n\"" . $sentence . "\""));
} else {
echo json_encode(array("parse" => "full", "response_type" => "in_channel","text" => "New " . $command . " note created on #" . $ticketnumber . ": " . $dataTData->company->identifier . " / " . $dataTData->summary . "\n\"" . $sentence . "\"")); //Post to slack
}
}
?>