-
composer.json
Open in GitHub{ "require": { "php": "^7.4", "barryvdh/laravel-ide-helper": "^2.1", // ... "guzzlehttp/guzzle": "^7.0.1", }, }
-
config/app.php
Open in GitHub// Here we register a Slack Service Provider to be loaded return [ // ... other values 'providers' => [ Illuminate\Auth\AuthServiceProvider::class, // ... App\Services\GitHub\GitHubServiceProvider::class, App\Services\Slack\SlackServiceProvider::class, ], ];
-
app/Services/Slack/SlackServiceProvider.php
Open in GitHub// Here we create an object of the Slack class and assign it to the initiated Guzzle Client object use GuzzleHttp\Client; class SlackServiceProvider extends ServiceProvider { public function register() { $appToken = config('services.slack.app_token'); // You can see the definition of Slack::class below $this->app->singleton(Slack::class, function () use ($appToken) { $client = new Client([ 'base_uri' => 'https://slack.com/api', 'headers' => [ 'Authorization' => "Bearer {$appToken}", ], ]); return new Slack($client); }); } }
-
config/services.php
Open in GitHubreturn [ /* |-------------------------------------------------------------------------- | Third Party Services |-------------------------------------------------------------------------- | 'mailgun' => [ // ... ], // ... 'slack' => [ 'app_token' => env('SLACK_APP_TOKEN'), ], ];
-
app/Services/Slack/Slack.php
Open in GitHub// Here's where we create the Slack methods we want // And we interact with Slack API via Guzzle Client, which is initiated via __construct() // So in all the class, we can call it $this->client // Remember: that Guzzle class is initiated already with the credentials // More information on this Slack API method: https://api.slack.com/methods/users.list use GuzzleHttp\Client; use Illuminate\Support\Collection; class Slack { protected $client; public function __construct(Client $client) { $this->client = $client; } public function getMembers(array $memberNames): Collection { $response = $this->client->get('/api/users.list'); $response = json_decode((string) $response->getBody(), true); return collect($response['members']) ->filter(function (array $member) use ($memberNames) { return in_array($member['name'], $memberNames); }) ->map(function (array $memberProperties) { return new Member($memberProperties); }); } }
-
app/Services/Slack/Member.php
Open in GitHub// Specific class describing Slack member and converting their emojis class Member { public $name; public $statusEmoji; public function __construct(array $memberProperties) { $this->name = $memberProperties['name']; $this->statusEmoji = $this->convertToRealEmoji($memberProperties['profile']['status_emoji'] ?? ''); } protected function convertToRealEmoji(string $statusEmoji): string { if ($statusEmoji === ':house_with_garden:') { return '?'; } if ($statusEmoji === ':knife_fork_plate:') { return '?️'; } if ($statusEmoji === ':palm_tree') { return '?'; } if ($statusEmoji === ':spiral_calendar_pad:') { return '?'; } return ''; } }
-
app/Tiles/TeamMember/Commands/FetchSlackStatusCommand.php
Open in GitHub// This is where the Slack class is actually used - to get the members in this Artisan command // Notice the parameter for the constructor - it's the instance of Slack class, which is automatically resolved // by a service provider that we saw above use App\Services\Slack\Slack; use App\Services\Slack\Member; use Illuminate\Console\Command; use App\Tiles\TeamMember\TeamMemberStore; class FetchSlackStatusCommand extends Command { protected $signature = 'dashboard:fetch-team-member-status'; protected $description = 'Determine team member statuses'; protected $slackMembers = [ 'seb', 'adriaan', 'freek', 'willem', 'alex', 'ruben', 'rias', 'brent', 'jef', 'wouter', ]; public function handle(Slack $slack) { $this->info('Fetching team member statuses from Slack...'); $slack ->getMembers($this->slackMembers) ->each(function (Member $member) { TeamMemberStore::find(strtolower($member->name))->setStatusEmoji($member->statusEmoji); }); $this->info('All done!'); } }