Fixes for interactive messages, we use block_id to determine what needs to be done

This commit is contained in:
Deon George 2022-09-06 17:23:54 +10:00
parent 1b55d7ab52
commit 145e322317
7 changed files with 58 additions and 19 deletions

View File

@ -65,15 +65,18 @@ abstract class Base
}
/**
* Separate out an callback command to the id that the command relates to
* Separate out a callback command to the id that the command relates to
*
* @param string $key
* @param string $item
* @param string|null $item
* @return string|null
* @throws \Exception
*/
final protected function keyitem(string $key,string $item): ?string
final protected function keyitem(string $key,string $item=NULL): ?string
{
if (! $item)
return $item;
$regex = '/^([a-z_]+)\|([0-9]+)$/';
$id = NULL;
$value = NULL;

View File

@ -11,9 +11,9 @@ use Slack\Exceptions\SlackSyntaxException;
* Class BlockKit - Slack Blockit Objects
*
* @notes
* + callback_id is used to identify the source of any action (modal). (Message blocks do not have a callback_id, accept in attachments).
* + callback_id is used to identify the source of any action (modal). (Message blocks do not have a callback_id, accept in legacy attachments).
* eg: hometab, add_product, ask_modal
* + block_id is used to identify the sub action(s) of any action (modal)
* + block_id is used to identify the sub action(s) of any action (modal). (Messages with blocks can have a block_id, we need to use this to determine what to do.)
* eg: multiple blocks (list of something)
* + action_id is used to identify the action that was initiated
* eg: view, edit, review

View File

@ -3,18 +3,51 @@
namespace Slack\Blockkit\Blocks\Elements;
use Slack\Blockkit\Element;
use Slack\Exceptions\SlackSyntaxException;
final class Confirm extends Element
{
public function __construct()
protected const LIMITS = [
'title' => 100,
'text' => 300,
'confirm' => 30,
'deny' => 30,
];
public function __construct(Text $title,Text $text,Text $confirm,Text $deny)
{
parent::__construct();
abort(500,'Not Implememted');
if ($title->type != 'plain_text')
throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$title->type));
if ($confirm->type != 'plain_text')
throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$confirm->type));
if ($deny->type != 'plain_text')
throw new SlackSyntaxException(sprintf('Title must be plain_text not %s',$deny->type));
$this->title = $this->validate('title',$title->text) ? $title : NULL;
$this->text = $this->validate('text',$text->text) ? $text : NULL;
$this->confirm = $this->validate('confirm',$confirm->text) ? $confirm : NULL;
$this->deny = $this->validate('deny',$deny->text) ? $deny : NULL;
}
public static function item(): self
public static function item(Text $title,Text $text,Text $confirm,Text $deny): self
{
return new self();
return new self($title,$text,$confirm,$deny);
}
public function style(string $string): self
{
if (! in_array($string,['default','primary','danger']))
throw new SlackSyntaxException(sprintf('Unknown style %s',$string));
$this->style = $string;
return $this;
}
}

View File

@ -64,20 +64,25 @@ final class BlockActions extends Base
case 'action':
return Arr::get(object_get($this->_data,'actions'),$this->index);
case 'action_id':
return object_get(Arr::get($this->actions,$this->index),$key);
// An event can have more than 1 action, each action can have 1 value.
case 'action_key':
return $this->keyitem('id',object_get($this->action,'action_id'));
return $this->keyitem('id',$this->action_id);
case 'action_value':
return $this->keyitem('value',object_get($this->action,'action_id'));
return $this->keyitem('value',$this->action_id);
// Interactive Messages have a block_id.
case 'block_key':
return $this->keyitem('id',$this->block_id);
case 'block_value':
return $this->keyitem('value',$this->block_id);
case 'actions':
case 'response_url':
return object_get($this->_data,$key);
case 'action_id':
case 'block_id':
return object_get($this->action,$key);

View File

@ -165,10 +165,10 @@ final class Message extends BlockKit
if ($this->_data->get('blocks') && $this->_data->get('attachments'))
throw new SlackSyntaxException('Message cannot have blocks and attachments.');
if ((! isset($this->o)) || (! $this->o->team))
if ((! isset($this->o)) || ((! $this->o->team) && (! $this->o->user_team)))
throw new SlackSyntaxException('Message needs to have a user or a channel to work out the team.');
$api = $this->o->team->slackAPI();
$api = $this->o->team ? $this->o->team->slackAPI() : $this->o->user_team->slackAPI();
if ($this->ephemeral) {
$response = $api->postEphemeral($this);

View File

@ -8,7 +8,7 @@ use Slack\Exceptions\SlackSyntaxException;
/**
* Class MessageAttachmentAction - Slack Message Attachments Actions
* Represents an Single Action for a Slack Message Attachment
* Represents a Single Action for a Slack Message Attachment
*
* @package App\Slack\Message
* @note These are now legacy - use blocks instead

View File

@ -61,8 +61,6 @@ class User extends Model
*/
public function getUserTeamAttribute(): ?Team
{
Log::debug(sprintf('%s:User [%s], Team [%s], Enterprise [%s]',self::LOGKEY,$this->id,$this->team_id,$this->enterprise_id),['m'=>__METHOD__]);
return $this->team_id ? $this->team : (($x=$this->enterprise->teams) ? $x->first() : NULL);
}