Skip to content

Commit

Permalink
Created incoming webhook integration to post new tickets to Slack.
Browse files Browse the repository at this point in the history
  • Loading branch information
jundis committed Aug 5, 2016
1 parent b422202 commit c15c7c0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cwslack.php and cwslack-activities.php were designed to be independent, but both

1. Download the cwslack.php file and config.php file.
2. Place on a compatible web server
3. Create a new slack slash command integration at hhttps://SLACK TEAM.slack.com/apps/A0F82E8CA-slash-commands
3. Create a new slack slash command integration at https://SLACK TEAM.slack.com/apps/A0F82E8CA-slash-commands
4. Set command to /t (or other if you prefer)
5. Set the URL to http://domain.tld/cwslack.php
6. Set Method to GET
Expand All @@ -25,7 +25,7 @@ cwslack.php and cwslack-activities.php were designed to be independent, but both

1. Download the cwslack.php file and config.php file.
2. Place on a compatible web server
3. Create a new slack slash command integration at hhttps://SLACK TEAM.slack.com/apps/A0F82E8CA-slash-commands
3. Create a new slack slash command integration at https://SLACK TEAM.slack.com/apps/A0F82E8CA-slash-commands
4. Set command to /act (or other if you prefer)
5. Set the URL to http://domain.tld/cwslack-activities.php
6. Set Method to GET
Expand All @@ -34,6 +34,24 @@ cwslack.php and cwslack-activities.php were designed to be independent, but both
9. Modify the config.php file with your companies values, make sure to set the specific $slackactivitiestoken to the one for the activities slash command.
10. Test it in Slack!

## cwslack-incoming.php

1. Download the cwslack-incoming.php file and config.php file.
2. Place on a compatible web server
3. Create a new slack incoming webhook integration at https://my.slack.com/services/new/incoming-webhook/
4. Set a name, icon, and if wanted.
5. Set channel that you want to post to and copy the Webhook URL
6. Create a new integrator login in ConnectWise:
- Go to System > Setup Tables in the client
- Type "int" in the table field and select Integrator Login
- Create a new login with whatever username/password, we don't need this.
- Set Access Level to "All Records"
- Enable "Service Ticket API" and select the board(s) you want this to run on.
- Enter http://domain.tld/cwslack-incoming.php?id= for the callback URL (do not enable legacy format)
7. Modify the config.php file with your companies values, make sure to set the specific $webhookurl to the value copied in step 5.
8. Change the $postupdated and $postadded to what you prefer. Enabling $postupdated can get spammy.
9. Test it in Slack by creating a new ticket on the board you selected in step 6!

# Command Usage

* denotes required
Expand Down
3 changes: 3 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
$slacktoken = "Slack Token Here"; //Set token from the Slack slash command screen.
$slackactivitiestoken = "Slack Token Here"; //Set your token for the activities slash command, if you're using cwslack-activities.php
$helpurl = "https://companyknowledgebase.com/document"; //Set your help article URL here.
$webhookurl = "https://hooks.slack.com/services/tokens" //Change this if you intend to use cwslack-incoming.php
$postadded = 1; //Set this to post new tickets to slack.
$postupdated = 0; //Set this to post updated tickets to slack. Defaults to off to avoid spam

?>
82 changes: 82 additions & 0 deletions cwslack-incoming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
//Receive connector for Connectwise Callbacks
die;
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.

$data = json_decode(file_get_contents('php://input')); //Decode incoming body from connectwise callback.
$info = json_decode(stripslashes($data->Entity)); //Decode the entity field which contains the JSON data we want.

if(empty($_GET['id']) || empty($_GET['action']) || strtolower($_GET['memberId'])=="zadmin" || $_GET['isInternalAnalysis']=="True" || empty($info)) die; //If anything we need doesn't exist, kill connection.

$ticketurl = $connectwise . "/v4_6_release/services/system_io/Service/fv_sr100_request.rails?service_recid=";
$header_data =array(
"Content-Type: application/json"
);

$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=$_GET['id'];

$ch = curl_init();

if($_GET['action'] == "added" && $postadded == 1)
{
$postfieldspre = array(
"attachments"=>array(array(
"title" => "<" . $ticketurl . $ticket . "&companyName=" . $companyname . "|#" . $ticket . ">: ". $info->Summary,
"pretext" => "Ticket #" . $ticket . " has been created by " . $info->UpdatedBy . ".",
"text" => $info->CompanyName . " / " . $info->ContactName . //Return "Company / Contact" string
"\n" . "Priority: " . $info->Priority . " | " . $info->StatusName . //Return "Date Entered / Status" string
"\n" . $info->Resources, //Return assigned resources
"mrkdwn_in" => array(
"text",
"pretext",
"title"
)
))
);
}
else if($_GET['action'] == "updated" && $postupdated == 1)
{
$postfieldspre = array(
"attachments"=>array(array(
"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
{
die;
}

$postfields = json_encode($postfieldspre);

$curlOpts = array(
CURLOPT_URL => $webhookurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header_data,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_POST => 1,
CURLOPT_HEADER => 1,
);
curl_setopt_array($ch, $curlOpts);
$answer = curl_exec($ch);

// If there was an error, show it
if (curl_error($ch)) {
die(curl_error($ch));
}
curl_close($ch);

?>

0 comments on commit c15c7c0

Please sign in to comment.