Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexR1712 committed Feb 25, 2017
1 parent a7ae880 commit c621d36
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.ini
.php_cs.cache
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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...
7 changes: 7 additions & 0 deletions config.ini.txt
Original file line number Diff line number Diff line change
@@ -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}"
60 changes: 60 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

header('Content-Type: application/json');

include 'src/Slack.php';
$config = parse_ini_file("config.ini");
$slack = new Slack($config['SLACK_TOKEN']);

$data = $slack->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'
]));
}
76 changes: 76 additions & 0 deletions src/Slack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Simple abstraction of Slack API
*
* Uses curl, if not falls back to file_get_contents and HTTP stream.
*
* For all api methods, refer to https://api.slack.com/
*
* @author Yong Zhen <[email protected]>
* @version 1.0.0
*/
class Slack
{
private $api_token;
private $api_endpoint = 'https://slack.com/api/<method>';

/**
* 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>', $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;
}
}

0 comments on commit c621d36

Please sign in to comment.