brandonenr7
3 years ago
347
1
Laravel

Double Slack Messages via Notification

Posted 3 years ago by brandonenr7

So I am trying to send a notification to a collection of users. For my case I am sending a notification to admins that a form was completed on the website. The notification is supposed to send an email to each admin and a message to our Slack channel.

So here is where the issue occurs... The notification properly sends an individual email to each of the admins, however it sends the message to the Slack channel twice. I am assuming it sends it to Slack twice because it was trying to notify the two admins, although the whole website uses the same Slack Webhook URL.

Is there any way to make it keep sending the emails individually, however only send a Slack message once?

This is the Notification being called from the controller. $accessRequest is just the model being passed.

$admins = User::where('super_admin', true)->get();
Notification::send($admins, new NotifyAdmins($accessRequest));

The User model has this method added.

    public function routeNotificationForSlack() {
        return env('SLACK_WEBHOOK_URL');
    }

And here is the Notification class' toSlack method

    public function toSlack($notifiable)
    {
        $request = $this->request;

        return (new SlackMessage)
            ->to('#eap-requests')
            ->content('A new request has been received!')
            ->attachment(function ($attachment) use ($request) {
                $attachment->title('Request #'.$request->id, route('access.request.show', $request))
                    ->fields([
                        'Name' => $request->name,
                        'Email' => $request->email,
                        'Teams Requested' => $request->teams,
                        'Average Members' => $request->members,
                    ]);
            });
    }

Any thoughts or suggestions how to keep the Slack message to a single call on that Webhook?

Please sign in or create an account to participate in this conversation.