Skip to content

Commit

Permalink
Merge pull request #59 from jundis/2.0
Browse files Browse the repository at this point in the history
2.0 Project
  • Loading branch information
jundis authored Oct 27, 2016
2 parents 7940e9f + e2656a0 commit 5e8d0ca
Show file tree
Hide file tree
Showing 14 changed files with 1,108 additions and 311 deletions.
60 changes: 46 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,31 @@ To search for ConnectWise configuration records, use cwslack-configs.php.

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

To modify the MySQL database in Slack, please configure and setup cwslack-dbmanage.php

# Installation Instructions

## cwslack.php, activities, contacts, notes, and configs
This script set and all modules require PHP version 5 and the cURL extension, and many require a MySQL or MariaDB server.

For non-MySQL installation instructions, please see README_NoMySQL.md

## cwslack.php, activities, contacts, notes, configs, and dbmanage

1. Download the respective php file, functions.php, and config.php files.
1. Download the respective php file, functions.php, install.php, and config.php files.
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 reflect the task necessary. E.x. /t for tickets, /act for activities, /note for notes.
5. Set the URL to https://domain.tld/cwslack.php (or other php file)
6. Set Method to GET
7. Copy the token
8. Set a name, icon, and auto complete text if wanted.
9. Modify the config.php file with your companies values and timezone. Full configuration info below.
10. Test it in Slack!
9. Run install.php and proceed through database setup. This will also verify you have the required PHP and cURL versions.
10. Modify the config.php file with your companies values and timezone. Full configuration info below.
11. Test it in Slack!

## cwslack-incoming.php

1. Download the cwslack-incoming.php, functions.php, and config.php files.
1. Download the cwslack-incoming.php, functions.php, install.php, and config.php files.
2. Place on a compatible web server
3. Create a new slack incoming web hook integration at https://my.slack.com/services/new/incoming-webhook/
4. Set a name, icon, and if wanted.
Expand All @@ -55,27 +62,29 @@ To follow tickets and get updates whenever they're updated, use cwslack-follow.p

