-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.php
66 lines (59 loc) · 1.98 KB
/
index.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
<?php
use w\Bot\controllers\BaseController;
use w\Bot\FootballState;
use w\Bot\structures\GameStateResponse;
use w\Bot\structures\SlackResponse;
include_once __DIR__ . '/init.php';
$footballState = new FootballState((int)getenv('APP_BOT_PLAYERS_NEEDED'));
$client = JoliCode\Slack\ClientFactory::create(getenv('APP_BOT_OAUTH_TOKEN'));
$payload = null;
$input = file_get_contents('php://input');
$payload = json_decode($input, true);
if (is_null($payload)) {
$payload = json_decode($_POST['payload'], true);
}
if (empty($payload)) {
$payload = [];
}
$controller = BaseController::getController();
/** @var BaseController $controllerInstance */
$controllerInstance = new $controller($client, $footballState->db, $footballState, $payload);
try {
$response = $controllerInstance->process();
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
die;
}
// error_log(print_r($response, true));
if (!is_null($response)) {
// Action Responses
if (is_string($response)) {
header("Content-Type: text/plain");
echo $response;
return;
} elseif ($response instanceof GameStateResponse) {
header('Content-type: application/json');
$messageBuilder = $footballState->getGameStateResponse($response->channel);
if (is_null($messageBuilder)) {
return;
}
echo json_encode($messageBuilder, JSON_PRETTY_PRINT);
// Event responses
} elseif ($response instanceof SlackResponse) {
$client->chatPostMessage(
[
'channel' => $response->channel,
'text' => $response->message,
'mrkdwn' => true,
]
);
} elseif (is_array($response)) {
if (!isset($response['channel'])) {
$response['channel'] = getenv('APP_BOT_CHANNEL');
}
if (isset($response['attachments'])) {
$response['attachments'] = json_encode($response['attachments']);
}
$client->chatPostMessage($response);
}
}