Reputation: 6830
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?
Upvotes: 1
Views: 1875
Reputation: 1501
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
Upvotes: 0
Reputation: 350
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:
Upvotes: 1
Reputation: 32852
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.
Upvotes: 2