wip
This commit is contained in:
25
app/Domain/Slack/Channels/SlackApiChannel.php
Normal file
25
app/Domain/Slack/Channels/SlackApiChannel.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack\Channels;
|
||||
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class SlackApiChannel
|
||||
{
|
||||
public function send($notifiable, Notification $notification): Response
|
||||
{
|
||||
$baseUrl = config("services.slack.url");
|
||||
$url = "{$baseUrl}/chat.postMessage";
|
||||
$channel = $notifiable->routeNotificationFor('slack', $notification);
|
||||
|
||||
return Http::withToken(config("services.slack.client_token"))
|
||||
->post($url, [
|
||||
"channel" => $channel,
|
||||
"text" => $notification->toSlack(),
|
||||
]);
|
||||
}
|
||||
}
|
51
app/Domain/Slack/Handlers/CatchAll.php
Normal file
51
app/Domain/Slack/Handlers/CatchAll.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\SlashCommand\Attachment;
|
||||
use Spatie\SlashCommand\AttachmentField;
|
||||
use Spatie\SlashCommand\Handlers\CatchAll as BaseCatchAllHandler;
|
||||
use Spatie\SlashCommand\Handlers\SignatureHandler;
|
||||
use Spatie\SlashCommand\Request;
|
||||
use Spatie\SlashCommand\Response;
|
||||
|
||||
class CatchAll extends BaseCatchAllHandler
|
||||
{
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$response = $this->respondToSlack("Nie rozpoznaję tej komendy: `/{$request->command} {$request->text}`");
|
||||
|
||||
[$command] = explode(' ', $this->request->text ?? "");
|
||||
|
||||
$alternativeHandlers = $this->findAlternativeHandlers($command);
|
||||
|
||||
if ($alternativeHandlers->count()) {
|
||||
$response->withAttachment($this->getCommandListAttachment($alternativeHandlers));
|
||||
}
|
||||
|
||||
if ($this->containsHelpHandler($alternativeHandlers)) {
|
||||
$response->withAttachment(Attachment::create()
|
||||
->setText("Aby wyświetlić wszystkie komendy, napisz: `/toby pomoc`")
|
||||
);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function getCommandListAttachment(Collection $handlers): Attachment
|
||||
{
|
||||
$attachmentFields = $handlers
|
||||
->map(function (SignatureHandler $handler) {
|
||||
return AttachmentField::create($handler->getFullCommand(), $handler->getDescription());
|
||||
})
|
||||
->all();
|
||||
|
||||
return Attachment::create()
|
||||
->setColor('warning')
|
||||
->setTitle('Czy miałeś na myśli:')
|
||||
->setFields($attachmentFields);
|
||||
}
|
||||
}
|
74
app/Domain/Slack/Handlers/DailySummary.php
Normal file
74
app/Domain/Slack/Handlers/DailySummary.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\SlashCommand\Attachment;
|
||||
use Spatie\SlashCommand\Request;
|
||||
use Spatie\SlashCommand\Response;
|
||||
use Spatie\SlashCommand\Handlers\SignatureHandler;
|
||||
use Toby\Domain\Enums\VacationType;
|
||||
use Toby\Domain\VacationTypeConfigRetriever;
|
||||
use Toby\Eloquent\Models\User;
|
||||
use Toby\Eloquent\Models\Vacation;
|
||||
|
||||
class DailySummary extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby dzisiaj";
|
||||
|
||||
protected $description = "Podsumowanie";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$configRetriever = app(VacationTypeConfigRetriever::class);
|
||||
|
||||
$now = Carbon::today();
|
||||
|
||||
/** @var Collection $absences */
|
||||
$absences = Vacation::query()
|
||||
->with(["user", "vacationRequest"])
|
||||
->whereDate("date", $now)
|
||||
->approved()
|
||||
->whereTypes(VacationType::all()->filter(fn(VacationType $type) => $configRetriever->isVacation($type)))
|
||||
->get()
|
||||
->map(fn(Vacation $vacation) => $vacation->user->profile->full_name);
|
||||
|
||||
/** @var Collection $remoteDays */
|
||||
$remoteDays = Vacation::query()
|
||||
->with(["user", "vacationRequest"])
|
||||
->whereDate("date", $now)
|
||||
->approved()
|
||||
->whereTypes(VacationType::all()->filter(fn(VacationType $type) => !$configRetriever->isVacation($type)))
|
||||
->get()
|
||||
->map(fn(Vacation $vacation) => $vacation->user->profile->full_name);
|
||||
|
||||
$birthdays = User::query()
|
||||
->whereRelation("profile", "birthday", $now)
|
||||
->get()
|
||||
->map(fn(User $user) => $user->profile->full_name);
|
||||
|
||||
$absencesAttachment = Attachment::create()
|
||||
->setTitle("Nieobecności :palm_tree:")
|
||||
->setColor('#eab308')
|
||||
->setText($absences->isNotEmpty() ? $absences->implode("\n") : "Wszyscy dzisiaj pracują :muscle:");
|
||||
|
||||
$remoteAttachment = Attachment::create()
|
||||
->setTitle("Praca zdalna :house_with_garden:")
|
||||
->setColor('#d946ef')
|
||||
->setText($remoteDays->isNotEmpty() ? $remoteDays->implode("\n") : "Wszyscy dzisiaj są w biurze :boom:");
|
||||
|
||||
$birthdayAttachment = Attachment::create()
|
||||
->setTitle("Urodziny :birthday:")
|
||||
->setColor('#3C5F97')
|
||||
->setText($birthdays->isNotEmpty() ? $birthdays->implode("\n") : "Dzisiaj nikt nie ma urodzin :cry:");
|
||||
|
||||
return $this->respondToSlack("Podsumowanie dla dnia {$now->toDisplayString()}")
|
||||
->withAttachment($absencesAttachment)
|
||||
->withAttachment($remoteAttachment)
|
||||
->withAttachment($birthdayAttachment)
|
||||
->displayResponseToEveryoneOnChannel();
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack;
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\SlashCommand\Request;
|
||||
@@ -13,13 +13,13 @@ use Toby\Eloquent\Models\User;
|
||||
|
||||
class GiveKeysTo extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby klucze:dla {to}";
|
||||
protected $signature = "toby klucze:dla {użytkownik}";
|
||||
|
||||
protected $description = "Daj klucze użytkownikowi {to}";
|
||||
protected $description = "Daj klucze wskazanemu użytkownikowi";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$to = $this->getArgument('to');
|
||||
$to = $this->getArgument('użytkownik');
|
||||
|
||||
$id = Str::between($to, "@", "|");
|
||||
|
45
app/Domain/Slack/Handlers/Help.php
Normal file
45
app/Domain/Slack/Handlers/Help.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\SlashCommand\Attachment;
|
||||
use Spatie\SlashCommand\AttachmentField;
|
||||
use Spatie\SlashCommand\Handlers\Help as BaseHelpHandler;
|
||||
use Spatie\SlashCommand\Handlers\SignatureHandler;
|
||||
use Spatie\SlashCommand\Request;
|
||||
use Spatie\SlashCommand\Response;
|
||||
|
||||
class Help extends BaseHelpHandler
|
||||
{
|
||||
protected $signature = "toby pomoc";
|
||||
protected $description = "Wyświetl wszystkie dostępne komendy tobiego";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$handlers = $this->findAvailableHandlers();
|
||||
|
||||
return $this->displayListOfAllCommands($handlers);
|
||||
}
|
||||
|
||||
protected function displayListOfAllCommands(Collection $handlers): Response
|
||||
{
|
||||
$attachmentFields = $handlers
|
||||
->sort(function (SignatureHandler $handlerA, SignatureHandler $handlerB) {
|
||||
return strcmp($handlerA->getFullCommand(), $handlerB->getFullCommand());
|
||||
})
|
||||
->map(function (SignatureHandler $handler) {
|
||||
return AttachmentField::create("/{$handler->getSignature()}", $handler->getDescription());
|
||||
})
|
||||
->all();
|
||||
|
||||
return $this->respondToSlack('Dostępne komendy')
|
||||
->withAttachment(
|
||||
Attachment::create()
|
||||
->setColor('good')
|
||||
->setFields($attachmentFields)
|
||||
);
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack;
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Spatie\SlashCommand\Request;
|
||||
@@ -15,12 +15,12 @@ use Toby\Eloquent\Models\YearPeriod;
|
||||
|
||||
class HomeOffice extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby zdalnie {date=dzisiaj}";
|
||||
protected $description = "Praca zdalna {kiedy}";
|
||||
protected $signature = "toby zdalnie {kiedy?}";
|
||||
protected $description = "Pracuj zdalnie wybranego dnia (domyślnie dzisiaj)";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$date = $this->getDateFromArgument($this->getArgument('date'));
|
||||
$date = $this->getDateFromArgument($this->getArgument('kiedy') ?? "dzisiaj");
|
||||
$user = $this->findUserBySlackId($request->userId);
|
||||
|
||||
$yearPeriod = YearPeriod::findByYear($date->year);
|
||||
@@ -34,7 +34,8 @@ class HomeOffice extends SignatureHandler
|
||||
"flow_skipped" => false,
|
||||
], $user);
|
||||
|
||||
return $this->respondToSlack("Praca zdalna dnia {$date->toDisplayString()} została utworzona pomyślnie");
|
||||
return $this->respondToSlack("Praca zdalna dnia {$date->toDisplayString()} została utworzona pomyślnie.")
|
||||
->displayResponseToEveryoneOnChannel();
|
||||
}
|
||||
|
||||
protected function getDateFromArgument(string $argument): Carbon
|
33
app/Domain/Slack/Handlers/KeyList.php
Normal file
33
app/Domain/Slack/Handlers/KeyList.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Spatie\SlashCommand\Attachment;
|
||||
use Spatie\SlashCommand\Request;
|
||||
use Spatie\SlashCommand\Response;
|
||||
use Spatie\SlashCommand\Handlers\SignatureHandler;
|
||||
use Toby\Eloquent\Models\Key;
|
||||
|
||||
class KeyList extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby klucze";
|
||||
|
||||
protected $description = "Lista wszystkich kluczy";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$keys = Key::query()
|
||||
->orderBy("id")
|
||||
->get()
|
||||
->map(fn(Key $key) => "Klucz nr {$key->id} - <@{$key->user->profile->slack_id}>");
|
||||
|
||||
return $this->respondToSlack("Lista kluczy")
|
||||
->withAttachment(
|
||||
Attachment::create()
|
||||
->setColor('#3C5F97')
|
||||
->setText($keys->implode("\n"))
|
||||
);
|
||||
}
|
||||
}
|
24
app/Domain/Slack/Handlers/SaySomething.php
Normal file
24
app/Domain/Slack/Handlers/SaySomething.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Spatie\SlashCommand\Request;
|
||||
use Spatie\SlashCommand\Response;
|
||||
use Spatie\SlashCommand\Handlers\SignatureHandler;
|
||||
|
||||
class SaySomething extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby powiedz {zdanie}";
|
||||
|
||||
protected $description = "Powiedz zdanie";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$sentence = $this->getArgument("zdanie");
|
||||
|
||||
return $this->respondToSlack($sentence)
|
||||
->displayResponseToEveryoneOnChannel();
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack;
|
||||
namespace Toby\Domain\Slack\Handlers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\SlashCommand\Request;
|
||||
@@ -13,13 +13,13 @@ use Toby\Eloquent\Models\User;
|
||||
|
||||
class TakeKeysFrom extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby klucze:od {from}";
|
||||
protected $signature = "toby klucze:od {użytkownik}";
|
||||
|
||||
protected $description = "Zabierz klucze użytkownikowi {from}";
|
||||
protected $description = "Zabierz klucze wskazanemu użytkownikowi";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$from = $this->getArgument('from');
|
||||
$from = $this->getArgument("użytkownik");
|
||||
|
||||
$id = Str::between($from, "@", "|");
|
||||
|
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Domain\Slack;
|
||||
|
||||
use Spatie\SlashCommand\Request;
|
||||
use Spatie\SlashCommand\Response;
|
||||
use Spatie\SlashCommand\Handlers\SignatureHandler;
|
||||
use Toby\Eloquent\Models\Key;
|
||||
|
||||
class KeyList extends SignatureHandler
|
||||
{
|
||||
protected $signature = "toby klucze";
|
||||
|
||||
protected $description = "Lista wszystkich kluczy";
|
||||
|
||||
public function handle(Request $request): Response
|
||||
{
|
||||
$temp = [];
|
||||
|
||||
foreach (Key::orderBy("id")->get() as $key) {
|
||||
$temp[] = "Klucz nr {$key->id} - <@{$key->user->profile->slack_id}>";
|
||||
}
|
||||
|
||||
|
||||
return $this->respondToSlack(implode("\n", $temp))
|
||||
->displayResponseToEveryoneOnChannel();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user