I'm trying to setup Slack notifications for my project but they don't seem to be coming through to my slack workspace. I think I've followed the Notifications docs correctly and added the following parts to my code.
Other than adding Bot Scopes to my Slack app, is there anything else I need to do in Slack? What's confusing is that I have an older project/app that is connected to Slack just fine, but that project uses webhooks and the user model's routeNotificationForSlack returns the webhook url instead, which seems to not be the suggested way now...
app/Listeners/UserRegistered.php
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Notifications\UserJoined;
use Illuminate\Support\Facades\Notification;
class UserRegistered implements ShouldQueue
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(Registered $event): void
{
// Slack
Notification::send($event->user, new UserJoined());
}
}
app/Notifications/UserJoined.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Slack\SlackMessage;
use Illuminate\Notifications\Notification;
class UserJoined extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct() {}
/**
* Get the notification"s delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return env("APP_ENV") === "production" ? ["slack"] : [];
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return new SlackMessage()->text(
"{$notifiable->username} - {$notifiable->email}",
);
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray($notifiable): array
{
return [];
}
}
composer.json
"laravel/framework": "^12.0",
"laravel/slack-notification-channel": "^3.6"
user model
public function routeNotificationForSlack(Notification $notification): mixed
{
return "#users";
}
.env
SLACK_BOT_USER_OAUTH_TOKEN=xoxb-beepboopbeep
SLACK_BOT_USER_DEFAULT_CHANNEL="#general"