Skip to content

Commit

Permalink
Merge pull request #178 from ndijkstra/slack-notifications
Browse files Browse the repository at this point in the history
Add Slack as Notification channel
  • Loading branch information
Cannonb4ll authored Oct 19, 2022
2 parents c04320a + 13f2338 commit d7d5236
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
24 changes: 13 additions & 11 deletions app/Filament/Pages/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function getFormSchema(): array
->helperText('These boards will automatically be prefilled when you create a project.')
->columnSpan(2),
])
->visible(fn($get) => $get('create_default_boards')),
->visible(fn ($get) => $get('create_default_boards')),

Toggle::make('show_projects_sidebar_without_boards')->label('Show projects in sidebar without boards')
->helperText('If you don\'t want to show projects without boards in the sidebar, toggle this off.')
Expand All @@ -96,7 +96,7 @@ protected function getFormSchema(): array

Toggle::make('project_required_when_creating_item')
->label('Project is required when creating an item')
->hidden(fn(Closure $get) => $get('select_project_when_creating_item') === false)
->hidden(fn (Closure $get) => $get('select_project_when_creating_item') === false)
->columnSpan(2),

Toggle::make('select_board_when_creating_item')
Expand All @@ -106,7 +106,7 @@ protected function getFormSchema(): array

Toggle::make('board_required_when_creating_item')
->label('Board is required when creating an item')
->hidden(fn(Closure $get) => $get('select_board_when_creating_item') === false)
->hidden(fn (Closure $get) => $get('select_board_when_creating_item') === false)
->columnSpan(2),

Toggle::make('users_must_verify_email')
Expand Down Expand Up @@ -149,10 +149,10 @@ protected function getFormSchema(): array
])->default(1),
Toggle::make('must_have_project')
->reactive()
->visible(fn($get) => $get('type') === 'recent-items')
->visible(fn ($get) => $get('type') === 'recent-items')
->helperText('Enable this to show items that have a project'),
Toggle::make('must_have_board')
->visible(fn($get) => $get('must_have_project') && $get('type') === 'recent-items')
->visible(fn ($get) => $get('must_have_project') && $get('type') === 'recent-items')
->helperText('Enable this to show items that have a board'),
])->helperText('Determine which items you want to show on the dashboard (for all users).'),
]),
Expand All @@ -165,11 +165,11 @@ protected function getFormSchema(): array
->columnSpan(2),
Toggle::make('show_changelog_author')
->label('Show the author of the changelog.')
->visible(fn($get) => $get('enable_changelog'))
->visible(fn ($get) => $get('enable_changelog'))
->columnSpan(2),
Toggle::make('show_changelog_related_items')
->label('Show the related items on the changelog.')
->visible(fn($get) => $get('enable_changelog'))
->visible(fn ($get) => $get('enable_changelog'))
->columnSpan(2),
]),

Expand All @@ -183,24 +183,26 @@ protected function getFormSchema(): array
->reactive()
->options([
'email' => 'E-mail',
'discord' => 'Discord'
'discord' => 'Discord',
'slack' => 'Slack'
]),
TextInput::make('name')->label(function ($get) {
return match ($get('type')) {
'email' => 'Name receiver',
'discord' => 'Label'
'discord', 'slack' => 'Label'
};
})->required(),
TextInput::make('webhook')
->label(function ($get) {
return match ($get('type')) {
'email' => 'E-mail',
'discord' => 'Discord webhook URL'
'discord' => 'Discord webhook URL',
'slack' => 'Slack webhook URL'
};
})
->required()
->url(function ($get) {
return $get('type') === 'discord';
return $get('type') === 'discord' || $get('type') === 'slack';
})
->email(function ($get) {
return $get('type') === 'email';
Expand Down
22 changes: 21 additions & 1 deletion app/Observers/ItemObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Models\Item;
use App\Models\User;
use App\Enums\ItemActivity;
use App\Services\WebhookClient;
use App\Settings\ColorSettings;
use App\Settings\GeneralSettings;
use Illuminate\Support\Facades\Storage;
use App\Mail\Admin\ItemHasBeenCreatedEmail;
Expand All @@ -25,7 +27,7 @@ public function created(Item $item)

match ($receiver['type']) {
'email' => Mail::to($receiver['webhook'])->send(new ItemHasBeenCreatedEmail($receiver, $item)),
'discord' => (new \App\Services\Discord($receiver['webhook']))->send('POST', [
'discord' => (new WebhookClient($receiver['webhook']))->send('POST', [
'username' => config('app.name'),
'avatar_url' => asset('storage/favicon.png'),
'embeds' => [
Expand All @@ -41,6 +43,24 @@ public function created(Item $item)
'color' => '2278750',
],
],
]),
'slack' => (new WebhookClient($receiver['webhook']))->send('POST', [
'username' => config('app.name'),
'icon_url' => asset('storage/favicon.png'),
'attachments' => [
[
'fallback' => 'A new roadmap item has been created: <' . route('items.show', $item) . '|' . $item->title . '>',
'pretext' => 'A new roadmap item has been created: <' . route('items.show', $item) . '|' . $item->title . '>',
'color' => app(ColorSettings::class)->primary ?? '#2278750',
'fields' => [
[
'title' => $item->title,
'value' => str($item->content)->limit(50),
'shorts' => false,
]
],
],
],
])
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class Discord
class WebhookClient
{
protected PendingRequest $client;

Expand Down

0 comments on commit d7d5236

Please sign in to comment.