From 4ff944cb3a5296504b305b4ae5ab2749af8c402d Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 4 Sep 2022 12:18:11 +1000 Subject: [PATCH] Implemented SlackSyntaxException and removed some deprecated functions --- src/API.php | 42 +++--- src/BlockKit.php | 4 +- src/Blockkit/Blocks/Accessories/Overflow.php | 8 +- src/Blockkit/Blocks/Actions.php | 6 +- src/Blockkit/Blocks/Context.php | 6 +- src/Blockkit/Blocks/Elements/Button.php | 9 +- .../Blocks/Elements/ExternalSelect.php | 11 +- .../Blocks/Elements/MultiExternalSelect.php | 12 +- .../Blocks/Elements/MultiStaticSelect.php | 18 +-- src/Blockkit/Blocks/Elements/Options.php | 9 +- .../Blocks/Elements/PlainTextInput.php | 7 +- src/Blockkit/Blocks/Elements/StaticSelect.php | 14 +- src/Blockkit/Blocks/Elements/Text.php | 7 +- src/Blockkit/Blocks/Header.php | 9 +- src/Blockkit/Blocks/Input.php | 15 +-- src/Blockkit/Blocks/Section.php | 12 +- src/Blockkit/Modal.php | 49 +++---- src/Console/Commands/SlackSocketClient.php | 5 +- src/Exceptions/SlackSyntaxException.php | 7 + .../InteractiveOptionsController.php | 1 - src/Interactive/BlockActions.php | 9 +- src/Interactive/ViewSubmission.php | 9 +- src/Jobs/DeleteResponse.php | 2 - src/Message.php | 122 +++++------------- src/Message/Attachment.php | 1 - src/Message/AttachmentAction.php | 11 +- src/Options/Base.php | 1 - src/Options/Unknown.php | 1 - 28 files changed, 169 insertions(+), 238 deletions(-) create mode 100644 src/Exceptions/SlackSyntaxException.php diff --git a/src/API.php b/src/API.php index 499fb1c..81fe68c 100644 --- a/src/API.php +++ b/src/API.php @@ -74,8 +74,8 @@ final class API /** * Delete a message in a channel * - * @param $channel - * @param $timestamp + * @param string $channel + * @param string $timestamp * @return Generic * @throws \Exception */ @@ -104,8 +104,8 @@ final class API /** * Get Messages on a channel from a specific timestamp * - * @param $channel - * @param $timestamp + * @param string $channel + * @param string $timestamp * @param int $limit * @return Generic * @throws \Exception @@ -120,7 +120,7 @@ final class API /** * Get information on a channel. * - * @param $channel + * @param string $channel * @return Generic * @throws \Exception */ @@ -177,7 +177,7 @@ final class API /** * Get information on a user * - * @param $user_id + * @param string $user_id * @return ResponseUser * @throws \Exception */ @@ -191,8 +191,8 @@ final class API /** * Get the list of channels for a user (the bot normally) * - * @param User $uo - * @param int $limit + * @param User $uo + * @param int $limit * @param string|null $cursor * @return ChannelList * @throws \Exception @@ -302,8 +302,8 @@ final class API /** * Remove a Pin from a message * - * @param $channel - * @param $timestamp + * @param string $channel + * @param string $timestamp * @return Generic * @throws \Exception */ @@ -370,18 +370,26 @@ final class API * Call the Slack API * * @param string $method - * @param null $parameters + * @param null $parameters * @return object - * @throws \Exception + * @throws SlackAlreadyPinnedException + * @throws SlackChannelNotFoundException + * @throws SlackException + * @throws SlackHashConflictException + * @throws SlackMessageNotFoundException + * @throws SlackNoAuthException + * @throws SlackNoPinException + * @throws SlackNotFoundException + * @throws SlackNotInChannelException + * @throws SlackThreadNotFoundException + * @throws SlackTokenScopeException */ private function execute(string $method,$parameters=NULL): object { switch (config('app.env')) { - case 'dev': $url = 'http://steno:3000'; + case 'steno': $url = 'http://steno:3000'; break; - case 'testing': $url = 'http://localhost:3000'; - break; - case 'testing-l': $url = 'http://steno_replay:3000'; + case 'replay': $url = 'http://steno_replay:3000'; break; default: $url = 'https://slack.com'; @@ -415,7 +423,7 @@ final class API ); } else { - throw new \Exception('Parameters unknown'); + throw new SlackException('Parameters unknown'); } try { diff --git a/src/BlockKit.php b/src/BlockKit.php index c8489d0..fa1d182 100644 --- a/src/BlockKit.php +++ b/src/BlockKit.php @@ -5,6 +5,8 @@ namespace Slack; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use Slack\Exceptions\SlackSyntaxException; + /** * Class BlockKit - Slack Blockit Objects * @@ -40,7 +42,7 @@ abstract class BlockKit implements \JsonSerializable protected function validate(string $key,$value) { if (Arr::get(static::LIMITS,$key) && (strlen($value) > static::LIMITS[$key])) - throw new Exception(sprintf('%s must be %d chars or less for buttons %s',$key,self::LIMITS[$key],get_class($this))); + throw new SlackSyntaxException(sprintf('%s must be %d chars or less for buttons %s',$key,static::LIMITS[$key],get_class($this))); return $value; } diff --git a/src/Blockkit/Blocks/Accessories/Overflow.php b/src/Blockkit/Blocks/Accessories/Overflow.php index 049eb54..3eedb74 100644 --- a/src/Blockkit/Blocks/Accessories/Overflow.php +++ b/src/Blockkit/Blocks/Accessories/Overflow.php @@ -2,12 +2,12 @@ namespace Slack\Blockkit\Blocks\Accessories; -use \Exception; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Slack\Blockkit\Blocks\Elements\{Confirm,Text}; use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class Overflow extends Element { @@ -21,7 +21,7 @@ final class Overflow extends Element /** * @param string $action_id * @param Collection $options - * @throws Exception + * @throws SlackSyntaxException * @todo We dont handle option_groups yet. */ public function __construct(string $action_id,Collection $options) @@ -34,10 +34,10 @@ final class Overflow extends Element $this->action_id = $this->validate('action_id',$action_id); if (count($options) < self::MIN_OPTIONS) - throw new Exception(sprintf('Must have atleast %d options',self::MIN_OPTIONS)); + throw new SlackSyntaxException(sprintf('Must have atleast %d options',self::MIN_OPTIONS)); if (count($options) > self::MAX_OPTIONS) - throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); $this->options = $options->transform(function($item) { return ['text'=>Text::item(Arr::get($item,'name'),'plain_text'),'value'=>(string)Arr::get($item,'id')]; diff --git a/src/Blockkit/Blocks/Actions.php b/src/Blockkit/Blocks/Actions.php index e08d2b1..96f17a9 100644 --- a/src/Blockkit/Blocks/Actions.php +++ b/src/Blockkit/Blocks/Actions.php @@ -2,11 +2,11 @@ namespace Slack\Blockkit\Blocks; -use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\Blocks; use Slack\Blockkit\Blocks\Elements\{Button,MultiStaticSelect}; +use Slack\Exceptions\SlackSyntaxException; final class Actions extends Blocks { @@ -38,7 +38,7 @@ final class Actions extends Blocks public function jsonSerialize() { if (! $this->elements) - throw new Exception('Must define at least 1 element'); + throw new SlackSyntaxException('Must define at least 1 element'); return parent::jsonSerialize(); } @@ -55,7 +55,7 @@ final class Actions extends Blocks public function elements(Collection $collection): self { if (count($collection) > self::MAX_ELEMENTS) - throw new Exception(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS)); // @todo Check that a valid element is added. https://api.slack.com/reference/block-kit/blocks#actions diff --git a/src/Blockkit/Blocks/Context.php b/src/Blockkit/Blocks/Context.php index 8d9e9b7..9f1a7a2 100644 --- a/src/Blockkit/Blocks/Context.php +++ b/src/Blockkit/Blocks/Context.php @@ -2,10 +2,10 @@ namespace Slack\Blockkit\Blocks; -use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\Blocks; +use Slack\Exceptions\SlackSyntaxException; final class Context extends Blocks { @@ -17,7 +17,7 @@ final class Context extends Blocks /** * @param Collection $collection - * @throws Exception + * @throws SlackSyntaxException * @todo Collection can only be image or text elements */ public function __construct(Collection $collection) @@ -28,7 +28,7 @@ final class Context extends Blocks $this->type = 'context'; if (count($collection) > self::MAX_ELEMENTS) - throw new Exception(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS)); $this->elements = $collection; } diff --git a/src/Blockkit/Blocks/Elements/Button.php b/src/Blockkit/Blocks/Elements/Button.php index aea58df..5ee61f2 100644 --- a/src/Blockkit/Blocks/Elements/Button.php +++ b/src/Blockkit/Blocks/Elements/Button.php @@ -2,9 +2,8 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; - use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class Button extends Element { @@ -24,10 +23,10 @@ final class Button extends Element $this->type = 'button'; if ($text->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$text->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); if (strlen($text->text) > self::LIMITS['text']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); $this->text = $text; @@ -52,7 +51,7 @@ final class Button extends Element public function style(string $string): self { if (! in_array($string,['default','primary','danger'])) - throw new Exception(sprintf('Unknown style %s',$string)); + throw new SlackSyntaxException(sprintf('Unknown style %s',$string)); $this->style = $string; diff --git a/src/Blockkit/Blocks/Elements/ExternalSelect.php b/src/Blockkit/Blocks/Elements/ExternalSelect.php index dbbd314..0dd4582 100644 --- a/src/Blockkit/Blocks/Elements/ExternalSelect.php +++ b/src/Blockkit/Blocks/Elements/ExternalSelect.php @@ -2,9 +2,8 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; - use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class ExternalSelect extends Element { @@ -16,7 +15,7 @@ final class ExternalSelect extends Element /** * @param Text $placeholder * @param string $action_id - * @throws Exception + * @throws SlackSyntaxException */ public function __construct(Text $placeholder,string $action_id) { @@ -26,10 +25,10 @@ final class ExternalSelect extends Element $this->type = 'external_select'; if ($placeholder->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); $this->placeholder = $placeholder; @@ -68,7 +67,7 @@ final class ExternalSelect extends Element public function min_query_length(int $int): self { if ($int < 1) - throw new Exception('Minimum 1 options must be configured'); + throw new SlackSyntaxException('Minimum 1 options must be configured'); $this->min_query_length = $int; diff --git a/src/Blockkit/Blocks/Elements/MultiExternalSelect.php b/src/Blockkit/Blocks/Elements/MultiExternalSelect.php index 21e9876..695db5b 100644 --- a/src/Blockkit/Blocks/Elements/MultiExternalSelect.php +++ b/src/Blockkit/Blocks/Elements/MultiExternalSelect.php @@ -2,10 +2,10 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class MultiExternalSelect extends Element { @@ -21,7 +21,7 @@ final class MultiExternalSelect extends Element /** * @param Text $placeholder * @param string $action_id - * @throws Exception + * @throws SlackSyntaxException * @todo We dont handle option_groups yet. */ public function __construct(Text $placeholder,string $action_id) @@ -32,10 +32,10 @@ final class MultiExternalSelect extends Element $this->type = 'multi_external_select'; if ($placeholder->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); $this->placeholder = $placeholder; @@ -74,7 +74,7 @@ final class MultiExternalSelect extends Element public function min_query_length(int $int): self { if ($int < 1) - throw new Exception('Minimum 1 options must be configured'); + throw new SlackSyntaxException('Minimum 1 options must be configured'); $this->min_query_length = $int; @@ -84,7 +84,7 @@ final class MultiExternalSelect extends Element public function max_selected_items(int $int): self { if ($int < 1) - throw new Exception('Minimum 1 options must be configured'); + throw new SlackSyntaxException('Minimum 1 options must be configured'); $this->max_selected_items = $int; diff --git a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php index 5b85eea..ff00c5b 100644 --- a/src/Blockkit/Blocks/Elements/MultiStaticSelect.php +++ b/src/Blockkit/Blocks/Elements/MultiStaticSelect.php @@ -2,10 +2,10 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class MultiStaticSelect extends Element { @@ -22,7 +22,7 @@ final class MultiStaticSelect extends Element * @param Text $placeholder * @param string $action_id * @param Collection $options - * @throws Exception + * @throws SlackSyntaxException * @todo We dont handle option_groups yet. */ public function __construct(Text $placeholder,string $action_id,Collection $options) @@ -33,20 +33,20 @@ final class MultiStaticSelect extends Element $this->type = 'multi_static_select'; if ($placeholder->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); $this->placeholder = $placeholder; $this->action_id = $this->validate('action_id',$action_id); if (! $options->count()) - throw new Exception('There are no options?'); + throw new SlackSyntaxException('There are no options?'); if ($options->count() > self::MAX_OPTIONS) - throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); $this->options = $options->transform(function($item) { return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value]; @@ -79,10 +79,10 @@ final class MultiStaticSelect extends Element // No initial options. if (count($initial)) { if (! $this->options) - throw new Exception('Cannot set an initial value without options defined first'); + throw new SlackSyntaxException('Cannot set an initial value without options defined first'); if (count($initial) > self::MAX_OPTIONS) - throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); $this->initial_options = $this->options->filter(function($item) use ($initial) { return in_array($item['value'],$initial); }); } @@ -93,7 +93,7 @@ final class MultiStaticSelect extends Element public function max_selected_items(int $int): self { if ($int < 1) - throw new Exception('Minimum 1 options must be configured'); + throw new SlackSyntaxException('Minimum 1 options must be configured'); $this->max_selected_items = $int; diff --git a/src/Blockkit/Blocks/Elements/Options.php b/src/Blockkit/Blocks/Elements/Options.php index 889f26e..4bbf988 100644 --- a/src/Blockkit/Blocks/Elements/Options.php +++ b/src/Blockkit/Blocks/Elements/Options.php @@ -2,9 +2,8 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; - use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; /** * @note Overflow, select, and multi-select menus can only use plain_text objects, @@ -24,7 +23,7 @@ final class Options extends Element parent::__construct(); if (strlen($text->text) > self::LIMITS['text']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); $this->text = $text; $this->value = $this->validate('value',$value); @@ -40,10 +39,10 @@ final class Options extends Element public function description(Text $text): self { if ($text->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$text->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); if (strlen($text->text) > self::LIMITS['description']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['description'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['description'])); $this->description = $text; diff --git a/src/Blockkit/Blocks/Elements/PlainTextInput.php b/src/Blockkit/Blocks/Elements/PlainTextInput.php index 812e585..0ce3fba 100644 --- a/src/Blockkit/Blocks/Elements/PlainTextInput.php +++ b/src/Blockkit/Blocks/Elements/PlainTextInput.php @@ -3,6 +3,7 @@ namespace Slack\Blockkit\Blocks\Elements; use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; /** * This is an element of an input dialog @@ -45,7 +46,7 @@ final class PlainTextInput extends Element public function min_length(int $int): self { if ($int > self::MAX_MIN_LENGTH) - throw new Exception(sprintf('min_length must be less than %d',self::MAX_MIN_LENGTH)); + throw new SlackSyntaxException(sprintf('min_length must be less than %d',self::MAX_MIN_LENGTH)); $this->min_length = $int; @@ -55,7 +56,7 @@ final class PlainTextInput extends Element public function max_length(int $int): self { if ($this->min_length && ($int < $this->min_length)) - throw new Exception('max_length must be greater than min_length'); + throw new SlackSyntaxException('max_length must be greater than min_length'); $this->max_length = $int; @@ -72,7 +73,7 @@ final class PlainTextInput extends Element public function placeholder(Text $text): self { if (strlen($text->text) > self::LIMITS['placeholder']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); $this->placeholder = $text; diff --git a/src/Blockkit/Blocks/Elements/StaticSelect.php b/src/Blockkit/Blocks/Elements/StaticSelect.php index da37e7b..1bf35c6 100644 --- a/src/Blockkit/Blocks/Elements/StaticSelect.php +++ b/src/Blockkit/Blocks/Elements/StaticSelect.php @@ -2,10 +2,10 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class StaticSelect extends Element { @@ -22,7 +22,7 @@ final class StaticSelect extends Element * @param Text $placeholder * @param string $action_id * @param Collection $options - * @throws Exception + * @throws SlackSyntaxException * @todo We dont handle option_groups yet. */ public function __construct(Text $placeholder,string $action_id,Collection $options) @@ -33,20 +33,20 @@ final class StaticSelect extends Element $this->type = 'static_select'; if ($placeholder->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$placeholder->type)); if (strlen($placeholder->text) > self::LIMITS['placeholder']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder'])); $this->placeholder = $placeholder; $this->action_id = $this->validate('action_id',$action_id); if (! $options->count()) - throw new Exception('There are no options?'); + throw new SlackSyntaxException('There are no options?'); if ($options->count() > self::MAX_OPTIONS) - throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS)); $this->options = $options->transform(function($item) { return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value]; @@ -77,7 +77,7 @@ final class StaticSelect extends Element public function initial_option(string $string=NULL): self { if (! $this->options) - throw new Exception('Cannot set an initial value without options defined first'); + throw new SlackSyntaxException('Cannot set an initial value without options defined first'); if ($string) $this->initial_option = $this->options->first(function($item) use ($string) { return $item['value'] == $string; }); diff --git a/src/Blockkit/Blocks/Elements/Text.php b/src/Blockkit/Blocks/Elements/Text.php index e58547c..88919b7 100644 --- a/src/Blockkit/Blocks/Elements/Text.php +++ b/src/Blockkit/Blocks/Elements/Text.php @@ -2,9 +2,8 @@ namespace Slack\Blockkit\Blocks\Elements; -use \Exception; - use Slack\Blockkit\Element; +use Slack\Exceptions\SlackSyntaxException; final class Text extends Element { @@ -15,7 +14,7 @@ final class Text extends Element parent::__construct(); if (! in_array($type,self::TYPES)) - throw new Exception(sprintf('Type [%s] not valid',$type)); + throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type)); $this->type = $type; $this->text = $text; @@ -31,7 +30,7 @@ final class Text extends Element public function emoji(bool $bool): self { if ($x=$this->type != 'plain_text') - throw new Exception(sprintf('Cannnot use emoji when type is [%s]',$x)); + throw new SlackSyntaxException(sprintf('Cannnot use emoji when type is [%s]',$x)); $this->emoji = $bool; diff --git a/src/Blockkit/Blocks/Header.php b/src/Blockkit/Blocks/Header.php index b6c2a9e..e6f2321 100644 --- a/src/Blockkit/Blocks/Header.php +++ b/src/Blockkit/Blocks/Header.php @@ -2,10 +2,9 @@ namespace Slack\Blockkit\Blocks; -use \Exception; - use Slack\Blockkit\Blocks; use Slack\Blockkit\Blocks\Elements\Text; +use Slack\Exceptions\SlackSyntaxException; final class Header extends Blocks { @@ -16,7 +15,7 @@ final class Header extends Blocks /** * @param Text $text - * @throws Exception + * @throws SlackSyntaxException */ public function __construct(Text $text) { @@ -26,10 +25,10 @@ final class Header extends Blocks $this->type = 'header'; if ($text->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$text->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); if (strlen($text->text) > self::LIMITS['text']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); $this->text = $text; } diff --git a/src/Blockkit/Blocks/Input.php b/src/Blockkit/Blocks/Input.php index e6da7ec..b636396 100644 --- a/src/Blockkit/Blocks/Input.php +++ b/src/Blockkit/Blocks/Input.php @@ -2,9 +2,8 @@ namespace Slack\Blockkit\Blocks; -use \Exception; - use Slack\Blockkit\{Blocks,Element}; +use Slack\Exceptions\SlackSyntaxException; final class Input extends Blocks { @@ -23,20 +22,20 @@ final class Input extends Blocks /** * @param Elements\Text|null $label - * @throws Exception + * @throws SlackSyntaxException */ public function __construct(Elements\Text $label=NULL) { parent::__construct(); if ($label->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$label->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$label->type)); // Defaults $this->type = 'input'; if (strlen($label->text) > self::LIMITS['label']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['label'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['label'])); $this->label = $label; } @@ -49,7 +48,7 @@ final class Input extends Blocks public function jsonSerialize() { if (! $this->element) - throw new Exception('Must define an element'); + throw new SlackSyntaxException('Must define an element'); return parent::jsonSerialize(); } @@ -66,7 +65,7 @@ final class Input extends Blocks public function element(Element $object): self { if (! in_array(get_class($object),self::VALID_ELEMENTS)) - throw new Exception(sprintf('Invalid element [%s] added to input',get_class($object))); + throw new SlackSyntaxException(sprintf('Invalid element [%s] added to input',get_class($object))); $this->element = $object; @@ -76,7 +75,7 @@ final class Input extends Blocks public function hint(Elements\Text $text): self { if (strlen($text->text) > self::LIMITS['hint']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['hint'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['hint'])); $this->hint = $text; diff --git a/src/Blockkit/Blocks/Section.php b/src/Blockkit/Blocks/Section.php index 149949f..0385725 100644 --- a/src/Blockkit/Blocks/Section.php +++ b/src/Blockkit/Blocks/Section.php @@ -2,11 +2,11 @@ namespace Slack\Blockkit\Blocks; -use \Exception; use Illuminate\Support\Collection; use Slack\Blockkit\{Blocks,Element}; use Slack\BLockKit\Blocks\Elements\Text; +use Slack\Exceptions\SlackSyntaxException; final class Section extends Blocks { @@ -20,7 +20,7 @@ final class Section extends Blocks /** * @param Text|NULL $text not required if fields is provided - * @throws Exception + * @throws SlackSyntaxException */ public function __construct(Text $text=NULL) { @@ -31,7 +31,7 @@ final class Section extends Blocks if ($text) { if (strlen($text->text) > self::LIMITS['text']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); $this->text = $text; } @@ -45,7 +45,7 @@ final class Section extends Blocks public function jsonSerialize() { if (! $this->text && ! $this->fields && ! $this->accessory) - throw new Exception('Must define text, accessory or fields'); + throw new SlackSyntaxException('Must define text, accessory or fields'); return parent::jsonSerialize(); } @@ -69,10 +69,10 @@ final class Section extends Blocks public function fields(Collection $collection): self { if (count($collection) > self::MAX_FIELDS) - throw new Exception(sprintf('Can only have maximum %d fields',self::MAX_FIELDS)); + throw new SlackSyntaxException(sprintf('Can only have maximum %d fields',self::MAX_FIELDS)); if (($x=$collection->map(function($item) { return strlen($item->text); })->max()) > self::MAX_FIELDS_TEXT) - throw new Exception(sprintf('The maximum size of the text in a field is %d (%d)',self::MAX_FIELDS_TEXT,$x)); + throw new SlackSyntaxException(sprintf('The maximum size of the text in a field is %d (%d)',self::MAX_FIELDS_TEXT,$x)); $this->fields = $collection; diff --git a/src/Blockkit/Modal.php b/src/Blockkit/Modal.php index 0d6e68d..f38fd1a 100644 --- a/src/Blockkit/Modal.php +++ b/src/Blockkit/Modal.php @@ -2,12 +2,11 @@ namespace Slack\Blockkit; -use \Exception; - use Slack\BlockKit; use Slack\Blockkit\Blocks\Divider; use Slack\Blockkit\Blocks\Elements\Text; use Slack\Blockkit\Blocks\Section; +use Slack\Exceptions\SlackSyntaxException; /** * This class creates a slack Modal Response @@ -31,17 +30,17 @@ final class Modal extends BlockKit parent::__construct(); if (! in_array($type,['modal','home'])) - throw new Exception(sprintf('Unknown type %s',$type)); + throw new SlackSyntaxException(sprintf('Unknown type %s',$type)); if ($type != 'modal' && $title) - throw new Exception(sprintf('Titles are not required for %s',$type)); + throw new SlackSyntaxException(sprintf('Titles are not required for %s',$type)); if ($title) { if ($title->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$title->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$title->type)); if (strlen($title->text) > self::LIMITS['text']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text'])); $this->title = $title; } @@ -56,14 +55,14 @@ final class Modal extends BlockKit * * @param Blocks $block * @return $this - * @throws Exception + * @throws SlackSyntaxException */ public function addBlock(Blocks $block): self { $this->blocks->push($block); if ($x=$this->blocks->count() > self::MAX_BLOCKS) - throw new Exception(sprintf('Modal can only have %d blocks',self::MAX_BLOCKS)); + throw new SlackSyntaxException(sprintf('Modal can only have %d blocks',self::MAX_BLOCKS)); return $this; } @@ -90,7 +89,7 @@ final class Modal extends BlockKit public function action(string $string) { if (! in_array($string,['clear','update'])) - throw new Exception(sprintf('Unknown action %s',$string)); + throw new SlackSyntaxException(sprintf('Unknown action %s',$string)); $this->action = $string; @@ -100,7 +99,7 @@ final class Modal extends BlockKit public function clear_on_close(bool $bool): self { if ($this->type != 'modal') - throw new Exception(sprintf('clear_on_close is not required for %s',$type)); + throw new SlackSyntaxException(sprintf('clear_on_close is not required for %s',$this->type)); $this->clear_on_close = $bool; @@ -117,13 +116,13 @@ final class Modal extends BlockKit public function close(Text $text): self { if ($this->type != 'modal') - throw new Exception(sprintf('Close is not required for %s',$type)); + throw new SlackSyntaxException(sprintf('Close is not required for %s',$this->type)); if ($text->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$text->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); if (strlen($text->text) > self::LIMITS['close']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['close'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['close'])); $this->close = $text; @@ -153,7 +152,7 @@ final class Modal extends BlockKit public function notify_on_close(bool $bool): self { if ($this->type != 'modal') - throw new Exception(sprintf('notify_on_close is not required for %s',$type)); + throw new SlackSyntaxException(sprintf('notify_on_close is not required for %s',$this->type)); $this->notify_on_close = $bool; @@ -178,13 +177,13 @@ final class Modal extends BlockKit public function submit(Text $text): self { if ($this->type != 'modal') - throw new Exception(sprintf('Submit is not required for %s',$type)); + throw new SlackSyntaxException(sprintf('Submit is not required for %s',$this->type)); if ($text->type != 'plain_text') - throw new Exception(sprintf('Text must be plain_text not %s',$text->type)); + throw new SlackSyntaxException(sprintf('Text must be plain_text not %s',$text->type)); if (strlen($text->text) > self::LIMITS['submit']) - throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['submit'])); + throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['submit'])); $this->submit = $text; @@ -194,24 +193,10 @@ final class Modal extends BlockKit public function submit_disabled(bool $bool): self { if ($this->type != 'modal') - throw new Exception(sprintf('submit_disabled is not required for %s',$type)); + throw new SlackSyntaxException(sprintf('submit_disabled is not required for %s',$this->type)); $this->submit_disabled = $bool; return $this; } - - /** - * Add a block to the modal - * - * @param Blocks $blocks - * @return $this - * @deprecated use addBlock() instead - */ - public function setBlocks(Blocks $blocks): self - { - $this->_data->put('blocks',$blocks); - - return $this; - } } \ No newline at end of file diff --git a/src/Console/Commands/SlackSocketClient.php b/src/Console/Commands/SlackSocketClient.php index fbc42fc..ddaa163 100644 --- a/src/Console/Commands/SlackSocketClient.php +++ b/src/Console/Commands/SlackSocketClient.php @@ -9,6 +9,7 @@ use React\EventLoop\Loop; use Slack\Client\SocketMode; use Slack\Command\Factory as SlackCommandFactory; use Slack\Event\Factory as SlackEventFactory; +use Slack\Exceptions\SlackException; use Slack\Interactive\Factory as SlackInteractiveFactory; class SlackSocketClient extends Command @@ -33,13 +34,13 @@ class SlackSocketClient extends Command * Execute the console command. * * @return void - * @throws \Exception + * @throws SlackException */ public function handle() { // Make sure our socket_token is defined if (! config('slack.socket_token')) - throw new \Exception('SocketMode Client Token not defined.'); + throw new SlackException('SocketMode Client Token not defined.'); $loop = Loop::get(); diff --git a/src/Exceptions/SlackSyntaxException.php b/src/Exceptions/SlackSyntaxException.php new file mode 100644 index 0000000..b92b0c5 --- /dev/null +++ b/src/Exceptions/SlackSyntaxException.php @@ -0,0 +1,7 @@ +container_type); + throw new \Exception('Unknown container type: '.$this->container_type); } case 'container_type': @@ -143,7 +142,7 @@ final class BlockActions extends Base // @todo To Check case 'overflow': // @todo To Check - throw new SlackException('To be implemented: ',$x); + throw new \Exception('To be implemented: ',$x); case 'static_select': return count(object_get($this->action,'selected_option')); case 'multi_static_select': @@ -162,7 +161,7 @@ final class BlockActions extends Base * * @param string $key * @return string|null - * @throws SlackException + * @throws \Exception */ private function action(string $key): ?string { @@ -185,7 +184,7 @@ final class BlockActions extends Base return $value; default: - throw new SlackException('Unknown key: '.$key); + throw new \Exception('Unknown key: '.$key); } } diff --git a/src/Interactive/ViewSubmission.php b/src/Interactive/ViewSubmission.php index ee41176..c332c11 100644 --- a/src/Interactive/ViewSubmission.php +++ b/src/Interactive/ViewSubmission.php @@ -2,13 +2,9 @@ namespace Slack\Interactive; -use Illuminate\Support\Arr; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Log; use Slack\Blockkit\Modal; -use Slack\Exceptions\SlackException; -use Slack\Models\Team; /** * Incoming modal view submission event. @@ -56,7 +52,7 @@ class ViewSubmission extends Base * * @param string $key * @return string|null - * @throws SlackException + * @throws \Exception */ private function callback(string $key): ?string { @@ -76,7 +72,7 @@ class ViewSubmission extends Base return $value; default: - throw new SlackException('Unknown key: '.$key); + throw new \Exception('Unknown key: '.$key); } } @@ -84,7 +80,6 @@ class ViewSubmission extends Base * This method should be overridden by a local implementation * * @return Modal - * @throws \Exception */ public function respond(): Modal { diff --git a/src/Jobs/DeleteResponse.php b/src/Jobs/DeleteResponse.php index 5b7794d..ffe70b9 100644 --- a/src/Jobs/DeleteResponse.php +++ b/src/Jobs/DeleteResponse.php @@ -15,7 +15,6 @@ final class DeleteResponse extends Job * Create a new job instance. * * @param string $url - * @throws \Exception */ public function __construct(string $url) { @@ -26,7 +25,6 @@ final class DeleteResponse extends Job * Execute the job. * * @return void - * @throws \Exception */ public function handle() { diff --git a/src/Message.php b/src/Message.php index 8e27917..82df0bd 100644 --- a/src/Message.php +++ b/src/Message.php @@ -12,8 +12,8 @@ use Illuminate\Support\Facades\Log; use Slack\Blockkit\Blocks; use Slack\Blockkit\Blocks\Context; use Slack\Blockkit\Blocks\Elements\Text; -use Slack\Exceptions\SlackException; -use Slack\Jobs\DeleteChat; +use Slack\Exceptions\{SlackException,SlackSyntaxException}; +use Slack\Jobs\{DeleteChat,DeleteResponse}; use Slack\Message\Attachment; use Slack\Models\{Channel,User}; use Slack\Response\Generic; @@ -34,7 +34,7 @@ final class Message extends BlockKit * Message constructor. * * @param Model|null $o Who the message will be to - Channel or User - * @throws SlackException + * @throws \Exception */ public function __construct(Model $o=NULL) { @@ -50,7 +50,7 @@ final class Message extends BlockKit $this->setUser($o); } else { - throw new SlackException('Model not handled: '.get_class($o)); + throw new \Exception('Model not handled: '.get_class($o)); } $this->o = $o; @@ -72,9 +72,9 @@ final class Message extends BlockKit /** * Add a block to the message * - * @param Blocks $blocks + * @param Attachment $attachment * @return Message - * @todo to test + * @throws SlackSyntaxException */ public function addAttachment(Attachment $attachment): self { @@ -84,7 +84,7 @@ final class Message extends BlockKit $this->attachments->push($attachment); if (count($this->attachments) > self::MAX_ATTACHMENTS) - throw new Exception(sprintf('Messages should not have more than %d attachments',self::MAX_ATTACHMENTS)); + throw new SlackSyntaxException(sprintf('Messages should not have more than %d attachments',self::MAX_ATTACHMENTS)); return $this; } @@ -133,7 +133,7 @@ final class Message extends BlockKit */ public function isEmpty(): bool { - return $this->jsonSerialize() ? FALSE : TRUE; + return ! $this->jsonSerialize(); } /** @@ -157,14 +157,17 @@ final class Message extends BlockKit if (! $delete && $this->selfdestruct) $delete = $this->selfdestruct; - if ($this->_data->has('ephemeral')) - abort('500','Cannot post ephemeral messages.'); - if ($this->_data->get('blocks') && $this->_data->get('attachments')) - throw new SlackException('Message cannot have blocks and attachments.'); + throw new SlackSyntaxException('Message cannot have blocks and attachments.'); $api = $this->o->team->slackAPI(); - $response = $this->_data->has('ts') ? $api->updateMessage($this) : $api->postMessage($this); + + if ($this->ephemeral) { + $response = $api->postEphemeral($this); + + } else { + $response = $this->_data->has('ts') ? $api->updateMessage($this) : $api->postMessage($this); + } if ($delete) { Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$delete->format('Y-m-d')),['m'=>__METHOD__]); @@ -181,11 +184,15 @@ final class Message extends BlockKit * * @note This URL can only be used 5 times in 30 minutes * @param string $url + * @param Carbon|null $delete * @return string * @throws SlackException */ - public function respond(string $url) + public function respond(string $url,Carbon $delete=NULL) { + if (! $delete && $this->selfdestruct) + $delete = $this->selfdestruct; + $request = curl_init(); curl_setopt($request,CURLOPT_URL,$url); @@ -216,6 +223,13 @@ final class Message extends BlockKit } } + if ($delete) { + Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$delete->format('Y-m-d')),['m'=>__METHOD__]); + + // Queue the delete of the response if requested + dispatch((new DeleteResponse($url))->onQueue('slack')->delay($delete)); + } + curl_close($request); return $result; } @@ -251,6 +265,7 @@ final class Message extends BlockKit ); $this->selfdestruct = $time; + return $this; } @@ -286,7 +301,6 @@ final class Message extends BlockKit /** * @return Message - * @todo - Check this is a valid option */ public function ephemeral(): self { @@ -323,8 +337,10 @@ final class Message extends BlockKit } /** + * Replace the original message + * + * @param bool $bool * @return Message - * @todo - Check this is a valid option */ public function replace_original(bool $bool=TRUE): self { @@ -391,78 +407,4 @@ final class Message extends BlockKit return $this; } - - /** - * Add an attachment to a message - * - * @param Attachment $attachments - * @return Message - * @deprecated use addAttachment() - */ - public function setAttachments(Attachment $attachments): self - { - $this->_data->put('attachments',[$attachments]); - - return $this; - } - - /** - * Add blocks to the message - * - * @param Blocks $blocks - * @return Message - * @throws \Exception - * @deprecated use addBlocks() - */ - public function setBlocks(Blocks $blocks): self - { - if ($this->blocks->count()) - throw new \Exception('Blocks already defined'); - - $this->blocks = $blocks; - - return $this; - } - - /** - * Message text - * - * @param string $string - * @return Message - * @deprecated use text() - */ - public function setText(string $string): self - { - $this->text = $string; - - return $this; - } - - /** - * Set the timestamp, used when replacing messages - * - * @param string $string - * @return Message - * @deprecated use ts() - */ - public function setTS(string $string): self - { - $this->_data->put('ts',$string); - - return $this; - } - - /** - * Set the thread timestamp, used when adding a threaded response - * - * @param string $string - * @return Message - * @deprecated use thead_ts() - */ - public function setThreadTS(string $string): self - { - $this->_data->put('thread_ts',$string); - - return $this; - } } \ No newline at end of file diff --git a/src/Message/Attachment.php b/src/Message/Attachment.php index 9359dc4..267cac7 100644 --- a/src/Message/Attachment.php +++ b/src/Message/Attachment.php @@ -2,7 +2,6 @@ namespace Slack\Message; -use \Exception; use Illuminate\Support\Arr; use Slack\BlockKit; diff --git a/src/Message/AttachmentAction.php b/src/Message/AttachmentAction.php index a7bf55b..f2bab2b 100644 --- a/src/Message/AttachmentAction.php +++ b/src/Message/AttachmentAction.php @@ -4,6 +4,7 @@ namespace Slack\Message; use Slack\BlockKit; use Slack\Blockkit\Blocks\Elements\Text; +use Slack\Exceptions\SlackSyntaxException; /** * Class MessageAttachmentAction - Slack Message Attachments Actions @@ -33,7 +34,7 @@ final class AttachmentAction extends BlockKit parent::__construct(); if (! in_array($type,self::TYPES)) - throw new Exception(sprintf('Type [%s] not valid',$type)); + throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type)); $this->type = $type; switch ($type) { @@ -80,7 +81,7 @@ final class AttachmentAction extends BlockKit public function data_source(string $string): self { if (! in_array($string,self::DATA_SOURCES)) - throw new Exception(sprintf('Type [%s] not valid',$string)); + throw new SlackSyntaxException(sprintf('Type [%s] not valid',$string)); $this->data_source = $string; @@ -97,13 +98,14 @@ final class AttachmentAction extends BlockKit /** * Set the text displayed in the action * - * @param string $type + * @param string $string * @return $this + * @throws SlackSyntaxException */ public function style(string $string): self { if (! in_array($string,self::STYLES)) - throw new Exception(sprintf('Type [%s] not valid',$string)); + throw new SlackSyntaxException(sprintf('Type [%s] not valid',$string)); $this->style = $string; @@ -115,6 +117,7 @@ final class AttachmentAction extends BlockKit * * @param string $string * @return $this + * @throws SlackSyntaxException */ public function value(string $string): self { diff --git a/src/Options/Base.php b/src/Options/Base.php index 09e3176..58b897f 100644 --- a/src/Options/Base.php +++ b/src/Options/Base.php @@ -61,7 +61,6 @@ abstract class Base extends SlackBase * This function should be overwritten in the parent class, and finish by calling return Message::blank(); * * @return Message - * @throws \Exception */ abstract public function respond(): Message; } \ No newline at end of file diff --git a/src/Options/Unknown.php b/src/Options/Unknown.php index 30329a6..1e77a9c 100644 --- a/src/Options/Unknown.php +++ b/src/Options/Unknown.php @@ -26,7 +26,6 @@ final class Unknown extends Base * Interactive messages can return their output in the incoming HTTP post * * @return Message - * @throws \Exception */ public function respond(): Message {