added Message/AttachmentAction

This commit is contained in:
Deon George 2022-02-24 09:00:45 +11:00
parent 3cadf2b808
commit dbd355555b
4 changed files with 140 additions and 15 deletions

View File

@ -69,9 +69,12 @@ final class PlaintTextInput extends Element
return $this;
}
public function placeholder(string $text): self
public function placeholder(Text $text): self
{
$this->placeholder = $this->validate('placeholder',$text);
if (strlen($text->text) > self::LIMITS['placeholder'])
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
$this->placeholder = $text;
return $this;
}

View File

@ -98,6 +98,13 @@ final class Message extends BlockKit
return $this;
}
public function clearAttachments(): self
{
$this->attachments = '';
return $this;
}
public function forgetTS(): self
{
$this->_data->forget('ts');

View File

@ -33,22 +33,12 @@ final class Attachment extends BlockKit
* @note These are now legacy - use blocks instead
* @note more information on this available https://api.slack.com/legacy/interactive-message-field-guide
*/
public function addAction(string $name,string $text,string $type,string $value,string $style=NULL,array $confirm=NULL): self
public function addAction(AttachmentAction $action): self
{
if (! Arr::get($this->_data,'actions')) {
if (! Arr::get($this->_data,'actions'))
$this->actions = collect();
$this->attachment_type = 'default';
}
$action = ['name'=>$name,'text'=>$text,'type'=>$type,'value'=>$value];
if ($style)
$action['style'] = $style;
// Confirm dialog is an array of 'title','text','ok_text','dismiss_text'
if ($confirm)
$action['confirm'] = $confirm;
$this->actions->push();
$this->actions->push($action);
return $this;
}

View File

@ -0,0 +1,125 @@
<?php
namespace Slack\Message;
use Slack\BlockKit;
use Slack\Blockkit\Blocks\Elements\Text;
/**
* Class MessageAttachmentAction - Slack Message Attachments Actions
* Represents an Single Action for a Slack Message Attachment
*
* @package App\Slack\Message
* @note These are now legacy - use blocks instead
* @note more information on this available https://api.slack.com/legacy/interactive-message-field-guide
*/
final class AttachmentAction extends BlockKit
{
protected const LIMITS = [
'text' => 30,
'value' => 2000,
];
private const DATA_SOURCES = ['static','users','channels','conversastions','external'];
private const STYLES = ['default','danger','primary'];
private const TYPES = ['button','select'];
// @todo options
// @todo option_groups
// @todo selected_options
public function __construct(string $text,string $name,string $type)
{
parent::__construct();
if (! in_array($type,self::TYPES))
throw new Exception(sprintf('Type [%s] not valid',$type));
$this->type = $type;
switch ($type) {
case 'button':
$this->action_id = $name;
$this->text = Text::item($this->validate('text',$text),'plain_text');
break;
case 'select':
$this->name = $name;
$this->text = $this->validate('text',$text);
break;
}
}
public static function item(string $text,string $name,string $type): self
{
return new self($text,$name,$type);
}
/* OPTIONAL ITEMS */
/**
* Set a confirmation diaglog when this action is selected
*
* @param string $title
* @param string $text
* @param string $ok_text
* @param string $dismiss_text
* @return $this
*/
public function confirm(string $title,string $text,string $ok_text,string $dismiss_text): self
{
$this->confirm = [
'title' => $title,
'text' => $text,
'ok_text' => $ok_text,
'dismiss_text' => $dismiss_text
];
return $this;
}
public function data_source(string $string): self
{
if (! in_array($string,self::DATA_SOURCES))
throw new Exception(sprintf('Type [%s] not valid',$string));
$this->data_source = $string;
return $this;
}
public function min_query_length(int $int): self
{
$this->min_query_length = $int;
return $this;
}
/**
* Set the text displayed in the action
*
* @param string $type
* @return $this
*/
public function style(string $string): self
{
if (! in_array($string,self::STYLES))
throw new Exception(sprintf('Type [%s] not valid',$string));
$this->style = $string;
return $this;
}
/**
* Set the value for the action
*
* @param string $string
* @return $this
*/
public function value(string $string): self
{
$this->value = $this->validate('value',$string);
return $this;
}
}