Skip to content

Commit

Permalink
Updated Slack Block Kit api to new format
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnesselr committed Dec 23, 2024
1 parent 06b7c56 commit 1adc3fc
Show file tree
Hide file tree
Showing 22 changed files with 587 additions and 506 deletions.
25 changes: 16 additions & 9 deletions app/Actions/Slack/UpdateSpaceBotAppHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class UpdateSpaceBotAppHome
public function __construct(SlackApi $slackApi)
{
$this->slackApi = $slackApi;
$this->home = Kit::newAppHome();
$this->home = Kit::appHome();
}

/**
Expand All @@ -36,18 +36,25 @@ public function execute(string $slack_id): void
$member = Customer::whereSlackId($slack_id)->first();

if (is_null($member)) {
$this->home->text(CommonResponses::unrecognizedUser());
$this->home->blocks(
Kit::section(
text: Kit::plainText(CommonResponses::unrecognizedUser()),
),
);
} elseif (! $member->member) {
$this->home->text(CommonResponses::notAMemberInGoodStanding());
$this->home->blocks(
Kit::section(
text: Kit::plainText(CommonResponses::notAMemberInGoodStanding()),
),
);
} else {
$this->activeMember($member);
$this->home->blocks(
Kit::section(
text: Kit::plainText("You're an active member! Thank you for being part of denhac!"),
),
);
}

$this->slackApi->views->publish($slack_id, $this->home);
}

private function activeMember(Customer $member)
{
$this->home->text("You're an active member! Thank you for being part of denhac!");
}
}
71 changes: 47 additions & 24 deletions app/External/Slack/Modals/CountdownTestModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Actions\CountdownModalLoop;
use App\External\Slack\BlockActions\RespondsToBlockActions;
use App\Http\Requests\SlackRequest;
use SlackPhp\BlockKit\Elements\ButtonStyle;
use SlackPhp\BlockKit\Kit;
use SlackPhp\BlockKit\Surfaces\Modal;

Expand All @@ -19,32 +20,52 @@ class CountdownTestModal implements ModalInterface

public function __construct(?int $timeLeft)
{
$this->modalView = Kit::newModal()
->callbackId(self::callbackId())
->title('Countdown Test')
->clearOnClose(true)
->clearOnClose('Close');

$this->modalView->newSection()
->mrkdwnText('This is a test to see how responsive something like a countdown is.');
$this->modalView = Kit::modal(
title: 'Countdown Test',
callbackId: self::callbackId(),
close: 'Close',
clearOnClose: true,
blocks: [
Kit::section(
text: 'This is a test to see how responsive something like a countdown is.'
)
],
);

if (is_null($timeLeft)) {
$this->modalView->newSection()
->mrkdwnText('The countdown has not yet started. Press the button to start.');

$this->modalView->newActions(self::START_COUNTDOWN)
->newButton(self::START_COUNTDOWN)
->asPrimary()
->text('Start Countdown');
$this->modalView->blocks(
Kit::section(
text: 'The countdown has not yet started. Press the button to start.'
),
Kit::actions(
elements: [
Kit::button(
actionId: self::START_COUNTDOWN,
text: 'Start Countdown',
style: ButtonStyle::PRIMARY,
)
],
blockId: self::START_COUNTDOWN
),
);
} elseif ($timeLeft == -1) {
$this->modalView->newSection()
->mrkdwnText('Countdown hopefully started!');
$this->modalView->blocks(
Kit::section(
text: 'Countdown hopefully started!'
),
);
} elseif ($timeLeft > 0) {
$this->modalView->newSection()
->mrkdwnText("The countdown has {$timeLeft} seconds left.");
$this->modalView->blocks(
Kit::section(
text: "The countdown has {$timeLeft} seconds left."
),
);
} else {
$this->modalView->newSection()
->mrkdwnText('The countdown is over! Thanks for testing!');
$this->modalView->blocks(
Kit::section(
text: 'The countdown is over! Thanks for testing!'
),
);
}
}

Expand All @@ -58,9 +79,11 @@ public static function handle(SlackRequest $request)
return self::clearViewStack();
}

public static function getOptions(SlackRequest $request) {}
public static function getOptions(SlackRequest $request)
{
}

public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->modalView->jsonSerialize();
}
Expand All @@ -72,7 +95,7 @@ public static function getBlockActions(): array
];
}

public static function onBlockAction(SlackRequest $request)
public static function onBlockAction(SlackRequest $request): CountdownTestModal
{
$viewId = $request->payload()['view']['id'];
app(CountdownModalLoop::class)
Expand Down
148 changes: 80 additions & 68 deletions app/External/Slack/Modals/CreateTrainableEquipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,76 +31,88 @@ class CreateTrainableEquipment implements ModalInterface

public function __construct(?Customer $user)
{
$this->modalView = Kit::newModal()
->callbackId(self::callbackId())
->title('New Trainable Equipment')
->clearOnClose(true)
->close('Cancel')
->submit('Submit');

$this->modalView->newInput()
->label('Equipment Name')
->blockId(self::EQUIPMENT_NAME)
->newTextInput()
->actionId(self::EQUIPMENT_NAME)
->placeholder('Name');

$trainerInput = $this->modalView->newInput()
->label('Initial Trainer')
->blockId(self::INITIAL_TRAINER)
->newSelectMenu()
->forExternalOptions()
->actionId(self::INITIAL_TRAINER)
->placeholder('Select a customer')
->minQueryLength(0);

if (! is_null($user)) {
$name = "{$user->first_name} {$user->last_name}";
$trainerInput->initialOption($name, "customer-{$user->id}");
}

$this->modalView->divider();

$this->modalView->header('Slack Channels & Email Groups');

$this->modalView->newContext()
->mrkdwnText(
'Users/trainers will be automatically added to these channels/emails. All are optional. '.
"Email must be an existing group, for now. Please ask in #general and we'll help make one ".
'if needed.'
$initialTrainerOption = Kit::option(
text: $name,
value: "customer-{$user->id}"
);
} else {
$initialTrainerOption = null;
}

$this->modalView->newInput()
->label('User slack channel')
->blockId(self::USER_SLACK_CHANNEL)
->optional(true)
->newSelectMenu()
->forChannels()
->placeholder('Select a channel')
->actionId(self::USER_SLACK_CHANNEL);

$this->modalView->newInput()
->label('User email')
->blockId(self::USER_EMAIL)
->optional(true)
->newTextInput()
->actionId(self::USER_EMAIL);

$this->modalView->newInput()
->label('Trainer slack channel')
->blockId(self::TRAINER_SLACK_CHANNEL)
->optional(true)
->newSelectMenu()
->forChannels()
->placeholder('Select a channel')
->actionId(self::TRAINER_SLACK_CHANNEL);

$this->modalView->newInput()
->label('Trainer email')
->blockId(self::TRAINER_EMAIL)
->optional(true)
->newTextInput()
->actionId(self::TRAINER_EMAIL);
$this->modalView = Kit::modal(
title: 'New Trainable Equipment',
callbackId: self::callbackId(),
clearOnClose: true,
close: 'Close',
submit: 'Submit',
blocks: [
Kit::input(
label: 'Equipment Name',
blockId: self::EQUIPMENT_NAME,
element: Kit::plainTextInput(
actionId: self::EQUIPMENT_NAME,
placeholder: 'Name',
),
),
Kit::input(
label: 'Initial Trainer',
blockId: self::INITIAL_TRAINER,
element: Kit::externalSelectMenu(
actionId: self::INITIAL_TRAINER,
placeholder: 'Select a customer',
initialOption: $initialTrainerOption,
minQueryLength: 0,
),
),
Kit::divider(),
Kit::header('Slack Channels & Email Groups'),
Kit::context(
elements: [
Kit::plainText(
text: 'Users/trainers will be automatically added to these channels/emails. All are ' .
'optional. Email must be an existing group, for now. Please ask in #general and we\'ll ' .
'help make one if needed.'
),
],
),
Kit::input(
label: 'User slack channel',
blockId: self::USER_SLACK_CHANNEL,
optional: true,
element: Kit::channelSelectMenu(
actionId: self::USER_SLACK_CHANNEL,
placeholder: 'Select a channel',
),
),
Kit::input(
label: 'User email',
blockId: self::USER_EMAIL,
optional: true,
element: Kit::plainTextInput(
actionId: self::USER_EMAIL,
),
),
Kit::input(
label: 'Trainer slack channel',
blockId: self::TRAINER_SLACK_CHANNEL,
optional: true,
element: Kit::channelSelectMenu(
actionId: self::TRAINER_SLACK_CHANNEL,
placeholder: 'Select a channel',
),
),
Kit::input(
label: 'Trainer email',
blockId: self::TRAINER_EMAIL,
optional: true,
element: Kit::plainTextInput(
actionId: self::TRAINER_EMAIL,
),
),
],
);
}

public static function callbackId(): string
Expand All @@ -111,7 +123,7 @@ public static function callbackId(): string
public static function handle(SlackRequest $request)
{
if (! $request->customer()->hasMembership(UserMembership::MEMBERSHIP_META_TRAINER)) {
Log::warning('CreateTrainableEquipment: Rejecting unauthorized submission from user '.$request->customer()->id);
Log::warning('CreateTrainableEquipment: Rejecting unauthorized submission from user ' . $request->customer()->id);
throw new \Exception('Unauthorized');
}

Expand Down Expand Up @@ -176,7 +188,7 @@ public static function getOptions(SlackRequest $request)
return SelectAMemberModal::getOptions($request);
}

public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->modalView->jsonSerialize();
}
Expand Down
Loading

0 comments on commit 1adc3fc

Please sign in to comment.