Skip to content

Commit

Permalink
Merge pull request #12 from jundis/dev
Browse files Browse the repository at this point in the history
Merging in Follow codes
  • Loading branch information
jundis authored Aug 26, 2016
2 parents 1917eae + 6e42d96 commit 68c8775
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 18 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ For creating new activities, use cwslack-activities.php.

To post new tickets or ticket updates to a Slack channel, use cwslack-incoming.php

To follow tickets and get updates whenever they're updated, use cwslack-follow.php (requires cwslack-incoming.php)

# Installation Instructions

## cwslack.php
Expand All @@ -27,7 +29,7 @@ To post new tickets or ticket updates to a Slack channel, use cwslack-incoming.p

## cwslack-activities.php

1. Download the cwslack.php file and config.php file.
1. Download the cwslack-activities.php file and config.php file.
2. Place on a compatible web server
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)
Expand All @@ -38,6 +40,19 @@ To post new tickets or ticket updates to a Slack channel, use cwslack-incoming.p
9. Modify the config.php file with your companies values, Full configuration info below.
10. Test it in Slack!

## cwslack-follow.php (Also requires cwslack-incoming.php to function)

1. Download the cwslack-follow.php file and config.php file.
2. Place on a compatible web server
3. Create a new slack slash command integration at https://SLACK TEAM.slack.com/apps/A0F82E8CA-slash-commands
4. Set command to /follow (or other if you prefer)
5. Set the URL to http://domain.tld/cwslack-follow.php
6. Set Method to GET
7. Copy the token
8. Set a name, icon, and autocomplete text if wanted.
9. Modify the config.php file with your companies values, Full configuration info below.
10. Test it in Slack!

## cwslack-incoming.php

1. Download the cwslack-incoming.php file and config.php file.
Expand Down Expand Up @@ -71,6 +86,7 @@ To post new tickets or ticket updates to a Slack channel, use cwslack-incoming.p
\* Asterisk denotes required.
\+ Plus denotes required for cwslack-activities.php
\- Minus denotes required for cwslack-incoming.php
\# Pound denotes required for cwslack-follow.php

* $connectwise * : This value needs to be set to your main connectwise URL.
* $companyname * : This value needs to be set to whatever your company name is that you use to login to ConnectWise.
Expand All @@ -87,6 +103,9 @@ To post new tickets or ticket updates to a Slack channel, use cwslack-incoming.p
* $badstatus : Set this if you have a status you want to ignore, set to Closed by default as tickets are rarely automatically closed.
* $badcompany : Set this if you have a company you want to ignore, set to CatchAll by default to avoid spam from unknown incoming e-mails.
* $posttext : Set this to 1 if you want to include the latest note with the Slack message. Set to 1 by default now.
* $slackfollowtoken # : Set to the token you got when creating a new slash command integration in Slack for /follow.
* $followenabled # : Defaults to 0, you need to change this to 1 if you want to enable follow.
* $dir # : Directory on the server to store files. Please note that the user running php (www-data in Linux) needs write/read access to this folder.
* $helpurl : Set to a help document explaining the slash command usage. Feel free to point to this GitHub repo, but ideally you would make it look pretty on your own internal site.

# Command Usage
Expand Down Expand Up @@ -118,3 +137,9 @@ If $posttext=1 in config.php, shows you the initial note of the ticket. This dis
/act new\*|[activity title]\*|[assigned to]*

All are required for activities. New will be replaced with more commands in the future.

## cwslack-follow.php

/follow [ticket number]* (unfollow)

Add unfollow to the end of the command to stop following a ticket.
10 changes: 10 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
$badcompany = "CatchAll (for email connector)"; //Set to any company name you want to fail, to avoid ticket creation for catchall from posting to Slack.
$posttext = 1; //Set to 1 if you want it to post the latest note from the ticket into chat whenever a ticket is created or updated.

//cwslack-follow.php
$slackfollowtoken = "Slack Token Here"; //Set your token for the follow slash command
$followenabled=0; //When set to 1, follow commands and the follow scripts will be enabled.
$dir="/var/www/storage/"; //Needs to be set to a directory writeable by PHP for storage of the storage.txt file.


//Change optional
$helpurl = "https://github.com/jundis/CWSlack-SlashCommands"; //Set your help article URL here.

Expand All @@ -33,5 +39,9 @@

//Timezone Setting to be used for all files.
date_default_timezone_set($timezone);
if ( !file_exists($dir) ) {
$oldmask = umask(0); // helpful when used in linux server
mkdir ($dir, 0744);
}

?>
78 changes: 78 additions & 0 deletions cwslack-follow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
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';

if(empty($_GET['token']) || ($_GET['token'] != $slackfollowtoken)) die; //If Slack token is not correct, kill the connection. This allows only Slack to access the page for security purposes.
if(empty($_GET['text'])) die; //If there is no text added, kill the connection.

if(file_exists($dir."storage.txt"))
{
$file = file_get_contents($dir."/storage.txt",FILE_SKIP_EMPTY_LINES);
}
else
{
$f = fopen($dir."storage.txt") or die("can't open file");
fclose($f);
$file = file_get_contents($dir."/storage.txt",FILE_SKIP_EMPTY_LINES);
}
$exploded = explode(" ",$_GET['text']); //Explode the string attached to the slash command for use in variables.

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") {
$test=json_encode(array("parse" => "full", "response_type" => "in_channel","text" => "Please visit " . $helpurl . " for more help information","mrkdwn"=>true));
echo $test;
return;
}
else //Else close the connection.
{
echo "Unknown entry for ticket number.";
return;
};
}

$ticketnumber = $exploded[0];
$username = $_GET['user_name'];
$command=NULL;

if (array_key_exists(1,$exploded)) //If a second string exists in the slash command array, make it the command.
{
$command = $exploded[1];
}

if($command=="unfollow")
{
$lines = explode("\n",$file);

foreach($lines as $line)
{
$tempex = explode("^",$line);

if($tempex[0]!=$ticketnumber)
{
$output[] = $line;
}
else
{
if($tempex[1]!=$username)
{
$output[]=$line;
}
else
{
echo "Unfollowed ticket #" .$ticketnumber;
}
}
}
$out = implode("\n",$output);
file_put_contents($dir."/storage.txt",$out);
}
else
{
file_put_contents($dir."/storage.txt","\n".$ticketnumber."^".$username,FILE_APPEND);
echo "Now following ticket #" . $ticketnumber;
}


?>
Loading

0 comments on commit 68c8775

Please sign in to comment.