laravel / slack-notification-channel

Slack Notification Channel for laravel.
https://laravel.com/docs/notifications#slack-notifications
MIT License
864 stars 59 forks source link

No way to disable unfurl_links or unfurl_media #78

Closed john-f-chamberlain closed 10 months ago

john-f-chamberlain commented 10 months ago

Slack Notification Channel Version

3.0.1

Laravel Version

10.2.8

PHP Version

8.1

Database Driver & Version

No response

Description

There is currently no way to set unfurl_links or unfurl_media to false. The only options currently are null (default) or true (by executing the unfurlLinks or unfurlMedia methods.

Unless specifically set to false, Slack will enable unfurl for links and media.

Steps To Reproduce

  1. Create a notification with a link displayed anywhere within the body, for example:
[...]
    public function toSlack(object $notifiable): SlackMessage
    {
        return (new SlackMessage)
            ->text("Test Notification")
            ->headerBlock('Test Notification')
            ->sectionBlock(function (SectionBlock $block) {
                $block->field("*Domain*\n<https://www.google.com/|www.google.com>")->markdown();
            });
    }
[...]
  1. Trigger the notification
  2. Notice that the notification includes the link preview even though you didn't execute the unfurlLinks method.
john-f-chamberlain commented 10 months ago

Documentation that states to disable link unfurling you must pass "false": https://api.slack.com/reference/messaging/link-unfurling#no_unfurling_please

john-f-chamberlain commented 10 months ago

Possible solution: Modify https://github.com/laravel/slack-notification-channel/blob/6ad5a0cbcefce89e917f13b7f7b866d882af6380/src/Slack/SlackMessage.php#L214-L219

To Be:

    public function unfurlLinks(bool $unfurl = true): self
    {
        $this->unfurlLinks = $unfurl;

        return $this;
    }

and modify https://github.com/laravel/slack-notification-channel/blob/6ad5a0cbcefce89e917f13b7f7b866d882af6380/src/Slack/SlackMessage.php#L224-L229

To Be:

    public function unfurlMedia(bool $unfurl = true): self
    {
        $this->unfurlMedia = $unfurl;

        return $this;
    }

This would allow you to set unfurl_media and unfurl_links to false and prevent link and media previews from being generated in Slack.

driesvints commented 10 months ago

Think you're looking at the wrong place as the notification class has the appropriate messages to set the boolean for these: https://github.com/laravel/slack-notification-channel/blob/6ad5a0cbcefce89e917f13b7f7b866d882af6380/src/Messages/SlackMessage.php#L240-L245

This is also the class that's documented.

john-f-chamberlain commented 10 months ago

Thank you! My apologies, I didn't realize my IDE autocompleted the wrong class! That would also explain why other methods were missing 🤦‍♂️

john-f-chamberlain commented 10 months ago

@driesvints Actually, this doesn't appear to be the class that's in the official documentation. The official documentation uses the class that I originally used: \Illuminate\Notifications\Slack\SlackMessage, not \Illuminate\Notifications\Messages\SlackMessage

https://laravel.com/docs/10.x/notifications#formatting-slack-notifications

The full example provided in the documentation is:

use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\BlockKit\Composites\ConfirmObject;
use Illuminate\Notifications\Slack\SlackMessage;

/**
 * Get the Slack representation of the notification.
 */
public function toSlack(object $notifiable): SlackMessage
{
    return (new SlackMessage)
            ->text('One of your invoices has been paid!')
            ->headerBlock('Invoice Paid')
            ->contextBlock(function (ContextBlock $block) {
                $block->text('Customer #1234');
            })
            ->sectionBlock(function (SectionBlock $block) {
                $block->text('An invoice has been paid.');
                $block->field("*Invoice No:*\n1000")->markdown();
                $block->field("*Invoice Recipient:*\ntaylor@laravel.com")->markdown();
            })
            ->dividerBlock()
            ->sectionBlock(function (SectionBlock $block) {
                $block->text('Congratulations!');
            });
}
driesvints commented 10 months ago

Right, seems to be part of https://github.com/laravel/slack-notification-channel/pull/64 by @claudiodekker. This might just have been forgotten. We'd appreciate a PR 👍