1

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 argument, 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 override the getResponse() method which outputs like this, thanks!

InvalidInput.php

public function getResponse(Request $request): Response
{
    return parent::getResponse($request)
        ->withAttachment(
            Attachment::create()
                ->setText($this->handler->getHelpDescription())
        );
}

Error

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!

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
David Dacruz
  • 137
  • 2
  • 16

2 Answers2

2

Can you make a trait, then include and use that trait to override the method? That way the package will still be updatable and your trait will take over the functionality you want to commandeer.

jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
  • This is certainly a way to do this but I haven' t created Traits yet I have to look into it and following this [tutorial](https://murze.be/building-a-laravel-powered-slack-bot) on how to use the package the author uses the empty method to send a response to slack `if (empty($domain)) { return $this->respondToSlack("You must provide a domain name."); }` – David Dacruz Dec 31 '17 at 19:06
  • 1
    Looks fun! I am sure you have found these... https://docs.spatie.be/laravel-slack-slash-command/v1/introduction – jeremykenedy Dec 31 '17 at 19:22
  • I think you need to create your own handler – jeremykenedy Dec 31 '17 at 19:23
  • 1
    Those docs led me to these: https://api.slack.com/docs/message-attachments – jeremykenedy Dec 31 '17 at 19:24
  • Indeed it is quite fun and I have found those links, also the SignatureHandler Class already exists in this awsome package, thanks for your time to help @developernator – David Dacruz Dec 31 '17 at 20:39
1

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 [];
    }
}

Formated output

David Dacruz
  • 137
  • 2
  • 16