diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b7c7cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +scripts/config/* +!scripts/config/default.config.php + diff --git a/README.md b/README.md index c01006e..411a0ba 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ slack-integration ================= -A place where I put some integration scripts for the popular Slack messaging platform. +Some integration scripts for the popular Slack messaging platform. +## Features -#Rally Bot - +### Rally Bot We use this to query our Rally instance for defects, tasks and user stories. I have it configured to respond to a /rallyme slash command in Slack. E.g.: @@ -15,18 +15,39 @@ E.g.: Each of these returns a nicely formatted summary of the defect, story or task, including the owner/submitter, creation date, story title, project and description. For documents that have attachments, such as uploaded screenshots, the summary will include a link to the first available attachment. -#Image Bot / Gif Bot - +### Image Bot / Gif Bot This one is simple. It uses the google image search JSON API to query for images that match a keyword. It is configured to use the Safe Search feature, since we use this tool in the workplace. Being able to add some levity to our work chats with amusing images from the internet makes life more fun. E.g. /imageme kittens /gifme roflcopter - -#XKCD Bot - +### XKCD Bot As geeks, we are well acquainted with the internet comic XKCD. This bot allows you to post an XKCD comic to the room, including the ALT text, which will be displayed beneath the image. E.g. /xkcd 100 will return the "family circus" episode of XKCD. + +## Installation + +1. Clone this repository to your server + +2. Create your config file from the default template: + + ``` + cd scripts/config + cp default.config.php config.php + ``` + +3. Create a new incoming webhook in Slack + + **Note**: Leave the channel to post to set to "#general", our scripts use channel-overrides to respond wherever the user issues a slash command. + +4. Edit your config file with the incoming webhook's unique webhook URL + +5. Add a slash command to Slack for each feature (for example: "When a user enters /rallyme, POST to http://example.com/rallyme.php") + + **Note**: The scripts will respond to requests over POST or GET. + +6. If you are implementing Rally Bot, add your credentials to the config file + diff --git a/scripts/config/default.config.php b/scripts/config/default.config.php new file mode 100644 index 0000000..d2db7e7 --- /dev/null +++ b/scripts/config/default.config.php @@ -0,0 +1,19 @@ +UserName}|{$command->UserName}>"; +$userlink = 'UserName . '|' . $command->UserName . '>'; if($imageresponse->responseData == null){ //{"responseData": null, "responseDetails": "qps rate exceeded", "responseStatus": 503} @@ -34,7 +32,7 @@ $payload = "@{$userlink} asked for '{$command->Text}'\n{$returnedimageurl}"; -$ret = slack_incoming_hook_post($hook, "gifbot", $command->ChannelName, $iconurl, $emoji, $payload); +$ret = slack_incoming_hook_post($SLACK_INCOMING_HOOK_URL, "gifbot", $command->ChannelName, $iconurl, $emoji, $payload); if($ret!="ok") print_r("@tdm, gifbot got this response when it tried to post to the incoming hook for /gifme.\n{$ret}"); ?> diff --git a/scripts/imageme.php b/scripts/imageme.php index 0af8dce..828c4c2 100644 --- a/scripts/imageme.php +++ b/scripts/imageme.php @@ -1,22 +1,20 @@ UserName}|{$command->UserName}>"; +$userlink = 'UserName . '|' . $command->UserName . '>'; $maxtries = 2; $tries = 0; startover: - + $imageresponse = RunImageSearch($command->Text); $tries++; @@ -24,13 +22,13 @@ //{"responseData": null, "responseDetails": "qps rate exceeded", "responseStatus": 503} $details = $imageresponse->responseDetails; $status = $imageresponse->responseStatus; - + if($status == 503 && $tries < $maxtries) { sleep(1); goto startover; //yeah, it's a goto. deal with it. http://xkcd.com/292/ } - + print_r("Sorry @{$userlink}, no image for you! [{$details}:{$status}]\n"); //print_r($imageresponse); die; @@ -41,12 +39,12 @@ $payload = "@{$userlink} asked for '{$command->Text}'\n{$returnedimageurl}"; -$ret = slack_incoming_hook_post($hook, "imagebot", $command->ChannelName, $iconurl, $emoji, $payload); +$ret = slack_incoming_hook_post($SLACK_INCOMING_HOOK_URL, "imagebot", $command->ChannelName, $iconurl, $emoji, $payload); if($ret!="ok") print_r("@tdm, gifbot got this response when it tried to post to the incoming hook for /imageme.\n{$ret}"); - - - + + + function RunImageSearch($text) { $enc = urlencode($text); @@ -54,7 +52,7 @@ function RunImageSearch($text) $imageSearchJson = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&safe=active&rsz=8&imgsz=medium&q='.$enc); $imageresponse = json_decode($imageSearchJson); - + return $imageresponse; } ?> diff --git a/scripts/include/rally.php b/scripts/include/rally.php index 9713410..e29f800 100644 --- a/scripts/include/rally.php +++ b/scripts/include/rally.php @@ -53,11 +53,11 @@ function HandleStory($id, $channel_name) $payload = GetRequirementPayload($ref); $result = postit($channel_name, $payload->text, $payload->attachments); - + if($result=='Invalid channel specified'){ die("Sorry, the rallyme command can't post messages to your private chat.\n"); } - + if($result!="ok"){ print_r($result."\n"); print_r(json_encode($payload)); @@ -68,15 +68,10 @@ function HandleStory($id, $channel_name) } function postit($channel_name, $payload, $attachments){ - global $config, $slackCommand; - - return slack_incoming_hook_post_with_attachments( - $config['slack']['hook'], - $config['rally']['botname'], - $slackCommand->ChannelName, - $config['rally']['boticon'], - $payload, - $attachments); + + global $SLACK_INCOMING_HOOK_URL, $RALLYBOT_NAME, $RALLYBOT_ICON, $slackCommand; + + return slack_incoming_hook_post_with_attachments($SLACK_INCOMING_HOOK_URL, $RALLYBOT_NAME, $slackCommand->ChannelName, $RALLYBOT_ICON, $payload, $attachments); } @@ -169,7 +164,7 @@ function GetDefectPayload($ref) array_push($fields,$firstattachment); global $slackCommand; - + $userlink = BuildUserLink($slackCommand->UserName); $user_message = "Ok, {$userlink}, here's the defect you requested."; @@ -265,7 +260,7 @@ function GetRequirementPayload($ref) if($blocked) array_push($fields, MakeField("blocked",$blockedreason,true)); - + array_push($fields, MakeField("description",$short_description,false)); if($firstattachment!=null) @@ -300,11 +295,11 @@ function getProjectPayload($projectRefUri) $project = CallAPI($projectRefUri); } -function CallAPI($uri) +function CallAPI($url) { - global $config; + global $RALLY_USERNAME, $RALLY_PASSWORD; - $json = get_url_contents_with_basicauth($uri, $config['rally']['username'], $config['rally']['password']); + $json = get_url_contents_with_basicauth($url, $RALLY_USERNAME, $RALLY_PASSWORD); $object = json_decode($json); return $object; @@ -334,20 +329,21 @@ function FindRequirement($id) function BuildUserLink($username) { - $userlink = ""; - return $userlink; + global $SLACK_SUBDOMAIN; + $userlink = ''; + return $userlink; } function GetArtifactQueryUri($id) { - global $config; - return str_replace("[[ID]]", $id, $config['rally']['artifactquery']); + $artifactquery = "https://rally1.rallydev.com/slm/webservice/v2.0/artifact?query=(FormattedID%20=%20[[ID]])"; + return str_replace("[[ID]]", $id, $artifactquery); } function GetDefectQueryUri($id) { - global $config; - return str_replace("[[ID]]", $id, $config['rally']['defectquery']); + $defectquery = "https://rally1.rallydev.com/slm/webservice/v2.0/defect?query=(FormattedID%20=%20[[ID]])"; + return str_replace("[[ID]]", $id, $defectquery); } function FindDefect($id) diff --git a/scripts/include/rally.secrets.php b/scripts/include/rally.secrets.php deleted file mode 100644 index 1df5ee6..0000000 --- a/scripts/include/rally.secrets.php +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/scripts/include/rallyme.config.php b/scripts/include/rallyme.config.php deleted file mode 100644 index 9e0f85d..0000000 --- a/scripts/include/rallyme.config.php +++ /dev/null @@ -1,11 +0,0 @@ - diff --git a/scripts/include/slack.config.php b/scripts/include/slack.config.php deleted file mode 100644 index e14cd95..0000000 --- a/scripts/include/slack.config.php +++ /dev/null @@ -1,7 +0,0 @@ - diff --git a/scripts/include/slack.php b/scripts/include/slack.php index 38639a2..12ce6c5 100644 --- a/scripts/include/slack.php +++ b/scripts/include/slack.php @@ -31,9 +31,9 @@ function BuildSlashCommand($request) function slack_incoming_hook_post($uri, $user, $channel, $icon, $emoji, $payload){ - + $data = array( - "text" => $payload, + "text" => $payload, "channel" => "#".$channel, "username"=>$user ); @@ -58,7 +58,7 @@ function slack_incoming_hook_post($uri, $user, $channel, $icon, $emoji, $payload function slack_incoming_hook_post_with_attachments($uri, $user, $channel, $icon, $payload, $attachments){ $data = array( - "text" => $payload, + "text" => $payload, "channel" => "#".$channel, "username"=>$user, "icon_url"=>$icon, diff --git a/scripts/include/slack.secrets.php b/scripts/include/slack.secrets.php deleted file mode 100644 index e243c8c..0000000 --- a/scripts/include/slack.secrets.php +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/scripts/meme.php b/scripts/meme.php index 4c68a81..f5af660 100644 --- a/scripts/meme.php +++ b/scripts/meme.php @@ -1,7 +1,8 @@ UserName, $cmd->ChannelName, null, ":bow:", $meme); +$response = slack_incoming_hook_post($SLACK_INCOMING_HOOK_URL, $cmd->UserName, $cmd->ChannelName, null, ":bow:", $meme); mylog('sent.txt',$response); diff --git a/scripts/rallyme.php b/scripts/rallyme.php index aca567c..9409def 100644 --- a/scripts/rallyme.php +++ b/scripts/rallyme.php @@ -2,12 +2,11 @@ require('include/slack.php'); require('include/curl.php'); require('include/rally.php'); -require('include/rallyme.config.php'); -require('include/slack.config.php'); +require('config/config.php'); $slackCommand = BuildSlashCommand($_REQUEST); $rallyFormattedId = strtoupper($slackCommand->Text); $result = HandleItem($slackCommand, $rallyFormattedId); -?> \ No newline at end of file +?> diff --git a/scripts/xkcd.php b/scripts/xkcd.php index 971667b..e4de2cd 100644 --- a/scripts/xkcd.php +++ b/scripts/xkcd.php @@ -1,12 +1,10 @@ \n"; -$ret = slack_incoming_hook_post($hook, "xkcdbot", $command->ChannelName, $iconurl, $emoji, $payload); -if($ret!="ok") +$ret = slack_incoming_hook_post($SLACK_INCOMING_HOOK_URL, "xkcdbot", $command->ChannelName, $iconurl, $emoji, $payload); +if ($ret != "ok") print_r("@tdm, gifbot got this response when it tried to post to the incoming hook.\n{$ret}"); -?> \ No newline at end of file +?>