' .'' .'' .'' .'' .'' .'Add to Slack', self::slack_authorise_url, http_build_query($this->parameters()) ); } public function home() { return sprintf('Hi, for instructions on how to install me, please reach out to %s.',config('slack.app_admin','Your slack admin')); } public function setup() { return Redirect::to(self::slack_authorise_url.'?'.http_build_query($this->parameters())); } /** * Install this Slack Application. * * @param Request $request * @return string * @throws \GuzzleHttp\Exception\GuzzleException */ public function install(Request $request,bool $oauth=FALSE) { if (! config('slack.client_id') OR ! config('slack.client_secret')) abort(403,'Slack ClientID or Secret not set'); $client = new Client; $response = $client->request('POST',self::slack_oauth_url,[ 'auth'=>[config('slack.client_id'),config('slack.client_secret')], 'form_params'=>[ 'code'=>$request->get('code'), 'redirect_url'=>$request->url(), ], ]); if ($response->getStatusCode() != 200) abort(403,'Something didnt work, status code not 200'); $output = json_decode($response->getBody()); if (App::environment() == 'local') file_put_contents('/tmp/install',print_r($output,TRUE)); if (! $output->ok) abort(403,'Something didnt work, status not OK ['.(string)$response->getBody().']'); // Are we an enterprise? $eo = NULL; if ($output->enterprise) { $eo = Enterprise::firstOrNew( [ 'enterprise_id'=>$output->enterprise->id ]); $eo->name = $output->enterprise->name; $eo->active = TRUE; $eo->save(); } // Store our team details $so = Team::firstOrNew( [ 'team_id'=>$output->team->id ]); // We just installed, so we'll make it active, even if it already exists. $so->description = $output->team->name; $so->active = 1; $so->enterprise_id = $eo ? $eo->id : NULL; $so->save(); dispatch((new TeamUpdate($so))->onQueue('slack')); // Store our app token $to = $so->token; if (! $to) { $to = new Token; $to->description = 'App: Oauth'; } $to->active = 1; $to->token = $output->access_token; $to->scope = $output->scope; $so->token()->save($to); Log::debug(sprintf('%s:TOKEN Created [%s]',self::LOGKEY,$to->id),['m'=>__METHOD__]); // Create the bot user // Store the user who install, and make them admin $bo = User::firstOrNew( [ 'user_id'=>$output->bot_user_id, ]); $bo->enterprise_id = $eo ? $eo->id : NULL; $bo->team_id = $so->id; $bo->active = 0; $bo->admin = 0; $bo->save(); $so->bot_id = $bo->id; $so->save(); Log::debug(sprintf('%s:BOT Created [%s]',self::LOGKEY,$bo->id),['m'=>__METHOD__]); // Store the user who install, and make them admin $uo = User::firstOrNew( [ 'user_id'=>$output->authed_user->id, ]); $uo->enterprise_id = $eo ? $eo->id : NULL; $uo->team_id = $eo ? NULL : $so->id; $uo->active = 1; $uo->admin = 1; $uo->save(); Log::debug(sprintf('%s:ADMIN Created [%s]',self::LOGKEY,$uo->id),['m'=>__METHOD__]); // Update Slack Object with admin_id $so->admin_id = $uo->id; $so->save(); return $oauth ? $output : sprintf('All set up! Head back to your slack instance %s.',$so->description); } /** * Define our parameters to install this Slack Integration * * @note The configuration file should include a list of scopes that this application needs * @return array */ private function parameters(): array { return [ 'client_id' => config('slack.client_id'), 'scope' => join(',',config('slack.bot_scopes')), 'user_scope' => join(',',config('slack.user_scopes')), ]; } }