**(Requires some variables from cwslack-incoming.php to function if you don't use that)**

1. Download the cwslack-firmalerts.php, functions.php, and config.php files.
1. Download the cwslack-firmalerts.php, functions.php, install.php, and config.php files.
2. Place on a compatible web server.
3. Change $posttousers or $posttochan to 0 in config.php if you don't want it posting to one or the other.
4. Setup a cron job or scheduled task on your server to run this PHP file **every minute.**
3. Run install.php and proceed through database setup. This will also verify you have the required PHP and cURL versions.
4. Change $posttousers or $posttochan to 0 in config.php if you don't want it posting to one or the other.
5. Setup a cron job or scheduled task on your server to run this PHP file **every minute.**
```Cron: * * * * * /usr/bin/php /var/www/cwslack-firmalerts.php >/dev/null 2>&1```
5. Set a firm appointment and test
6. Set a firm appointment and test

## cwslack-follow.php

**(Also requires cwslack-incoming.php to function)**

1. Download the cwslack-follow.php, functions.php, and config.php files.
1. Download the cwslack-follow.php, functions.php, install.php, and config.php files.
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 https://domain.tld/cwslack-follow.php
6. Set Method to GET
7. Copy the token
8. Set a name, icon, and auto complete text if wanted.
9. Modify the config.php file with your companies values, Full configuration info below.
10. Test it in Slack!
9. Run install.php and proceed through database setup. This will also verify you have the required PHP and cURL versions.
10. Modify the config.php file with your companies values and timezone. Full configuration info below.
11. Test it in Slack!

To enable ConnectWise link to follow and unfollow a ticket:

Expand Down Expand Up @@ -110,7 +119,9 @@ To enable ConnectWise link to follow and unfollow a ticket:
\& Ampersand denotes required for cwslack-contacts.php
\^ Caret denotes required for cwslack-notes.php
\% Percent denotes required for cwslack-notes.php
\= Equals denotes required for cwslack-firmalerts.php
\= Equals denotes required for cwslack-firmalerts.php
\@ At denotes required for cwslack-dbmanage.php


####All
* $connectwise * : This value needs to be set to your main ConnectWise URL.
Expand All @@ -120,6 +131,11 @@ To enable ConnectWise link to follow and unfollow a ticket:
* $apiprivatekey * : Set to your Private Key from API setup
* $slacktoken * : Set to the token you got when creating a new slash command integration in Slack.
* $timezone * : Set to your timezone according to http://php.net/manual/en/timezones.america.php .
* $usedatabase #,^,=,@ : Automatically configured by install.php
* $dbhost #,^,=,@ : Automatically configured by install.php
* $dbusername #,^,=,@ : Automatically configured by install.php
* $dbpassword #,^,=,@ : Automatically configured by install.php
* $dbdatabase #,^,=,@ : Automatically configured by install.php

####Activities
* $slackactivitiestoken + : Set to the token you got when creating a new slash command integration in Slack for /activities.
Expand Down Expand Up @@ -158,6 +174,11 @@ To enable ConnectWise link to follow and unfollow a ticket:
####Configs
* $slackconfigstoken % : Set to the token you got when creating a new slash command integration in Slack for /config.

####DBManage

* $slackdbmantoken @ : Set your token for the database management slash command
* $adminlist @ : Usernames that are allowed to use this command. Separate by pipe symbol as seen in example if you need multiple people to have access.

####General
* $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.

Expand Down Expand Up @@ -212,6 +233,17 @@ This does allow spaces for the ticket note so do not wrap in quotation marks or

## cwslack-configs.php

/config [company name]*|[config name]*
/config [company name]\*|[config name]*

Requires pipe symbol between the two, will return details on config that matches search.

## cwslack-dbmanage.php

/dbm [command]* [options]

Commands available:
* help - Display this help text
* listmap - List all username mappings between CW and Slack
* addmap [slackname]* [cwname]* - Associate the two names
* removemap [slackname]* - Remove a mapping
* clearfollow confirm* - Clears the follow database
89 changes: 89 additions & 0 deletions README_NoMySQL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# CWSlack-SlashCommands

See README.md for full configuration details and other information.

This file should only be used if you cannot use a MySQL or Maria DB server for this script. Note that future updates past 2.0 may not fully update old modules and may require MySQL for new modules.


# Installation Instructions

## cwslack.php, activities, contacts, notes, and configs

1. Download the respective php file, functions.php, and config.php files.
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 reflect the task necessary. E.x. /t for tickets, /act for activities, /note for notes.
5. Set the URL to https://domain.tld/cwslack.php (or other php file)
6. Set Method to GET
7. Copy the token
8. Set a name, icon, and auto complete text if wanted.
9. Modify the config.php file with your companies values and timezone. Full configuration info below.
10. Test it in Slack!

## cwslack-incoming.php

1. Download the cwslack-incoming.php, functions.php, and config.php files.
2. Place on a compatible web server
3. Create a new slack incoming web hook 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 Web hook 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 https://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 and timezone, make sure to set the value for $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!

## cwslack-firmalerts.php

**(Requires some variables from cwslack-incoming.php to function if you don't use that)**

1. Download the cwslack-firmalerts.php, functions.php, and config.php files.
2. Place on a compatible web server.
3. Change $posttousers or $posttochan to 0 in config.php if you don't want it posting to one or the other.
4. Setup a cron job or scheduled task on your server to run this PHP file **every minute.**
```Cron: * * * * * /usr/bin/php /var/www/cwslack-firmalerts.php >/dev/null 2>&1```
5. Set a firm appointment and test

## cwslack-follow.php

**(Also requires cwslack-incoming.php to function)**

1. Download the cwslack-follow.php, functions.php, and config.php files.
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 https://domain.tld/cwslack-follow.php
6. Set Method to GET
7. Copy the token
8. Set a name, icon, and auto complete text if wanted.
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!


# API Key Setup

1. Login to ConnectWise
2. In the top right, click on your name
3. Go to "My Account"
4. Select the "API Keys" tab
5. Click the Plus icon to create a new key
6. Provide a description and click the Save icon.
7. Save this information, you cannot retrieve the private key ever again so if lost you will need to create new ones.
35 changes: 25 additions & 10 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
$companyname = "MyCompany"; //Set your company name from Connectwise. This is the company name field from login.
$apipublickey = "Key"; //Public API key
$apiprivatekey = "Key"; //Private API key
$timezone = "America/Chicago"; //Set your timezone here.
$timezone = "America/Chicago"; //Set your timezone here.

// Database Configuration, required for if you want to use MySQL/Maria DB features.
$usedatabase = 0; // Set to 0 by default, set to 1 if you want to enable MySQL.
$dbhost = "127.0.0.1"; //Your MySQL DB
$dbusername = "username"; //Your MySQL DB Username
$dbpassword = "password"; //Your MySQL DB Password
$dbdatabase = "cwslack"; //Change if you have an existing database you want to use, otherwise leave as default.

//cwslack.php
$slacktoken = "Slack Token Here"; //Set token from the Slack slash command screen.
Expand All @@ -39,7 +46,7 @@

//cwlsack-notes.php
$slacknotestoken = "Slack Token Here"; //Set your token for the notes slash command
$usecwname = 0; //If set to 1, it will create tickets using the user's slack name. Command will fail if their slack name is not the same as connectwise name.
$usecwname = 1; //If set to 1, it will create tickets using the user's slack name. Command will fail if their slack name is not the same as connectwise name.

//cwslack-configs.php
$slackconfigstoken = "Slack Token Here"; //Set your token for the configs slash command
Expand Down Expand Up @@ -67,12 +74,16 @@
$firmalertchan = "#dispatch"; //When you want to split time alerts and firm alerts into their own channels.

//cwslack-follow.php
//Requires cwslack-incoming.php to function.
$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.
$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. Not needed if using MySQL.
$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.

//cwslack-dbmanage.php
$slackdbmantoken = "Slack Token Here"; //Set your token for the database management slash command
$adminlist = "admin1|admin2"; //Separate by pipe symbol as seen in example if you need multiple people to have access.

//Change optional
$helpurl = "https://github.com/jundis/CWSlack-SlashCommands"; //Set your help article URL here.
Expand All @@ -83,9 +94,13 @@

//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);
}

if ($usedatabase!=1) //Setup directory for Follow module.
{
if ( !file_exists($dir) ) {
$oldmask = umask(0); // helpful when used in linux server
mkdir ($dir, 0744);
}
}

?>
38 changes: 5 additions & 33 deletions cwslack-activities.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,22 @@
require_once 'config.php';
require_once 'functions.php';

$apicompanyname = strtolower($companyname); //Company name all lower case for api auth.
$authorization = base64_encode($apicompanyname . "+" . $apipublickey . ":" . $apiprivatekey); //Encode the API, needed for authorization.
// Authorization array. Auto encodes API key for auhtorization above.
$header_data = postHeader($companyname, $apipublickey, $apiprivatekey);

if(empty($_GET['token']) || ($_GET['token'] != $slackactivitiestoken)) 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($_GET['text'])) die("No text provided."); //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.

