laravel / slack-notification-channel

Slack Notification Channel for laravel.

Home Page:https://laravel.com/docs/notifications#slack-notifications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No way to disable unfurl_links or unfurl_media

john-f-chamberlain opened this issue · comments

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.

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

Possible solution:
Modify

public function unfurlLinks(): self
{
$this->unfurlLinks = true;
return $this;
}

To Be:

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

        return $this;
    }

and modify

public function unfurlMedia(): self
{
$this->unfurlMedia = true;
return $this;
}

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.

Think you're looking at the wrong place as the notification class has the appropriate messages to set the boolean for these:

public function unfurlLinks($unfurlLinks)
{
$this->unfurlLinks = $unfurlLinks;
return $this;
}

This is also the class that's documented.

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

@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!');
            });
}

Right, seems to be part of #64 by @claudiodekker. This might just have been forgotten. We'd appreciate a PR 👍