/
slack.php
74 lines (63 loc) · 1.86 KB
/
slack.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
<?
/*
* SlackWrapper class file
*
* @author Thomas Steinke
*/
namespace Fight;
use Fight\Main;
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 function respond() {
$params = $_POST;
$path = $params["trigger_word"];
$text = substr($params["text"], 6);
if ($path === "fight") {
$command = explode(" ", $text)[0];
if (Main::isMethod($command)) {
$path = $command;
$text = substr($text, strlen($command) + 1);
}
}
$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
$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)
]);
}
}
// 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] : '';
}