This commit is contained in:
Adrian Hopek
2022-04-25 13:23:49 +02:00
parent 851a52fe32
commit 25816cc47a
28 changed files with 531 additions and 239 deletions

View File

@@ -4,48 +4,32 @@ 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\Handlers\BaseHandler;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use Toby\Domain\Slack\Traits\ListsHandlers;
class CatchAll extends BaseCatchAllHandler
class CatchAll extends BaseHandler
{
use ListsHandlers;
public function canHandle(Request $request): bool
{
return true;
}
public function handle(Request $request): Response
{
$response = $this->respondToSlack("Nie rozpoznaję tej komendy: `/{$request->command} {$request->text}`");
$handlers = $this->findAvailableHandlers();
$attachmentFields = $this->mapHandlersToAttachments($handlers);
[$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 $this->respondToSlack(":x: Nie rozpoznaję tej komendy. Lista wszystkich komend:")
->withAttachment(
Attachment::create()
->setColor("danger")
->useMarkdown()
->setFields($attachmentFields),
);
}
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);
}
}