//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)); //Encode a JSON response with a help URL.
echo $test; //Return the JSON
return; //Kill the connection.
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.
}

$urlactivities = $connectwise . "/v4_6_release/apis/3.0/sales/activities/";
$activityurl = $connectwise . '/v4_6_release/ConnectWise.aspx?fullscreen=false&locale=en_US#startscreen=activity_detail&state={"p":"activity_detail", "s":{"p":{"pid":3, "rd": ';
$activityurl = $connectwise . '/v4_6_release/ConnectWise.aspx?fullscreen=false&locale=en_US#startscreen=activity_detail&state={"p":"activity_detail", "s":{"p":{"pid":3, "rd":';
$activityurl2 = ' ,"compId":0, "contId":0, "oppid":0}}}';

$utc = time(); //Get the time.
// Authorization array. Auto encodes API key for auhtorization above.
$header_data =array(
"Authorization: Basic ". $authorization,
);
// Authorization array, with extra json content-type used in patch commands to change tickets.
$header_data2 =array(
"Authorization: Basic " . $authorization,
"Content-Type: application/json"
);

$command=NULL; //Create a command variable and set it to Null
if (array_key_exists(0,$exploded)) //If a string exists in the slash command array, make it the command.
{
Expand All @@ -64,26 +51,11 @@
if($command=="new") {
$dataResponse = cURLPost(
$urlactivities,
$header_data2,
$header_data,
"POST",
array("name"=>$exploded[1],"status"=>array("id"=>1),"assignTo"=>array("identifier"=>$exploded[2])));
}

if(array_key_exists("code",$dataResponse)) { //Check if array contains error code
if($dataResponse->code == "NotFound") { //If error code is NotFound
echo "This should never occur..."; //Report that the ticket was not found.
return;
}
if($dataResponse->code == "Unauthorized") { //If error code is an authorization error
echo "401 Unauthorized, check API key to ensure it is valid."; //Fail case.
return;
}
else {
echo "Unknown Error Occurred, check API key and other API settings. Error: " . $dataResponse->code; //Fail case.
return;
}
}

$return="Unknown command.";
if($command == "new") //If command is new.
{
Expand Down
Loading

0 comments on commit 5e8d0ca

Please sign in to comment.