|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Repositories\PostRepository; |
| 6 | +use GuzzleHttp\Exception\GuzzleException; |
| 7 | +use Illuminate\Http\Request; |
| 8 | + |
| 9 | +class SlackInvitationController extends Controller |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * Show the application dashboard. |
| 14 | + * |
| 15 | + * @return \Illuminate\Http\Response |
| 16 | + */ |
| 17 | + public function invite() |
| 18 | + { |
| 19 | + $result = $this-> request_invitation( '[email protected]'); |
| 20 | + |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + function request_invitation($email) |
| 25 | + { |
| 26 | + // users.admin.invite is undocumented API, keep in mind. |
| 27 | + $api_response = $this->call_api('/api/users.admin.invite?t=' . time(), [ |
| 28 | + 'email' => $email, |
| 29 | + 'set_active' => 'true', |
| 30 | + '_attempts' => '1', |
| 31 | + ]); |
| 32 | + dd($api_response); |
| 33 | + if ($api_response->ok !== true) { |
| 34 | + throw new \RuntimeException('Slack API response not ok, error:' . $api_response->error); // TODO more nice Exception |
| 35 | + } |
| 36 | + } |
| 37 | + function call_api($path, $params = []) |
| 38 | + { |
| 39 | + $client = new \GuzzleHttp\Client([ |
| 40 | + 'base_uri' => "https://slack.com/" |
| 41 | + ]); |
| 42 | + var_dump(env('SLACK_API')); |
| 43 | + var_dump(env('SLACK_TOKEN')); |
| 44 | + |
| 45 | + try { |
| 46 | + $api_response_raw = $client->request('post', $path, [ |
| 47 | + 'form_params' => array_merge(['token' => env('SLACK_API')], $params) |
| 48 | + ]); |
| 49 | + } catch (GuzzleException $e) { |
| 50 | + } |
| 51 | + $api_response = json_decode($api_response_raw->getBody()); |
| 52 | + if ($api_response === null) { |
| 53 | + throw new \RuntimeException('Slack API response an invalid json'); // TODO more nice Exception |
| 54 | + } |
| 55 | + return $api_response; |
| 56 | + } |
| 57 | +} |
0 commit comments