Skip to content

Instantly share code, notes, and snippets.

@Hais
Last active August 29, 2015 14:04
Show Gist options
  • Save Hais/9eb8905f2af857a04570 to your computer and use it in GitHub Desktop.
Save Hais/9eb8905f2af857a04570 to your computer and use it in GitHub Desktop.
SlackShat
<?php
/**
*
* To run:
*
* #1 change token
*
* #2 php -S localhost:8080 index.php
*
* #3 http://localhost:8080
*
*/
class User
{
public $id, $avatar, $name;
function __construct($id, $name, $avatar)
{
$this->id = $id;
$this->avatar = $avatar;
$this->name = $name;
}
}
class Channel
{
public $id, $name;
function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
class Bot
{
const token = " CHANGE ME ";
public function execute($method, $params = [])
{
$params['token'] = self::token;
$params['pretty'] = 1;
$paramsStr = http_build_query($params);
$url = 'https://slack.com/api/' . $method . '?' . $paramsStr;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
}
public function sendToSlack($msg, User $user, Channel $channel)
{
return $this->execute('chat.postMessage', [
'channel' => '#' . $channel->name,
'username' => $user->name,
'icon_url' => $user->avatar,
'text' => $msg
]);
}
public function getUsers()
{
static $users;
if (!$users) {
$users = [];
$obj = json_decode($this->execute('users.list'));
foreach ($obj->members as $member) {
$users[] = new User($member->id, $member->profile->real_name ?: $member->name, $member->profile->image_48);
}
}
return $users;
}
public function getCharacters()
{
return [
new User("batman", "Batman", "http://png-1.findicons.com/files/icons/1293/the_batman_vol_1/48/batman.png"),
new User("obama", "Barack Obama", "http://www.skagitdemocrats.org/wp-content/uploads/2012/08/BarackObama-48x48.jpg"),
new User("jenkins", "Jenkins", "https://slack.global.ssl.fastly.net/20653/img/services/jenkins-ci_48.png"),
new User("zoidberg", "Zoidberg", "http://www.pixeljoint.com/files/icons/avatar__r1447351251.png"),
];
}
public function getChannels()
{
static $channels;
if (!$channels) {
$obj = json_decode($this->execute('channels.list'));
$channels = [];
foreach ($obj->channels as $objChannel) {
$channels[$objChannel->id] = new Channel($objChannel->id, $objChannel->name);
}
}
return $channels;
}
public function getMergedUsers()
{
$users = [];
foreach ([$this->getUsers(), $this->getCharacters()] as $group) {
foreach ($group as $user) {
$users[$user->id] = $user;
}
}
return $users;
}
public function getChannelById($id)
{
return $this->getChannels()[$id];
}
public function getUserById($id)
{
return $this->getMergedUsers()[$id];
}
}
$bot = new Bot();
if ($_POST) {
echo $bot->sendToSlack($_POST['text'], $bot->getUserById($_POST['users']), $bot->getChannelById($_POST['channel']));
die;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function () {
$("#submit").click(function () {
$("#result").html("Loading...");
$.ajax({
type: "POST",
url: "/",
data: $("#form").serializeArray(),
success: function (data) {
$("#result").html(data);
},
dataType: "text"
});
});
});
</script>
<title>SlackShat</title>
</head>
<body>
<div class="container">
<h1>SlackShat</h1>
<form role="form" class="form-horizontal" id="form">
<div class="form-group">
<label for="exampleInputEmail1" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<select name="users" class="form-control">
<? foreach (['Characters' => $bot->getCharacters(), 'Users' => $bot->getUsers()] as $group => $users) : ?>
<optgroup label="<?= $group ?>" id="user" name="user">
<? foreach ($users as $user) : ?>
<option value="<?= $user->id ?>"><?= $user->name ?></option>
<? endforeach ?>
</optgroup>
<? endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Channel</label>
<div class="col-sm-10">
<select name="channel" id="password" class="form-control">
<? foreach ($bot->getChannels() as $channel) : ?>
<option
value="<?= $channel->id ?>" <?= $channel->name == 'general' ? 'selected="true"' : '' ?>><?= $channel->name ?></option>
<? endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<label for="text" class="col-sm-2 control-label">Text</label>
<div class="col-sm-10">
<textarea class="form-control" id="text" name="text" rows="3"></textarea>
</div>
</div>
</form>
<div class="form-group">
<label for="text" class="col-sm-2 control-label">&nbsp;</label>
<div class="col-sm-10">
<button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg" id="submit">
Submit
</button>
</div>
</div>
</div>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true"
id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
class="sr-only">Close</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<pre style="height: 400px" id="result">
</pre>
</div>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment