Skip to content

Commit

Permalink
[3.x] Add the ability to get the Block Kit Builder url from a SlackMe…
Browse files Browse the repository at this point in the history
…ssage (#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 <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Nov 29, 2024
1 parent cd3e120 commit 282d52d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Slack/SlackMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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());
}
}
27 changes: 27 additions & 0 deletions tests/Slack/Feature/SlackMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 282d52d

Please sign in to comment.