Laravel 8 Slack 附件

2021-07-19 10:57 更新

你還可以添加 “附件” 到 Slack 消息。相對簡單文本消息,附件可以提供更加豐富的格式選項(xiàng)。在這個(gè)例子中,我們會(huì)發(fā)送一個(gè)在應(yīng)用程序中出現(xiàn)的異常錯(cuò)誤通知,包含查看更多異常細(xì)節(jié)的鏈接:

/**
 * Get the Slack representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return SlackMessage
 */
public function toSlack($notifiable)
{
    $url = url('/exceptions/'.$this->exception->id);

    return (new SlackMessage)
                ->error()
                ->content('Whoops! Something went wrong.')
                ->attachment(function ($attachment) use ($url) {
                    $attachment->title('Exception: File Not Found', $url)
                               ->content('File [background.jpg] was not found.');
                });
} 

上面的示例會(huì)生成一個(gè)如下所示的 Slack 消息:

basic-slack-attachment.png

附件還允許你指定要呈獻(xiàn)給用戶的數(shù)組數(shù)據(jù)。為了提高可讀性,給定的數(shù)組會(huì)以表格形式展示:

/**
 * 獲取 Slack 形式的通知。
 *
 * @param  mixed  $notifiable
 * @return SlackMessage
 */
public function toSlack($notifiable)
{
    $url = url('/invoices/'.$this->invoice->id);

    return (new SlackMessage)
                ->success()
                ->content('One of your invoices has been paid!')
                ->attachment(function ($attachment) use ($url) {
                    $attachment->title('Invoice 1322', $url)
                               ->fields([
                                    'Title' => 'Server Expenses',
                                    'Amount' => '$1,234',
                                    'Via' => 'American Express',
                                    'Was Overdue' => ':-1:',
                                ]);
                });
} 

上面的示例會(huì)生成一個(gè)如下所示的 Slack 消息:

slack-fields-attachment.png
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號