1

I'm trying to send a notification with a mention of a user in a general channel. This is what I have:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class WeeklyTasksResponsible extends Notification
{
    use Queueable;

    protected $employee;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(\App\Employee $employee)
    {
        $this->employee = $employee;
    }

    /**
     * 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 SlackMessage
     */
    public function toSlack($notifiable)
    {
        return (new SlackMessage)
            ->content('Reponsible for this week is: ' . $this->employee->slack_name);
    }
}

This will sent a weekly notification in the general slack channel of our company. The message is "Responsible for this week is: nameofuser". The problem is the user doesn't see a notification of this.

I've also tried do this:

public function toSlack($notifiable)
{
    return (new SlackMessage)
        ->content('Reponsible for this week is: @' . $this->employee->slack_name);
}

But it isn't the same as mentioning someone myself in the channel.

How can I do this?

Erik Kalkoken
  • 23,798
  • 6
  • 53
  • 81
nielsv
  • 5,818
  • 29
  • 96
  • 196
  • 1
    According to their [documentation](https://api.slack.com/methods/chat.postMessage), you have to set the `link_names` parameter to `true`/`1`. I don't have enough time to give you the solution myself, hope someone can help you. You can see an explanation in [this](https://stackoverflow.com/a/40774442/7117697) answer. – Kenny Horna Nov 24 '18 at 15:45
  • Or just use the user ID instead of the user name and it should work. e.g. `` – Erik Kalkoken Nov 24 '18 at 17:56

3 Answers3

1

As mentioned by @HCK you can enable matching of usernames for @username mentions in chat.postMessages by setting the optional parameter link_names to true.

However, creating mentions with usernames is deprecated and should no longer be used.

The recommended approach is to create mentions with the user ID, which will work by default.

Example:

<@U12345678>

See the post A lingering farewell to the username from Slack for details about username deprecation.

Erik Kalkoken
  • 23,798
  • 6
  • 53
  • 81
0

I just found the following in the Laravel 6.18.0 source code:

    /**
     * Find and link channel names and usernames.
     *
     * @return $this
     */
    public function linkNames()
    {
        $this->linkNames = 1;

        return $this;
    }

vendor/laravel/slack-notification-channel/src/Messages/SlackMessage.php

So you can use it like this:

public function toSlack($notifiable)
    {
        return (new SlackMessage)
        ->linkNames()
        ...
    }

As @erik-kalkoken pointed out, use the Slack user ID, enclosed by <> and with the @ sign. You can find it in your profile in the Slack App:

enter image description here

repat
  • 146
  • 2
  • 9
0

Actually, I tried that way and it's works pretty well.

$content .= "New order!\r\n @person";

return (new SlackMessage)
            ->content($content)->linkNames();

You would need to add the @ before name of person or team and in order for slack to recognize them as mentions not just text, you would need to chain the content with linkNames