Php Slack API Laravel使用spatie/Laravel Slack SLACH命令包格式化错误
我使用的是一个packagespatie/laravel slack slash命令,下面的代码运行良好,除了这种情况,如果用户没有输入参数,就会捕获错误,因为包中有一个Exceptions\InvalidInput.php类,我只想知道如何格式化此错误或如何覆盖getResponse()方法输出如下,谢谢 InvalidInput.phpPhp Slack API Laravel使用spatie/Laravel Slack SLACH命令包格式化错误,php,laravel,slack-api,spatie,Php,Laravel,Slack Api,Spatie,我使用的是一个packagespatie/laravel slack slash命令,下面的代码运行良好,除了这种情况,如果用户没有输入参数,就会捕获错误,因为包中有一个Exceptions\InvalidInput.php类,我只想知道如何格式化此错误或如何覆盖getResponse()方法输出如下,谢谢 InvalidInput.php public function getResponse(Request $request): Response { return parent::g
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)
);
}
}
然后尝试检查是否为空,但这也不起作用
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}")
);
}
对不起,我忘了跟你打招呼了!错误发生在哪里?当你没有在getArgument()中输入“day”时函数?您可以创建一个trait,然后包含并使用该trait覆盖该方法吗?这样,包仍然是可更新的,并且您的trait将接管您想要征用的功能。找到了如何通过修改validate()获得我期望的结果SignatureHandler.php注释中的方法抛出异常,返回一个空数组,并检查handle方法中的empty现在是否工作。感谢您的帮助。虽然这不是最好的方法,但这是实现我想要的最快的方法
public function validate()
{
try {
$this->input->validate();
} catch (RuntimeException $exception) {
//throw new InvalidInput($exception->getMessage(), $this, $exception);
return [];
}
}
是,当我使用类似于以下命令的命令时,会出现错误输出:
/tasks day
,故意对“day”参数发出命令必须执行的操作:在其所在的任何位置查找getArgument()函数。可能存在类似此公共函数getArgument($variable)的内容,您应该将其更改为公共函数getArgument($variable=null)在第一行的函数中,只要说if($variable==null)return[],然后键入if(empty($slack_request))将起作用。按照逻辑。它将带您找到解决方案。无法提供更多信息,因为我无法在该代码中看到getargument函数我认为我理解您的方法足够简单。我需要做的只是将SignatureHandler.php getargument()更改为公共函数getargument($foo==null){return$this->input->getArgument($foo);}
correct?有没有办法在包更新时保护此修改?这当然是一种方法,但我还没有创建Traits,我必须研究它,然后了解如何使用包作者使用empty方法发送对slackif(empty($domain))的响应{return$this->respondToSlack(“您必须提供域名。”);}
看起来很有趣!我相信你已经找到了这些……我认为你需要创建自己的Handler这些文档让我找到了这些:确实很有趣,我已经找到了这些链接,而且SignatureHandler类已经存在于这个令人惊讶的包中,感谢你花时间帮助@developer
public function validate()
{
try {
$this->input->validate();
} catch (RuntimeException $exception) {
//throw new InvalidInput($exception->getMessage(), $this, $exception);
return [];
}
}