我使用的是一个包spatie/laravel-slack-slash-command,下面的代码工作正常,除了这种情况,如果用户没有输入参数,会有一个错误被捕获,因为包中有一个Exceptions\InvalidInput.php类,我只是想知道如何格式化这个错误或如何覆盖这样输出的getResponse()方法,谢谢!
InvalidInput.php
public function getResponse(Request $request): Response
{
return parent::getResponse($request)
->withAttachment(
Attachment::create()
->setText($this->handler->getHelpDescription())
);
}
这是我想如何格式化错误
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}")
);
}
这是我的班级
<?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)
);
}
}
我试着像建议的那样将SignatureHandler.php从$foo编辑为这个
public function getArgument($foo = null)
{
if($foo == null) return [];
return $this->input->getArgument($foo);
}
然后尝试检查是否为空,但也不起作用
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}")
);
}
对不起,我忘了跟你打招呼!
2条答案
按热度按时间bjg7j2ky1#
你能创建一个trait,然后包含并使用这个trait来覆盖方法吗?这样包仍然是可更新的,你的trait将接管你想要征用的功能。
368yc8dk2#
找到了如何通过修改SignatureHandler.phpcommenting中的validate()方法来获得我所期望的结果,抛出异常返回一个空数组,并检查handle方法中的empty是否有效。感谢您的帮助。虽然这不是最好的方法,但这是完成我想要的最快的方法。