Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify script configuration #5

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scripts/config/*
!scripts/config/default.config.php

37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.:
Expand All @@ -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

19 changes: 19 additions & 0 deletions scripts/config/default.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

////////////////////
// Slack settings //
////////////////////

$SLACK_INCOMING_HOOK_URL = 'REPLACE ME'; //URL of an Incoming WebHook including https:// and token value

$SLACK_SUBDOMAIN = 'REPLACE ME'; //subdomain used to identify your team's instance, like 'cim'

////////////////////
// Rally settings //
////////////////////

$RALLY_USERNAME = 'REPLACE ME';
$RALLY_PASSWORD = 'REPLACE ME';

$RALLYBOT_NAME = 'rallybot';
$RALLYBOT_ICON = 'https://yt3.ggpht.com/-vkXOTHhRGck/AAAAAAAAAAI/AAAAAAAAAAA/IBjv0oYIm5Q/s100-c-k-no/photo.jpg';
8 changes: 3 additions & 5 deletions scripts/gifme.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?
require('include/curl.php');
require('include/slack.php');
require('include/slack.config.php');
require('config/config.php');

$command = BuildSlashCommand($_REQUEST);

$hook = $config['slack']['hook'];

//use one or the other of $emoji or $iconurl
$emoji = ":camera:";
$iconurl = null;
Expand All @@ -17,7 +15,7 @@

$imageresponse = json_decode($imageSearchJson);

$userlink = "<https://cim.slack.com/team/{$command->UserName}|{$command->UserName}>";
$userlink = '<https://' . $SLACK_SUBDOMAIN . '.slack.com/team/' . $command->UserName . '|' . $command->UserName . '>';

if($imageresponse->responseData == null){
//{"responseData": null, "responseDetails": "qps rate exceeded", "responseStatus": 503}
Expand All @@ -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}");
?>
22 changes: 10 additions & 12 deletions scripts/imageme.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
<?
require('include/curl.php');
require('include/slack.php');
require('include/slack.config.php');
require('config/config.php');

$command = BuildSlashCommand($_REQUEST);

$hook = $config['slack']['hook'];

//use one or the other of $emoji or $iconurl
$emoji = ":camera:";
$iconurl = null;
$userlink = "<https://cim.slack.com/team/{$command->UserName}|{$command->UserName}>";
$userlink = '<https://' . $SLACK_SUBDOMAIN . '.slack.com/team/' . $command->UserName . '|' . $command->UserName . '>';
$maxtries = 2;
$tries = 0;


startover:

$imageresponse = RunImageSearch($command->Text);
$tries++;

if($imageresponse->responseData == null){
//{"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;
Expand All @@ -41,20 +39,20 @@

$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);

$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;
}
?>
40 changes: 18 additions & 22 deletions scripts/include/rally.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}


Expand Down Expand Up @@ -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.";

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -334,20 +329,21 @@ function FindRequirement($id)

function BuildUserLink($username)
{
$userlink = "<https://cim.slack.com/team/{$username}|@{$username}>";
return $userlink;
global $SLACK_SUBDOMAIN;
$userlink = '<https://' . $SLACK_SUBDOMAIN . '.slack.com/team/' . $username . '|@' . $username . '>';
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)
Expand Down
4 changes: 0 additions & 4 deletions scripts/include/rally.secrets.php

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/include/rallyme.config.php

This file was deleted.

7 changes: 0 additions & 7 deletions scripts/include/slack.config.php

This file was deleted.

6 changes: 3 additions & 3 deletions scripts/include/slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions scripts/include/slack.secrets.php

This file was deleted.

5 changes: 3 additions & 2 deletions scripts/meme.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?
require('include/memegenerator.php');
require('include/slack.php');
require('include/curl.php');
require('include/slack.php');
require('config/config.php');

/*
token=K2gDHdWZvZSmwOkW9O6yVbA7
Expand Down Expand Up @@ -34,7 +35,7 @@
$meme = CreateNewMeme($gen, $top, $bottom);
mylog('sent.txt',$meme);

$response = slack_incoming_hook_post($config['slack']['hook'], $cmd->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);

Expand Down
5 changes: 2 additions & 3 deletions scripts/rallyme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
?>
?>
10 changes: 4 additions & 6 deletions scripts/xkcd.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?
require('include/curl.php');
require('include/slack.php');
require('include/slack.config.php');
require('config/config.php');

$command = BuildSlashCommand($_REQUEST);

$hook = $config['slack']['hook'];

//use one or the other of $emoji or $iconurl
$emoji = null;
$iconurl = "http://upload.wikimedia.org/wikipedia/en/1/13/Stick_figure.png";
Expand All @@ -24,7 +22,7 @@

$payload = "{$image}\n<http://xkcd.com/{$comicid}/|{$alt}>\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}");
?>
?>