public static function respond() { ini_set("display_errors", 0); set_exception_handler(["Fight\\APIWrapper", "error_handler"]); register_shutdown_function(["Fight\\APIWrapper", "fatal_handler"]); $url = $_SERVER["REQUEST_URI"]; if (strrpos($url, "?")) { $url = substr($url, 0, strrpos($url, "?")); } $path = explode("/", $url); $path = $path[count($path) - 1]; $params = json_decode(file_get_contents("php://input"), TRUE) ?: []; $params = array_merge($_POST, $params); if ($params["text"] === null && $path !== "login") { throw new Exception("Text missing", 400); } $text = $params["text"]; if ($path === "fight") { $command = explode(" ", $text)[0]; if (Main::isMethod($command)) { $path = $command; $text = substr($text, strlen($command) + 1); } } if ($path === "login") { if ($params["email"] === null) { throw new Exception("Email is missing", 400); } $result = Main::login($params); } else { if ($params["user_id"] === null) { throw new Exception("You are not logged in!", 401); } if ($params["team_id"] === null) { throw new Exception("Team ID missing", 400); } if ($params["channel_id"] === null) { throw new Exception("Channel ID missing", 400); } $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'); $attachments = []; foreach ($result["data"] as $update) { $attachment = $update->toAttachment($result["user"]); if ($attachment) { $attachments[] = $attachment; } } echo json_encode($attachments); self::$complete = true; }
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 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)]); }