-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslack.php
More file actions
executable file
·151 lines (134 loc) · 5.28 KB
/
slack.php
File metadata and controls
executable file
·151 lines (134 loc) · 5.28 KB
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
<?php
require_once('slackToken.php');
class Slack {
public static function apiCall($endpoint, $data, $token = null) {
$data['token'] = $token == null ? SlackToken::$BOT_TOKEN : $token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// $output contains the output string
$output = curl_exec($ch);
curl_close($ch);
$json = json_decode($output, true);
return $json;
}
public static function messageChannel($channel, $text) {
$data = array(
'channel' => $channel,
'text' => $text,
'username' => 'widespread_nazareth',
'as_user' => 'true'
);
return self::apiCall("https://slack.com/api/chat.postMessage", $data);
}
public static function getRandomWordString($nWords) {
$f = fopen("test.log", "r") or die("Cannot open");
$file = fread($f, filesize("test.log"));
$f_arr = explode(" ", $file);
shuffle($f_arr);
$c = 0;
$msg = "";
foreach($f_arr as $word) {
if ($c >= $nWords) {
break;
$c = 0;
$msg = "";
}
$msg .= $word . " ";
$c += 1;
}
fclose($f);
return $msg;
}
public static function getChannelMessages($channelID) {
$data = array(
'channel' => $channelID,
'limit' => 100
);
$resp = null;
$messages = array();
while ($resp == null or ($resp["response_metadata"] and $resp["response_metadata"]["next_cursor"])) {
if ($resp != null) {
$data["cursor"] = $resp["response_metadata"]["next_cursor"];
}
$resp = self::apiCall("https://slack.com/api/conversations.history", $data, $token=SlackToken::$USER_TOKEN);
$messages = array_merge($messages, $resp["messages"]);
}
return $messages;
}
public static function getChannels() {
// both private and public channels
$data = array('exclude_archived' => true);
$ret = self::apiCall("https://slack.com/api/channels.list", $data)["channels"];
return array_merge($ret, self::apiCall("https://slack.com/api/groups.list", $data)["groups"]);
}
public static function channelInvite($channelID, $userID) {
$data = array(
'channel' => $channelID,
'user' => $userID,
);
return self::apiCall("https://slack.com/api/channels.invite", $data, $token=SlackToken::$USER_TOKEN);
}
public static function groupInvite($channelID, $userID) {
$data = array(
'channel' => $channelID,
'user' => $userID,
);
return self::apiCall("https://slack.com/api/groups.invite", $data, $token=SlackToken::$USER_TOKEN);
}
public static function getUsers() {
return self::apiCall("https://slack.com/api/users.list", array());
}
public static function addMsgReaction($channelID, $msgTimestamp, $reactName) {
$data = array(
'channel' => $channelID,
'name' => $reactName,
'timestamp' => $msgTimestamp
);
return self::apiCall("https://slack.com/api/reactions.add", $data);
}
}
if ($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET["react"]) and $_GET["react"] == "nice")
{
$targetRealName = "benelliott";
$targetRealChannel = "botextensive";
$benBotUserID = "loopdloop";
$targetUser = null;
foreach (Slack::getUsers()["members"] as $user) {
if ($user["name"] == $targetRealName) {
$targetUser = $user["id"];
echo $targetUser . " " . $user["name"] . "\n<br>";
}
if ($user["name"] == $benBotUserID) {
$benBotUserID = $user["id"];
}
if ($user["name"] == "widespread_nazareth") {
echo "widespread_nazareth " . $user["id"] . "\n<br>";
}
}
foreach (Slack::getChannels() as $channel) {
print_r($channel["name"]); echo "\n<br>";
if ($channel["name"] == $targetRealChannel) {
echo $channel["name"] . "\n<br>";
echo $channel["id"] . "\n<br>";
echo $benBotUserID . "\n<br>";
print_r(Slack::groupInvite($channel["id"], $benBotUserID));
foreach (Slack::getChannelMessages($channel["id"]) as $message) {
if ($message["user"] == $targetUser) {
// Slack::addMsgReaction($channel["id"], $message["ts"], "banana");
// Slack::addMsgReaction($channel["id"], $message["ts"], "nash");
// Slack::addMsgReaction($channel["id"], $message["ts"], "kishan");
// Slack::addMsgReaction($channel["id"], $message["ts"], "kissing_heart");
// Slack::addMsgReaction($channel["id"], $message["ts"], "eggplant");
// Slack::addMsgReaction($channel["id"], $message["ts"], "peach");
// Slack::addMsgReaction($channel["id"], $message["ts"], "heart");
// print_r($message);
}
}
}
}
}
?>