From c621d36e8a13dc9c25e896dac3d67d66fec522e8 Mon Sep 17 00:00:00 2001 From: Alexander Rodriguez Date: Sat, 25 Feb 2017 13:02:29 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 2 ++ README.md | 6 ++++ config.ini.txt | 7 +++++ index.php | 60 +++++++++++++++++++++++++++++++++++++++ src/Slack.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+) create mode 100644 .gitignore create mode 100644 config.ini.txt create mode 100644 index.php create mode 100644 src/Slack.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4aa302 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.ini +.php_cs.cache \ No newline at end of file diff --git a/README.md b/README.md index d6e595c..a0270a0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # rafflebot-slack Bot for Slack, for Select active user in your team. + +You need, a API Key of your team, for get all members, and slash command integration. + +After that, you need to rename config.ini.txt, to config.ini, replace these values with your new values. + +Documentation is on WIP... \ No newline at end of file diff --git a/config.ini.txt b/config.ini.txt new file mode 100644 index 0000000..28ef267 --- /dev/null +++ b/config.ini.txt @@ -0,0 +1,7 @@ +SLACK_TOKEN = "YOUR-SLACK-TOKEN-HERE" +COMMAND_TOKEN = "YOUR-SLACK-SLASH-TOKEN" +COMMAND_NAME[] = "/yourcommand" +; Use {user} for replace with name of selected user of your team. +; Use {text} for add the parameter passed to the slash command. +; You can use Markdown formatting. +TEXT = "*Sorteo* de `{text}`, el seleccionado fue: {user}" \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..32b9765 --- /dev/null +++ b/index.php @@ -0,0 +1,60 @@ +call('users.list', ['presence' => true]); + + +if (isset($data['members'])) { + $users=[]; + foreach ($data['members'] as $member) { + if (isset($member['presence']) && $member['presence'] == "active") { + $users[] = $member; + } + } + $index = array_rand($users); + $user_id = $users[$index]['id']; + $username = $users[$index]['name']; +} else { + die(json_encode([ + 'ok' => false, + 'error' => 'Failed Connection to Slack', + 'response' => $data + ])); +} + + +$request = $_POST; + +if (!empty($request['token']) && $request['token'] == $config['COMMAND_TOKEN']) { + $command = (!empty($request['command'])) ? $request['command'] : ''; + $text = (!empty($request['text'])) ? $request['text'] : ''; + $username = (!empty($request['user_name'])) ? $request['user_name'] : ''; + + switch ($command) { + case $config['COMMAND_NAME'][0]: + $response_text = str_replace('{text}', $text, $config['TEXT']); + $response_text = str_replace('{user}', "<@$user_id|$username>", $response_text); + $response = [ + 'response_type' => 'in_channel', + 'text' => $response_text, + 'username' => "markdownbot", + 'mrkdwn' => true + + ]; + $response = json_encode($response); + echo $response; + + break; + + } +} else { + die(json_encode([ + 'ok' =>false, + 'error' => 'Close Connection, is\'nt a post request' + ])); +} diff --git a/src/Slack.php b/src/Slack.php new file mode 100644 index 0000000..6e94a8b --- /dev/null +++ b/src/Slack.php @@ -0,0 +1,76 @@ + + * @version 1.0.0 + */ +class Slack +{ + private $api_token; + private $api_endpoint = 'https://slack.com/api/'; + + /** + * Create a new instance + * @param string $api_token Your Slack api bearer token + */ + public function __construct($api_token) + { + $this->api_token = $api_token; + } + + /** + * Calls an API method. You don't have to pass in the token, it will automatically be included. + * @param string $method The API method to call. + * @param array $args An associative array of arguments to pass to the API. + * @param integer $timeout Set maximum time the request is allowed to take, in seconds. + * @return array The response as an associative array, JSON-decoded. + */ + public function call($method, $args = array(), $timeout = 10) + { + return $this->request($method, $args, $timeout); + } + + /** + * Performs the underlying HTTP request. + * @param string $method The API method to call. + * @param array $args An associative array of arguments to pass to the API. + * @param integer $timeout Set maximum time the request is allowed to take, in seconds. + * @return array The response as an associative array, JSON-decoded. + */ + private function request($method, $args = array(), $timeout = 10) + { + $url = str_replace('', $method, $this->api_endpoint); + $args['token'] = $this->api_token; + + if (function_exists('curl_version')) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POSTFIELDS, $args); + $result = curl_exec($ch); + curl_close($ch); + } else { + $post_data = http_build_query($args); + $result = file_get_contents($url, false, stream_context_create(array( + 'http' => array( + 'protocol_version' => 1.1, + 'method' => 'POST', + 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . + "Content-length: " . strlen($post_data) . "\r\n" . + "Connection: close\r\n", + 'content' => $post_data + ), + ))); + } + + return $result ? json_decode($result, true) : false; + } +}