From 282d52d70195283eb1b92e59d7bdc2d525361f4b Mon Sep 17 00:00:00 2001 From: Mark Hoekstra Date: Fri, 29 Nov 2024 15:59:32 +0100 Subject: [PATCH] [3.x] Add the ability to get the Block Kit Builder url from a SlackMessage (#100) * Add the ability to get the Block Kit Builder url from a SlackMessage * Test the ability to get the Block Kit Builder url from a SlackMessage * Fix styleci error in SlackMessageTest * formatting --------- Co-authored-by: Mark Hoekstra Co-authored-by: Taylor Otwell --- src/Slack/SlackMessage.php | 12 ++++++++++- tests/Slack/Feature/SlackMessageTest.php | 27 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Slack/SlackMessage.php b/src/Slack/SlackMessage.php index f190aef..acc3efa 100644 --- a/src/Slack/SlackMessage.php +++ b/src/Slack/SlackMessage.php @@ -320,6 +320,16 @@ public function toArray(): array ], $optionalFields); } + /** + * Get the Block Kit URL for the message. + * + * @return string + */ + public function toBlockKitBuilderUrl(): string + { + return 'https://app.slack.com/block-kit-builder#'.rawurlencode(json_encode(Arr::except($this->toArray(), ['username', 'text', 'channel']), true)); + } + /** * Dump the payload as a URL to the Slack Block Kit Builder. * @@ -331,6 +341,6 @@ public function dd(bool $raw = false) dd($this->toArray()); } - dd('https://app.slack.com/block-kit-builder#'.rawurlencode(json_encode(Arr::except($this->toArray(), ['username', 'text', 'channel']), true))); + dd($this->toBlockKitBuilderUrl()); } } diff --git a/tests/Slack/Feature/SlackMessageTest.php b/tests/Slack/Feature/SlackMessageTest.php index 0b90c99..e4fa421 100644 --- a/tests/Slack/Feature/SlackMessageTest.php +++ b/tests/Slack/Feature/SlackMessageTest.php @@ -573,4 +573,31 @@ public function it_can_combined_block_kit_template_and_block_contract_in_order() ], ]); } + + /** @test */ + public function it_can_return_an_block_kit_builder_url() + { + $message = (new SlackChannelTestNotification(function (SlackMessage $message) { + $message + ->username('larabot') + ->to('#ghost-talk') + ->headerBlock('Budget Performance') + ->sectionBlock(function (SectionBlock $sectionBlock) { + $sectionBlock->text('A message *with some bold text* and _some italicized text_.')->markdown(); + }); + }))->toSlack( + new SlackChannelTestNotifiable(new SlackRoute('#ghost-talk', 'fake-token')) + ); + + $returnedUrl = $message->toBlockKitBuilderUrl(); + $expectedUrl = 'https://app.slack.com/block-kit-builder#' + .rawurlencode('{"blocks":[{"type":"header","text":{"type":"plain_text","text":"Budget Performance"}},{"type":"section","text":{"type":"mrkdwn","text":"A message *with some bold text* and _some italicized text_."}}]}'); + + $this->assertStringNotContainsStringIgnoringCase('username', $returnedUrl); + $this->assertStringNotContainsStringIgnoringCase('larabot', $returnedUrl); + $this->assertStringNotContainsStringIgnoringCase('channel', $returnedUrl); + $this->assertStringNotContainsStringIgnoringCase('#ghost-talk', $returnedUrl); + + $this->assertEquals($expectedUrl, $returnedUrl); + } }