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

Argument #1 ($message) must be of type Illuminate\Notifications\Slack\SlackMessage

aromka opened this issue · comments

Slack Notification Channel Version

3.0

Laravel Version

10.4

PHP Version

8.1

Database Driver & Version

No response

Description

In the latest update it seems that return type for toSlack() changed from \Illuminate\Notifications\Messages\SlackMessage to Illuminate\Notifications\Slack\SlackMessage which results in

TypeError: Illuminate\Notifications\Slack\SlackChannel::buildJsonPayload(): Argument #1 ($message) must be of type Illuminate\Notifications\Slack\SlackMessage, Illuminate\Notifications\Messages\SlackMessage given, called in /vendor/laravel/slack-notification-channel/src/Slack/SlackChannel.php on line 38

This should probably be documented as a breaking change for the upgrade.

Steps To Reproduce

Upgrade slack notifications to v3.0

@aromka I think this is an application issue. The toSlack method is defined within your application with a return type, but I guess you've now switched to Block Kit and need to update the return type too.

Hi @jbrooksuk , I'm also facing the same issue.
I couldn't clearly understand your explanation, with all due respect :)

The documentation says that toSlack() should return Illuminate\Notifications\Slack\SlackMessage but in the code it expects Illuminate\Notifications\Messages\SlackMessage

public function buildJsonPayload(SlackMessage $message)

@zeshan77 there are two ways to build Slack notifications:

  • Webhook Channel (the old way, which you're using currently)
  • Block Kit

You only need to change the toSlack return type if you're rebuilding your notification to send with Block Kit. Hope that helps.

I'm also getting this error. v3.0.

Laravel v9.51.0

PHP 8.1

Notification class:

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Messages\SlackAttachment;

class ItemNotification extends Notification
{
    public function __construct(
        public string $link,
        public string $imageUrl,
        public string $action = 'Added'
    ) {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['slack'];
    }

    /**
     * Get the Slack representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\SlackMessage
     */
    public function toSlack($notifiable)
    {
        return (new SlackMessage)
            ->content("{$this->action}: {$this->link}")
            ->attachment(function (SlackAttachment $attachment) {
                $attachment->image($this->imageUrl);
            });
    }
}

How I'm calling it:

Notification::route('slack', config('logging.channels.slack.url'))
    ->notify(new ItemNotification("{$subUrl}/{$newId}", $imageUrl));

If anyone else is encountering this and can't figure out why, you may still be supplying an incoming webhook url as the notification route.

You may be doing this via the routeNotificationFor method on a Notifiable:

class User extends Authenticatable
{
    use Notifiable;
 
    /**
     * Route notifications for the Slack channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForSlack($notification)
    {
        return 'https://hooks.slack.com/services/...';
    }
}

or you may be doing it as an AnonymousNotifiable:

Notification::send(Notification::route('slack', "https://hooks.slack.com/services/..."), new MySlackNotification())

To resolve this error, you need to replace these webhook urls with channel names:

class User extends Authenticatable
{
    use Notifiable;
 
    /**
     * Route notifications for the Slack channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForSlack($notification)
    {
        return '#my-channel';
    }
}

or if you are using an AnonymousNotifiable:

Notification::send(Notification::route('slack', "#my-channel"), new MySlackNotification())

The determineChannel method in SlackNotificationRouterChannel uses the format of the route to determine which SlackMessage to use. See https://github.com/laravel/slack-notification-channel/blob/3.x/src/SlackNotificationRouterChannel.php