Issue
I am using a package spatie/laravel-slack-slash-command and the code below works fine except for this condition if the user inputs no argurment, there’s an error being caught because there’s a Exceptions\InvalidInput.php class in the package, I would simply like to know how to format this error or how to overide the getResponse() method wich outputs like this, thanks!
InvalidInput.php
public function getResponse(Request $request): Response
{
return parent::getResponse($request)
->withAttachment(
Attachment::create()
->setText($this->handler->getHelpDescription())
);
}
This is how I would like to format the error
if (empty($slack_request))
{
return $this->respondToSlack("Argument Missing")
->withAttachment(Attachment::create()
->setColor('warning')
->setText("You must provide two digits between 00 and 13 after ex : /tasks day {00}")
);
}
Here’s my Class
<?php
namespace App\SlashCommandHandlers;
use Spatie\SlashCommand\Attachment;
use Spatie\SlashCommand\AttachmentField;
use Spatie\SlashCommand\Handlers\SignatureHandler;
use Spatie\SlashCommand\Request;
use Spatie\SlashCommand\Response;
use App\Models\Retry42\Project;
class Projects extends SignatureHandler
{
protected $signature = 'tasks day {day}';
public function handle(Request $request): Response
{
$slack_request = $this->getArgument('day');
if (empty($slack_request))
{
return $this->respondToSlack("Argument Missing")
->withAttachment(Attachment::create()
->setColor('warning')
->setText("You must provide two digits between 00 and 13 after ex : /tasks day {00}")
);
}
if(!preg_match("/^(0[0-9]|1[0-3])$/", $slack_request))
{
return $this->respondToSlack("Invalid argument, two digits between 00 and 13")
->withAttachment(Attachment::create()
->setColor('warning')
->setText("Project day must be two digits between 00 and 13")
);
}
$day = $slack_request;
$project = 'Day '.$day;
$project = Project::where('name', '=', $project)->firstOrFail();
$tasks = $project->tasks->toArray();
if (!count($tasks)) {
return $this->respondToSlack("Sorry we could not get you any tasks for Day {$day}")
->withAttachment(Attachment::create()
->setColor('warning')
->setText("Try another day!")
);
}
$attachmentFields = collect($tasks)->reduce(function (array $attachmentFields, array $task) {
$value = $task['description'] ?? '';
if($task['visible'] == 1)
{
$attachmentFields[] = AttachmentField::create('Name', $task['name'])->displaySideBySide();
$attachmentFields[] = AttachmentField::create('Description', $value)->displaySideBySide();
}
return $attachmentFields;
}, []);
return $this->respondToSlack("Here are the tasks for Day {$day}")
->withAttachment(Attachment::create()
->setColor('good')
->setFields($attachmentFields)
);
}
}
I tried like suggested editing the SignatureHandler.php from $foo to this
public function getArgument($foo = null)
{
if($foo == null) return [];
return $this->input->getArgument($foo);
}
and then trying to check if empty but that did not work either
if (empty($slack_request))
{
return $this->respondToSlack("Argument Missing")
->withAttachment(Attachment::create()
->setColor('warning')
->setText("You must provide two digits between 00 and 13 after ex : /tasks day {00}")
);
}
Sorry I forgot to say hello to you!
Solution
Found how to get the result I was expecting by modifying the validate() method in the SignatureHandler.php commenting throw exception returning instead an empty array and check if empty works now in the handle method. Thanks for the help. Though this is not the best way to do this it’s the quickest way to accomplish what I wanted for this.
public function validate()
{
try {
$this->input->validate();
} catch (RuntimeException $exception) {
//throw new InvalidInput($exception->getMessage(), $this, $exception);
return [];
}
}
Answered By – David Dacruz
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0