Skip to content

Commit

Permalink
Follow changes to allow CW links.
Browse files Browse the repository at this point in the history
  • Loading branch information
jundis committed Sep 12, 2016
1 parent f66eddb commit efdfb42
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ To follow tickets and get updates whenever they're updated, use cwslack-follow.p
9. Modify the config.php file with your companies values, Full configuration info below.
10. Test it in Slack!

To enable ConnectWise link to follow and unfollow a ticket:

1. Open Setup Tables in ConnectWise.
2. Open the "Links" table.
3. Create a new Link referencing "Service"
4. Set the Link Name to "Slack Follow"
5. Set the Link Definition to https://yourdomain.tld/cwslack-follow.php?memberid=[memberid]&srnumber=[srnumber]&method=follow
6. Create a new Link referencing "Service"
7. Set the Link Name to "Slack Unfollow"
8. Set the Link Definition to https://yourdomain.tld/cwslack-follow.php?memberid=[memberid]&srnumber=[srnumber]&method=unfollow
9. Change the "method" on these links to whatever you set your $followtoken and $unfollowtoken to in config.php.
10. Test the links!


## cwslack-incoming.php

1. Download the cwslack-incoming.php file and config.php file.
Expand Down Expand Up @@ -106,6 +120,8 @@ To follow tickets and get updates whenever they're updated, use cwslack-follow.p
* $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.
* $followtoken : Change to any value and use this in the ConnectWise link tables.
* $unfollowtoken : Change to any value and use this in the ConnectWise link tables.
* $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
3 changes: 2 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
$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.

$followtoken="follow"; //Change to random text to be used in your CW follow link if you use it. Defaults to follow which is fine for testing.
$unfollowtoken="unfollow"; //Change to random text to be used in your CW unfollow link if you use it. Defaults to unfollow which is fine for testing.

//Change optional
$helpurl = "https://github.com/jundis/CWSlack-SlashCommands"; //Set your help article URL here.
Expand Down
45 changes: 36 additions & 9 deletions cwslack-follow.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
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.
$link=0;

$exploded = explode(" ",$_GET['text']); //Explode the string attached to the slash command for use in variables.
if(empty($_GET['method']) || ($_GET['method'] != $followtoken && $_GET['method'] != $unfollowtoken)){
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.

$exploded = explode(" ",$_GET['text']); //Explode the string attached to the slash command for use in variables.
} else {
$link=1;
}

//File Handling block
if(file_exists($dir."storage.txt")) //Check if storage file exists.
Expand All @@ -39,7 +45,7 @@
}

//Check for command errors.
if(!is_numeric($exploded[0])) {
if($link==0 && !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));
Expand All @@ -53,13 +59,33 @@
};
}

$ticketnumber = $exploded[0]; //Read ticket number to variable for convenience.
$username = $_GET['user_name']; //Read Slack username to variable for convenience.
$command=NULL; //Set a null command variable, so it has something set no matter what.

if (array_key_exists(1,$exploded)) //If a second string exists in the slash command array, make it the command.
if($link==0){
$ticketnumber = $exploded[0]; //Read ticket number to variable for convenience.
$username = $_GET['user_name']; //Read Slack username to variable for convenience.

if (array_key_exists(1,$exploded)) //If a second string exists in the slash command array, make it the command.
{
$command = $exploded[1];
}
}
else
{
$command = $exploded[1];
$ticketnumber = $_GET['srnumber'];
$username = $_GET['memberid'];
if($_GET['method']==$followtoken)
{
//For future use.
}
else if ($_GET['method']==$unfollowtoken)
{
$command="unfollow"; //Set command to unfollow if it matches the CW unfollowtoken
}
else
{
die; //If matches neither token, die.
}
}

if($command=="unfollow") //If unfollow is set in the text received from Slack.
Expand All @@ -82,10 +108,11 @@
}
else //If the ticket number and username match.
{
echo "Unfollowed ticket #" .$ticketnumber; //Return text to Slack and do not output this line.
//Do not output this line.
}
}
}
echo "Unfollowed ticket #" .$ticketnumber; //Return text to Slack
$out = implode("\n",$output); //Implode all lines.
file_put_contents($dir."/storage.txt",$out); //Output to file again, excluding the line unfollowed.
}
Expand Down

0 comments on commit efdfb42

Please sign in to comment.