public static function getUserInfo($team_id, $user_id)
 {
     $app = FightAppModel::findById($team_id);
     if (!$app) {
         throw new Exception("App not installed!");
     }
     $ch = curl_init("https://slack.com/api/users.info");
     curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => ["token" => $app->api_token, "user" => $user_id]]);
     $info = json_decode(curl_exec($ch), true);
     return $info["user"]["profile"];
 }
Example #2
0
 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;
     }
 }