diff --git a/scripts/gifme.php b/scripts/gifme.php index 4dc4fc5..eb19f93 100644 --- a/scripts/gifme.php +++ b/scripts/gifme.php @@ -1,4 +1,4 @@ -Text); -$imageSearchJson = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&safe=active&as_filetype=gif&rsz=8&imgsz=medium&q=animated+'.$enc); +$imageSearchJson = get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&safe=active&as_filetype=gif&rsz=8&imgsz=medium&q=animated+' . $enc); $imageresponse = json_decode($imageSearchJson); $userlink = "UserName}|{$command->UserName}>"; -if($imageresponse->responseData == null){ +if ($imageresponse->responseData == null) { //{"responseData": null, "responseDetails": "qps rate exceeded", "responseStatus": 503} $details = $imageresponse->responseDetails; $status = $imageresponse->responseStatus; @@ -29,12 +29,11 @@ die; } -$whichImage = rand(0,7); +$whichImage = rand(0, 7); $returnedimageurl = $imageresponse->responseData->results[$whichImage]->url; $payload = "@{$userlink} asked for '{$command->Text}'\n{$returnedimageurl}"; $ret = slack_incoming_hook_post($hook, "gifbot", $command->ChannelName, $iconurl, $emoji, $payload); -if($ret!="ok") +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..2807637 100644 --- a/scripts/imageme.php +++ b/scripts/imageme.php @@ -1,4 +1,4 @@ -Text); $tries++; -if($imageresponse->responseData == null){ +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/ + + 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; } -$whichImage = rand(0,7); +$whichImage = rand(0, 7); $returnedimageurl = $imageresponse->responseData->results[$whichImage]->url; $payload = "@{$userlink} asked for '{$command->Text}'\n{$returnedimageurl}"; $ret = slack_incoming_hook_post($hook, "imagebot", $command->ChannelName, $iconurl, $emoji, $payload); -if($ret!="ok") +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); + $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); - $imageresponse = json_decode($imageSearchJson); - - return $imageresponse; + return $imageresponse; } -?> diff --git a/scripts/include/curl.php b/scripts/include/curl.php index d3ff09c..ab6050c 100644 --- a/scripts/include/curl.php +++ b/scripts/include/curl.php @@ -1,30 +1,32 @@ - \ No newline at end of file diff --git a/scripts/include/googleimage.php b/scripts/include/googleimage.php index 67b4cec..230f577 100644 --- a/scripts/include/googleimage.php +++ b/scripts/include/googleimage.php @@ -1,39 +1,36 @@ -Size = $size; - $cmd->Query = $query; - $cmd->Count = $maxresults; - $cmd->Safe = $safe; - $cmd->FileType = $filetype; - - return $cmd; + + $cmd = new stdClass(); + $cmd->Size = $size; + $cmd->Query = $query; + $cmd->Count = $maxresults; + $cmd->Safe = $safe; + $cmd->FileType = $filetype; + + return $cmd; } function GetImageSearchResponse($cmd) { - - $googleImageSearch = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&safe={$cmd->Safe}&as_filetype={$cmd->FileType}&rsz={$cmd->Count}&imgsz={$cmd->Size}&q={$cmd->Query}"; - $result = CallAPI($googleImageSearch); + $googleImageSearch = "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&safe={$cmd->Safe}&as_filetype={$cmd->FileType}&rsz={$cmd->Count}&imgsz={$cmd->Size}&q={$cmd->Query}"; - return $result; + $result = CallAPI($googleImageSearch); + + return $result; } function GetRandomResultFromResponse($n, $result) { - $resultArray = $result->responseData->results; - $count = count($resultArray); - $item = rand(0,$count-1); - return $resultArray[$item]; + $resultArray = $result->responseData->results; + $count = count($resultArray); + $item = rand(0, $count - 1); + return $resultArray[$item]; } - -?> diff --git a/scripts/include/log.php b/scripts/include/log.php index 4de5c15..54c617b 100644 --- a/scripts/include/log.php +++ b/scripts/include/log.php @@ -1,11 +1,11 @@ - diff --git a/scripts/include/memegenerator.php b/scripts/include/memegenerator.php index e994146..4a06a01 100644 --- a/scripts/include/memegenerator.php +++ b/scripts/include/memegenerator.php @@ -1,18 +1,17 @@ - diff --git a/scripts/include/rally.php b/scripts/include/rally.php index 9713410..a57d354 100644 --- a/scripts/include/rally.php +++ b/scripts/include/rally.php @@ -1,26 +1,25 @@ -ChannelName); - die; - break; - case "US": - case "TA": - return HandleStory($rallyFormattedId, $slackCommand->ChannelName); - die; - break; - default: - print_r("Sorry, I don't know what kind of rally object {$rallyFormattedId} is. If you need rallyme to work with these, buy a :beer:. I hear he likes IPAs."); - die; - break; + $rallyItemType = substr($rallyFormattedId, 0, 2); + + switch ($rallyItemType) { + + case "DE": + return HandleDefect($rallyFormattedId, $slackCommand->ChannelName); + die; + break; + case "US": + case "TA": + return HandleStory($rallyFormattedId, $slackCommand->ChannelName); + die; + break; + default: + print_r("Sorry, I don't know what kind of rally object {$rallyFormattedId} is. If you need rallyme to work with these, buy a :beer:. I hear he likes IPAs."); + die; + break; } } @@ -32,12 +31,12 @@ function HandleDefect($id, $channel_name) $result = postit($channel_name, $payload->text, $payload->attachments); - if($result=='Invalid channel specified'){ + 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"); + if ($result != "ok") { + print_r($result . "\n"); print_r(json_encode($payload)); print_r("\n"); die("Apparently the Rallyme script is having a problem. Ask about it. :frowning:"); @@ -45,7 +44,6 @@ function HandleDefect($id, $channel_name) return $result; } - function HandleStory($id, $channel_name) { $ref = FindRequirement($id); @@ -53,13 +51,13 @@ 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 == 'Invalid channel specified') { + die("Sorry, the rallyme command can't post messages to your private chat.\n"); } - - if($result!="ok"){ - print_r($result."\n"); + + if ($result != "ok") { + print_r($result . "\n"); print_r(json_encode($payload)); print_r("\n"); die("Apparently the Rallyme script is having a problem. Ask about it. :frowning:"); @@ -67,20 +65,13 @@ function HandleStory($id, $channel_name) return $result; } -function postit($channel_name, $payload, $attachments){ +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); + return slack_incoming_hook_post_with_attachments($config['slack']['hook'], $config['rally']['botname'], $slackCommand->ChannelName, $config['rally']['boticon'], $payload, $attachments); } - - function GetRallyAttachmentLink($attachmentRef) { $attachments = CallAPI($attachmentRef); @@ -97,7 +88,7 @@ function GetRallyAttachmentLink($attachmentRef) function GetDefectPayload($ref) { - global $show,$requesting_user_name; + global $show, $requesting_user_name; $object = CallAPI($ref); @@ -128,10 +119,9 @@ function GetDefectPayload($ref) $attachmentcount = $defect->Attachments->Count; $firstattachment = null; - if($attachmentcount>0) - { + if ($attachmentcount > 0) { $linktxt = GetRallyAttachmentLink($defect->Attachments->_ref); - $firstattachment = MakeField("attachment",$linktxt,false); + $firstattachment = MakeField("attachment", $linktxt, false); } $defecturi = "https://rally1.rallydev.com/#/{$projectid}d/detail/defect/{$defectid}"; @@ -141,35 +131,35 @@ function GetDefectPayload($ref) $color = "bad"; - $clean_description = html_entity_decode(strip_tags($description), ENT_HTML401|ENT_COMPAT, 'UTF-8'); + $clean_description = html_entity_decode(strip_tags($description), ENT_HTML401 | ENT_COMPAT, 'UTF-8'); $short_description = TruncateText($clean_description, 300); $fields = array( - MakeField("link",$linktext,false), + MakeField("link", $linktext, false), - MakeField("id",$itemid,true), - MakeField("owner",$owner,true), + MakeField("id", $itemid, true), + MakeField("owner", $owner, true), - MakeField("project",$projectName,true), - MakeField("created",$created,true), + MakeField("project", $projectName, true), + MakeField("created", $created, true), - MakeField("submitter",$submitter,true), - MakeField("state",$state,true), + MakeField("submitter", $submitter, true), + MakeField("state", $state, true), - MakeField("priority",$priority,true), - MakeField("severity",$severity,true), + MakeField("priority", $priority, true), + MakeField("severity", $severity, true), - MakeField("frequency",$frequency,true), - MakeField("found in",$foundinbuild,true), + MakeField("frequency", $frequency, true), + MakeField("found in", $foundinbuild, true), - MakeField("description",$short_description,false) + MakeField("description", $short_description, false) ); - if($firstattachment!=null) - array_push($fields,$firstattachment); + if ($firstattachment != null) + array_push($fields, $firstattachment); global $slackCommand; - + $userlink = BuildUserLink($slackCommand->UserName); $user_message = "Ok, {$userlink}, here's the defect you requested."; @@ -185,16 +175,11 @@ function GetRequirementPayload($ref) $requirement = null; - if($object->HierarchicalRequirement) - { + if ($object->HierarchicalRequirement) { $requirement = $object->HierarchicalRequirement; - } - elseif($object->Task) - { + } elseif ($object->Task) { $requirement = $object->Task; - } - else - { + } else { $class = get_class($object); global $slackCommand; $userlink = BuildUserLink($slackCommand->UserName); @@ -206,7 +191,6 @@ function GetRequirementPayload($ref) $title = $requirement->_refObjectName; - $ProjectFull = CallAPI($projecturi); $projectid = $ProjectFull->Project->ObjectID; $storyid = $requirement->ObjectID; @@ -226,17 +210,16 @@ function GetRequirementPayload($ref) $attachmentcount = $requirement->Attachments->Count; $firstattachment = null; - if($attachmentcount>0) - { + if ($attachmentcount > 0) { $linktxt = GetRallyAttachmentLink($requirement->Attachments->_ref); - $firstattachment = MakeField("attachment",$linktxt,false); + $firstattachment = MakeField("attachment", $linktxt, false); } $parent = null; - if($hasparent) + if ($hasparent) $parent = $requirement->Parent->_refObjectName; - $clean_description = html_entity_decode(strip_tags($description), ENT_HTML401|ENT_COMPAT, 'UTF-8'); + $clean_description = html_entity_decode(strip_tags($description), ENT_HTML401 | ENT_COMPAT, 'UTF-8'); $short_description = TruncateText($clean_description, 300); $storyuri = "https://rally1.rallydev.com/#/{$projectid}d/detail/userstory/{$storyid}"; @@ -245,32 +228,30 @@ function GetRequirementPayload($ref) $dovegray = "#CEC7B8"; - - $fields = array( - MakeField("link",$linktext,false), - MakeField("parent",$parent,false), + MakeField("link", $linktext, false), + MakeField("parent", $parent, false), - MakeField("id",$itemid,true), - MakeField("owner",$owner,true), + MakeField("id", $itemid, true), + MakeField("owner", $owner, true), - MakeField("project",$projectName,true), - MakeField("created",$created,true), + MakeField("project", $projectName, true), + MakeField("created", $created, true), - MakeField("estimate",$estimate,true), - MakeField("state",$state,true)); + MakeField("estimate", $estimate, true), + MakeField("state", $state, true) + ); - if($childcount>0) - array_push($fields,MakeField("children",$childcount,true)); + if ($childcount > 0) + array_push($fields, MakeField("children", $childcount, true)); - if($blocked) - array_push($fields, MakeField("blocked",$blockedreason,true)); - - array_push($fields, MakeField("description",$short_description,false)); + if ($blocked) + array_push($fields, MakeField("blocked", $blockedreason, true)); - if($firstattachment!=null) - array_push($fields,$firstattachment); + array_push($fields, MakeField("description", $short_description, false)); + if ($firstattachment != null) + array_push($fields, $firstattachment); global $slackCommand; $userlink = BuildUserLink($slackCommand->UserName); @@ -279,18 +260,18 @@ function GetRequirementPayload($ref) $obj = new stdClass; $obj->text = ""; $obj->attachments = MakeAttachment($user_message, "", $dovegray, $fields, $storyuri); -// print_r(json_encode($obj));die; + // print_r(json_encode($obj));die; return $obj; } - -function MakeField($title, $value, $short=false) +function MakeField($title, $value, $short = false) { $attachmentfield = array( "title" => $title, "value" => $value, - "short" => $short); + "short" => $short + ); return $attachmentfield; } @@ -310,7 +291,6 @@ function CallAPI($uri) return $object; } - function GetProjectID($projectref) { $ProjectFull = CallAPI($projectref); @@ -323,10 +303,10 @@ function FindRequirement($id) $query = GetArtifactQueryUri($id); $searchresult = CallAPI($query); -// print_r($searchresult);die; + // print_r($searchresult);die; $count = GetCount($searchresult); - if($count == 0) + if ($count == 0) NotFound($id); return GetFirstObjectFromSearchResult("HierarchicalRequirement", $searchresult); @@ -334,8 +314,8 @@ function FindRequirement($id) function BuildUserLink($username) { - $userlink = ""; - return $userlink; + $userlink = ""; + return $userlink; } function GetArtifactQueryUri($id) @@ -356,7 +336,7 @@ function FindDefect($id) $searchresult = CallAPI($query); $count = GetCount($searchresult); - if($count == 0) + if ($count == 0) NotFound($id); return GetFirstObjectFromSearchResult("Defect", $searchresult); @@ -371,15 +351,14 @@ function NotFound($id) { global $slackCommand; $userlink = BuildUserLink($slackCommand->UserName); - print_r("Sorry {$userlink}, I couldn't find {$id}");die; + print_r("Sorry {$userlink}, I couldn't find {$id}"); + die; } - function GetFirstObjectFromSearchResult($objectName, $result) { - foreach ($result->QueryResult->Results as $result) - { - if($result->_type == $objectName) + foreach ($result->QueryResult->Results as $result) { + if ($result->_type == $objectName) return $result->_ref; } global $slackCommand; @@ -392,10 +371,8 @@ function GetFirstObjectFromSearchResult($objectName, $result) function TruncateText($text, $len) { - if(strlen($text) <= $len) + if (strlen($text) <= $len) return $text; - return substr($text,0,$len)."...[MORE]"; + return substr($text, 0, $len) . "...[MORE]"; } - -?> diff --git a/scripts/include/slack.php b/scripts/include/slack.php index 38639a2..009a0f1 100644 --- a/scripts/include/slack.php +++ b/scripts/include/slack.php @@ -1,4 +1,4 @@ - $payload, - "channel" => "#".$channel, - "username"=>$user - ); + "text" => $payload, + "channel" => "#" . $channel, + "username" => $user + ); - if($icon!=null) - { + if ($icon != null) { $data['icon_url'] = $icon; - } - elseif($emoji!=null) - { + } elseif ($emoji != null) { $data['icon_emoji'] = $emoji; } - $data_string = "payload=" . json_encode($data, JSON_HEX_AMP|JSON_HEX_APOS|JSON_NUMERIC_CHECK|JSON_PRETTY_PRINT); + $data_string = "payload=" . json_encode($data, JSON_HEX_AMP | JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); - mylog('sent.txt',$data_string); + mylog('sent.txt', $data_string); return curl_post($uri, $data_string); } - - -function slack_incoming_hook_post_with_attachments($uri, $user, $channel, $icon, $payload, $attachments){ +function slack_incoming_hook_post_with_attachments($uri, $user, $channel, $icon, $payload, $attachments) +{ $data = array( - "text" => $payload, - "channel" => "#".$channel, - "username"=>$user, - "icon_url"=>$icon, - "attachments"=>array($attachments)); - - $data_string = "payload=" . json_encode($data, JSON_HEX_AMP|JSON_HEX_APOS|JSON_NUMERIC_CHECK|JSON_PRETTY_PRINT); - mylog('sent.txt',$data_string); + "text" => $payload, + "channel" => "#" . $channel, + "username" => $user, + "icon_url" => $icon, + "attachments" => array( + $attachments + ) + ); + + $data_string = "payload=" . json_encode($data, JSON_HEX_AMP | JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); + mylog('sent.txt', $data_string); return curl_post($uri, $data_string); } - - /* slack attachment format @@ -86,13 +83,16 @@ function slack_incoming_hook_post_with_attachments($uri, $user, $channel, $icon, "fields": [ { "title": "Required Field Title", // The title may not contain markup and will be escaped for you + "value": "Text value of the field. May contain standard message markup and must be escaped as normal. May be multi-line.", + "short": false // Optional flag indicating whether the `value` is short enough to be displayed side-by-side with other values } ] } */ -function MakeAttachment($pretext, $text, $color, $fields, $fallback){ +function MakeAttachment($pretext, $text, $color, $fields, $fallback) +{ $obj = new stdClass; $obj->fallback = $fallback; @@ -100,9 +100,8 @@ function MakeAttachment($pretext, $text, $color, $fields, $fallback){ $obj->pretext = $pretext; $obj->color = $color; - if(sizeof($fields)>0) + if (sizeof($fields) > 0) $obj->fields = $fields; return $obj; } -?> diff --git a/scripts/meme.php b/scripts/meme.php index 4c68a81..a8ce9f6 100644 --- a/scripts/meme.php +++ b/scripts/meme.php @@ -1,4 +1,4 @@ -Text; $memetext = str_replace("memebot ", "", $cmdText); @@ -32,22 +31,19 @@ $bottom = urlencode($parts[2]); $meme = CreateNewMeme($gen, $top, $bottom); -mylog('sent.txt',$meme); +mylog('sent.txt', $meme); $response = slack_incoming_hook_post($config['slack']['hook'], $cmd->UserName, $cmd->ChannelName, null, ":bow:", $meme); -mylog('sent.txt',$response); +mylog('sent.txt', $response); //str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) //print_r($cmd->Text);die; - - //$out = new stdClass(); //$out->text = $meme; //$json = json_encode($out); //mylog('sent.txt',$json); //print_r($json); -?> diff --git a/scripts/rallyme.php b/scripts/rallyme.php index aca567c..f3047ff 100644 --- a/scripts/rallyme.php +++ b/scripts/rallyme.php @@ -1,4 +1,4 @@ -Text); $result = HandleItem($slackCommand, $rallyFormattedId); -?> \ No newline at end of file diff --git a/scripts/xkcd.php b/scripts/xkcd.php index 971667b..f1780f2 100644 --- a/scripts/xkcd.php +++ b/scripts/xkcd.php @@ -1,4 +1,4 @@ -\n"; $ret = slack_incoming_hook_post($hook, "xkcdbot", $command->ChannelName, $iconurl, $emoji, $payload); -if($ret!="ok") +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