-
Notifications
You must be signed in to change notification settings - Fork 0
/
bump_into_colleagues.php
155 lines (136 loc) · 4.27 KB
/
bump_into_colleagues.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
#!/usr/bin/php
date_default_timezone_set('Europe/Stockholm');
# INSTALLATION:
# - Get a token from Slack, paste it into the config below
# - Setup a cron job on your server to run this script every minute
# TODO (OPTIONAL - PLEASE MAKE PULL REQUEST IF YOU DO!):
# - Create pagination if more users than 200
class SlackRoulette{
function __construct(){
# config
$this->env = 'dev'; # Beware! "prod" creates real Slack messages...
$this->token = 'xxx'; # Create a Slack App and get the token manually
$this->teamname = 'xxx'; # Can be any hyphenated simple-text-string-that-looks-like-this-but-shorter
$this->group_size = 3;
$this->run_day = "Tuesday";
$this->run_time = "09:01";
# don't touch
$this->teams = [];
$this->users = [];
$this->groups = [];
}
function run(){
if(date('l') == $this->run_day && date('H:i') == $this->run_time){
$this->log('Starting run...');
$this->users = [];
$this->groups = [];
$this->get_users();
$this->partition_users();
$this->post_messages();
}else{
$this->log('Not set to run this time...');
}
}
function get_users(){
$all_users = json_decode(file_get_contents('https://slack.com/api/users.list?token='.$this->token));
if($all_users->ok =='true'){
foreach($all_users->members as $item){
$skip = false;
if($item->deleted == 'true'){
$skip = true;
}
if(isset($item->is_restricted) && $item->is_restricted == 'true'){
$skip = true;
}
if(isset($item->is_ultra_restricted) && $item->is_ultra_restricted == 'true'){
$skip = true;
}
if($item->is_bot == 'true'){
$skip = true;
}
if($item->is_app_user == 'true'){
$skip = true;
}
if(isset($item->is_invited_user) && $item->is_invited_user == 'true'){
$skip = true;
}
if($item->name == 'slackbot'){
$skip = true;
}
if(!$skip){
$status = json_decode(file_get_contents('https://slack.com/api/users.getPresence?token='.$this->token.'&user='.$item->id));
if($status->presence == 'active'){
$this->users[] = $item->id;
}
}
}
}
$this->log('Found '.count($this->users).' users');
}
function partition_users(){
shuffle($this->users);
$this->groups = array_chunk($this->users, $this->group_size);
$this->log('Made '.count($this->groups).' groups');
}
function post_messages(){
if($this->groups){
foreach($this->groups as $id=>$group){
if(count($group) === $this->group_size){
foreach($group as $user){
$str = "Hey!\n";
$str .= "Some people are hanging out on this call below.\n";
$str .= "You have about 10 minutes to get to know them better. Use the time wisely. Show your true colors.\n";
$str .= "Here are a few topics to talk about:\n";
$str .= $this->random_subjects();
$str .= "http://g.co/meet/bic-".$this->teamname."-w".date('W')."-".$id;
$data['token'] = $this->token;
$data['channel'] = $user;
$data['text'] = $str;
$data['unfurl_links'] = 'true';
if($this->env == 'prod'){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://slack.com/api/chat.postMessage");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
}
$this->log($str);
}
}else{
$this->log('A group was too small, leaving '.count($group).' users unmatched ('.json_encode($group).')');
}
}
}
}
function random_subjects(){
$subjects = [
'The monarchy',
'Best movie ever',
'A song a hate that i love',
'My life in 10 years',
'My biggest dream',
'Art - overrated?',
'Where does music come from?',
'Someone who important in my teens',
'A secret of mine',
'The secret to creativity',
'The meaning of life',
'What is free will',
'I my spare time...',
'Dream vacation',
'When I grow up I will...',
'When I retire I will...',
'I would like to know how to...'
];
shuffle($subjects);
return "- ".$subjects[0]."\n"."- ".$subjects[1]."\n";
}
function log($message){
file_put_contents('./bump_into_colleagues_'.date("Ymd").'.log', date('H:i:s')." ".$message."\n", FILE_APPEND);
}
}
$sr = new SlackRoulette;
$sr->run();