I recently struggled to get my Laravel 11 app to send messages to a Slack notification channel.
My first confusion was that the docs refer to making the relevant model class notifiable
and sending the notification that way. Which wasn’t relevant to my use case as I wanted to send it from a utility command.
Googling – or, erm, maybe it was Claude – came up with this route method, which looked promising:
Notification::route('slack',
config('services.slack.notifications.webhook_url'))
->notify(new InterestingThing());
Code language: PHP (php)
But I then ran headfirst into this error:
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
Code language: PHP (php)
The documentation states that the notifiable
model should have the following method, which returns the channel name, not the webhook URL I’d been using.
/**
* Route notifications for the Slack channel.
*/
public function routeNotificationForSlack(Notification $notification): mixed
{
return '#foo-channel';
}
Code language: PHP (php)
I had tried this (spoiler: correct version), but I got [channel_not_found]
:
Notification::route('slack',
config('services.slack.notifications.channel'))
->notify(new InterestingThing());
RuntimeException: Slack API call failed with error [channel_not_found]
Code language: PHP (php)
After going around in circles for a bit, I thankfully stumbled upon this Github comment from James Brooks, which gave me the context I needed to understand the problem:
there are two ways to build Slack notifications:
- Webhook Channel (the old way, which you’re using currently)
- Block Kit
So, webhook is the old way, and that’s great because I *definitely* want to use Block Kit to get that formatting goodness, but why am I getting [channel_not_found]
?
TL;DR
Go to your Slack client, and in the channel you want the bot to post, do this:
/invite @your-bot
- I’d copied and pasted code that doesn’t work with Laravel 11 (the old Webhook way) 🤦
- The bot needs permission to post to the channel
I’m posting this hoping it saves someone else time in the future.
Leave a Reply