Slack API Laravel 使用spatie/laravel-slack-slash-command 包格式错误

问题描述 投票:0回答:2

我正在使用包 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}")
        );
    }

抱歉忘记跟你打招呼了!

php laravel slack-api
2个回答
2
投票

你能做一个特征,然后包含并使用那个特征来覆盖方法吗?这样包仍然可以更新,你的特征将接管你想要征用的功能。


1
投票

找到如何通过修改 SignatureHandler.php 中的 validate() 方法来获得我期望的结果,注释抛出异常返回而不是一个空数组,并检查 handle 方法中现在是否有效。谢谢您的帮助。虽然这不是最好的方法,但这是完成我想要的最快方法。

 public function validate()
{
    try {
        $this->input->validate();
    } catch (RuntimeException $exception) {
     //throw new InvalidInput($exception->getMessage(), $this, $exception);
            return [];
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.