osa-eg/laravel-teams-notification

一个用于向Microsoft Teams发送通知的Laravel包

v2.1.2 2024-09-23 05:24 UTC

This package is auto-updated.

Last update: 2024-09-23 05:25:31 UTC


README

Laravel Teams Notification是一个使用"当收到webhook请求时向频道发布"工作流webhook发送通知到Microsoft Teams的包。它支持发送普通消息、带有跟踪信息的异常消息和包含附加详情或JSON块的消息,遵循Microsoft Teams自适应卡所需的JSON结构。该包还包括Laravel的自定义日志功能,使您能够轻松地将现有Laravel应用程序与它集成,并将重要事件直接记录到Microsoft Teams频道。

Image 2

目录

特性

  • 发送普通消息:向Teams发送简单的文本通知。
  • 发送包含额外详情的消息:在通知中包含额外详情。
  • 发送成功消息:用绿色突出显示成功的操作。
  • 发送警告消息:用橙色表示警告。
  • 发送错误消息:用红色报告错误,可选堆栈跟踪。
  • 发送包含JSON块的消息:在消息中包含格式化的JSON数据。
  • 自定义日志:使用Laravel的日志系统直接将消息记录到Microsoft Teams。
  • 可配置的消息颜色:使用预定义选项设置消息的自定义颜色。

安装

要安装此包,您需要PHP 7.0或更高版本以及Laravel 5.5或更高版本。使用Composer

composer require osa-eg/laravel-teams-notification

然后,将您的Microsoft Teams webhook URL添加到您的.env文件

TEAMS_WEBHOOK_URL=your_teams_webhook_url

发布文件

配置

要发布此包包含的配置文件到您的Laravel项目,运行

php artisan vendor:publish --tag=laravel-teams-notification-config

使用方法

发送普通消息

要发送普通消息,使用sendMessage方法

use Osama\LaravelTeamsNotification\TeamsNotification;

$notification = new TeamsNotification();
$message = "System Notification";
$notification->sendMessage($message);

发送包含额外详情和颜色的普通消息

要发送包含额外详情的普通消息,使用带有第二个参数的sendMessage方法

use Osama\LaravelTeamsNotification\TeamsNotification;

$notification = new TeamsNotification();
$message = "System Notification";
$details = [
    'Server' => 'Production',
    'Status' => 'Running',
    'Uptime' => '24 days'
];
$notification->sendMessage($message, $details);

发送成功消息

要发送成功消息,使用success方法

use Osama\LaravelTeamsNotification\TeamsNotification;

$notification = new TeamsNotification();
$message = "Operation completed successfully!";
$details = [
    'Duration' => '2 seconds',
    'Processed Items' => '150'
];
$notification->success()->sendMessage($message, $details);

发送警告消息

要发送警告消息,使用warning方法

use Osama\LaravelTeamsNotification\TeamsNotification;

$notification = new TeamsNotification();
$message = "Warning: High Memory Usage Detected";
$details = [
    'Memory Usage' => '95%',
    'Server' => 'Production'
];
$notification->warning()->sendMessage($message, $details);

发送带有跟踪和默认关注颜色的错误消息

要发送带有跟踪的错误消息,使用error方法和bindTrace方法

use Osama\LaravelTeamsNotification\TeamsNotification;

try {
    // Code that may throw an exception
} catch (\Exception $exception) {
    $notification = new TeamsNotification();
    $notification->bindTrace()->error()->sendException($exception);
}

发送包含数组作为JSON块的具有自定义颜色的消息

要发送将数组作为JSON块的消息,使用sendJsonMessage方法

use Osama\LaravelTeamsNotification\TeamsNotification;

$notification = new TeamsNotification();
$message = "Data Update";
$data = [
    'user_id' => 12345,
    'action' => 'update',
    'status' => 'success',
    'timestamp' => date('Y-m-d H:i:s')
];
$notification->success()->sendJsonMessage($message, $data);

自定义日志

该包还支持将自定义日志记录到Microsoft Teams。要设置自定义日志,请按照以下步骤操作

  1. 在您的Laravel项目中配置日志

    config/logging.php中,添加以下配置

    'channels' => [
        // Other channels...
    
        'teams' => [
            'driver' => 'custom',
             'via' => \Osama\LaravelTeamsNotification\Logging\TeamsLoggingChannel::class,
             'webhook_url' => env('TEAMS_WEBHOOK_URL'),
         ],
  2. 使用自定义日志通道

    要记录消息到Teams,使用teams日志通道

    Log::channel('teams')->info('This is an info message');
    Log::channel('teams')->error('This is an error message');

方法

  • setColor(string $color):设置消息的颜色。有效的颜色有 "default"、"dark"、"light"、"accent"、"good"、"warning"、"attention"。
  • success():将消息颜色设置为 "good"。
  • warning():将消息颜色设置为 "warning"。
  • error():将消息颜色设置为 "attention"。
  • sendMessage($message, array $details = []):发送带有额外详情的普通消息。
  • sendException(\Exception $exception):发送带有可选跟踪详情的异常消息。
  • bindTrace():在异常消息中包含跟踪。
  • sendJsonMessage($message, array $data):以JSON块的形式发送一个包含数组的消息。

了解更多

有关将Microsoft Teams通知集成到您的Laravel应用程序的详细指南,请查看我的Medium文章

通过Microsoft Teams工作流集成简化Laravel通知

许可证

本软件包是开源软件,受MIT许可协议许可。


This README now includes a Table of Contents section that links to different parts of the document for easier navigation.