【问题标题】:Slack API Laravel using spatie/laravel-slack-slash-command package formating errorsSlack API Laravel 使用 spatie/laravel-slack-slash-command 包格式错误
【发布时间】:2017-12-31 18:38:44
【问题描述】:

我正在使用一个包 spatie/laravel-slack-slash-command 并且下面的代码可以正常工作,除了这种情况,如果用户没有输入参数,则会捕获一个错误,因为有一个异常\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


    【解决方案1】:

    你能否创建一个特征,然后包含并使用该特征来覆盖该方法?这样,包仍然可以更新,并且您的 trait 将接管您想要征用的功能。

    【讨论】:

    • 这当然是一种方法,但我还没有创建 Traits,但我必须研究它并遵循这个tutorial 关于如何使用作者使用空方法发送的包回复 slack if (empty($domain)) { return $this-&gt;respondToSlack("You must provide a domain name."); }
    • 看起来很有趣!我相信你已经找到了这些……docs.spatie.be/laravel-slack-slash-command/v1/introduction
    • 我认为您需要创建自己的处理程序
    • 那些文档把我带到了这些:api.slack.com/docs/message-attachments
    • 确实很有趣,我找到了这些链接,而且 SignatureHandler 类已经存在于这个可怕的包中,感谢您抽出时间来帮助@developernator
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2019-12-11
      • 2017-07-12
      • 1970-01-01
      相关资源
      最近更新 更多