代码之家  ›  专栏  ›  技术社区  ›  nielsv

组通道中用户的添加通知

  •  1
  • nielsv  · 技术社区  · 7 年前

    我正在尝试发送一个通知,其中提到一个普通频道中的用户。这就是我所拥有的:

    <?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);
        }
    }
    

    这将在我们公司的总频道每周发送一次通知。信息是“本周负责人是:nameofuser”。问题是用户看不到这方面的通知。

    我也试过这样做:

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

    我该怎么做?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Erik Kalkoken    7 年前

    正如@HCK所提到的,您可以为 @username chat.postMessages 通过设置可选参数 link_names true .

    推荐的方法是使用用户ID创建提及,这在默认情况下是有效的。

    例子:

    <@U12345678>
    

    看到帖子了吗 A lingering farewell to the username 从Slack获取有关用户名弃用的详细信息。

        2
  •  0
  •   repat    6 年前

        /**
         * 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

    所以你可以这样使用它:

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

    正如@erikkalkoken指出的,使用Slack用户ID,用 <> @ 签字。您可以在Slack应用程序的个人资料中找到它:

    enter image description here

        3
  •  0
  •   Abdelsalam Megahed    5 年前

    $content .= "New order!\r\n @person";
    
    return (new SlackMessage)
                ->content($content)->linkNames();
    

    您需要添加 @

    推荐文章