/
SlackWrapper.php
204 lines (165 loc) · 5.91 KB
/
SlackWrapper.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?
/*
* SlackWrapper class file
*
* @author Thomas Steinke
*/
namespace Fight;
require_once __DIR__ . "/config.php";
use Fight\Main;
use Fight\Model\FightAppModel;
class SlackWrapper
{
// Example payload from Slack
/*
{
"token":"zuE4F5lLtrHkL7DKbpiwFH4m",
"team_id":"T0B2LSLP6",
"team_domain":"team-pc",
"service_id":"12884001970",
"channel_id":"C0CS03RK4",
"channel_name":"testslack",
"timestamp":"1445368606.000013",
"user_id":"U0B2QPTNU",
"user_name":"thomassteinke",
"text":"fight dude",
"trigger_word":"fight"
}
*/
public static $complete = false;
public static function addApp() {
$request = curl_init("https://slack.com/api/oauth.access");
$params = [
"client_id" => "11088904788.13274729057",
"client_secret" => "b8d1c5e5875ea444adcbc595c52ee26c",
"code" => $_GET["code"]
];
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $params);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($request), true);
if (!$response["ok"]) {
$status_header = 'HTTP/1.1 200 OK';
header($status_header);
header('Content-Type: text/html');
echo "App could not be added. Sorry!";
return;
}
$app = FightAppModel::build([
"team" => $response["team_id"],
"api_token" => $response["access_token"],
"channel" => $response["incoming_webhook"]["channel"]
]);
$status_header = 'HTTP/1.1 200 OK';
header($status_header);
header('Content-Type: text/html');
$app->save();
echo "App added!<hr>" .
"Please refer to <a href='http://thomassteinke.com/api/fight#add-outgoing'>the previous page</a> to complete installation.";
}
public static function respond() {
$params = $_POST;
$path = $params["trigger_word"];
$text = substr($params["text"], strlen($path) + 1);
if ($path === "fight") {
$command = explode(" ", $text)[0];
if (Main::isMethod($command)) {
$path = $command;
$text = substr($text, strlen($command) + 1);
}
}
ini_set("display_errors", 0);
set_exception_handler(["Fight\SlackWrapper", "error_handler"]);
register_shutdown_function(["Fight\SlackWrapper", "fatal_handler"]);
$result = Main::main($path, [
"text" => $text,
"user_id" => $params["user_id"],
"user_name" => $params["user_name"],
"team_id" => $params["team_id"],
"channel_id" => $params["channel_id"]
]);
$status_header = 'HTTP/1.1 ' . $result["status"] . ' ' . getStatusCodeMessage($result["status"]);
header($status_header);
header('Content-Type: application/json');
// TODO Send attachments to Slack if token is available
$app = FightAppModel::findById($params["team_id"]);
if ($app) {
$attachments = [];
foreach ($result["data"] as $update) {
$attachment = $update->toAttachment($result["user"]);
if ($attachment) $attachments[] = $attachment;
}
$ch = curl_init("https://slack.com/api/chat.postMessage");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
"token" => $app->api_token,
"channel" => $params["channel_id"],
"text" => "",
"username" => "Fight Club",
"attachments" => json_encode($attachments)
]
]);
$result = curl_exec($ch);
if (!$result) {
// Add a space so we don't trigger ourselves
echo json_encode([
"text" => " " . implode("\n", $attachments)
]);
}
self::$complete = true;
}
else {
$attachments = [];
foreach ($result["data"] as $update) {
$attachments[] = $update->toString();
}
// Add a space so we don't trigger ourselves
echo json_encode([
"text" => " " . implode("\n", $attachments)
]);
self::$complete = true;
}
}
public static function error_handler($e) {
header('HTTP/1.1 200 OK');
header('Content-Type: application/json');
echo json_encode([
"text" => $e->getMessage()
]);
self::$complete = true;
die();
}
public static function fatal_handler() {
if (self::$complete) {
return;
}
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
if( $error !== NULL) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
header('HTTP/1.1 200 OK');
header('Content-Type: application/json');
$message = "There was an error in a FightYourFriends call!<br><br>";
$message .= "Error: " . $errstr . " at " . $errfile . ":" . $errline . "<br><br>";
$message .= "Other info:<br>";
$message .= "$_POST: " . json_encode($_POST) . "<br>";
$sent = mail("exyphnos@gmail.com", "ERROR in FightYourFriends!", $message);
echo json_encode([
"text" => "ERROR: " . $errstr . " at " . $errfile . ":" . $errline . ". Email sent to FYF admin."
]);
}
}
}
// From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
$codes = parse_ini_file(__DIR__ . "/codes.ini");
function getStatusCodeMessage($status) {
return (isset($codes[$status])) ? $codes[$status] : '';
}