Implemented SlackSyntaxException and removed some deprecated functions

This commit is contained in:
Deon George 2022-09-04 12:18:11 +10:00
parent 4c7d18c6b0
commit 4ff944cb3a
28 changed files with 169 additions and 238 deletions

View File

@ -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 {

View File

@ -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;
}

View File

@ -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')];

View File

@ -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

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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; });

View File

@ -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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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();

View File

@ -0,0 +1,7 @@
<?php
namespace Slack\Exceptions;
class SlackSyntaxException extends SlackException
{
}

View File

@ -18,7 +18,6 @@ final class InteractiveOptionsController extends Controller
*
* @param Request $request
* @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory
* @throws \Exception
*/
public function fire(Request $request)
{

View File

@ -5,7 +5,6 @@ namespace Slack\Interactive;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Slack\Exceptions\SlackException;
use Slack\Models\Channel;
/**
@ -93,7 +92,7 @@ final class BlockActions extends Base
return NULL;
default:
throw new SlackException('Unknown container type: '.$this->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);
}
}

View File

@ -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
{

View File

@ -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()
{

View File

@ -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;
}
}

View File

@ -2,7 +2,6 @@
namespace Slack\Message;
use \Exception;
use Illuminate\Support\Arr;
use Slack\BlockKit;

View File

@ -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
{

View File

@ -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;
}

View File

@ -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
{