osa-eg/laravel-teams-notification
Composer 安装命令:
composer require osa-eg/laravel-teams-notification
包简介
A Laravel package to send notifications to Microsoft Teams
关键字:
README 文档
README
Laravel Teams Notification is a package for sending notifications to Microsoft Teams using the "Post to a channel when a webhook request is received" workflow webhook. It supports sending normal messages, exception messages with trace, and messages with additional details or JSON blocks, following the JSON structure required by Microsoft Teams adaptive cards. The package also includes custom logging functionality for Laravel, making it easy to integrate with your existing Laravel applications and log important events directly to Microsoft Teams channels.
Table of Contents
Features
- Send Normal Messages: Send simple text notifications to Teams.
- Send Messages with Additional Details: Include extra details in the notification.
- Send Success Messages: Highlight successful operations with a green color.
- Send Warning Messages: Indicate warnings with an orange color.
- Send Error Messages: Report errors with a red color and optional stack trace.
- Send Messages with JSON Blocks: Include formatted JSON data in the message.
- Custom Logging: Log messages directly to Microsoft Teams using Laravel’s logging system.
- Configurable Message Colors: Set custom colors for messages with predefined options.
Installation
To install the package, you need PHP 7.0 or higher and Laravel 5.5 or later. Use Composer:
composer require osa-eg/laravel-teams-notification
Then, add your Microsoft Teams webhook URL to your .env file:
TEAMS_WEBHOOK_URL=your_teams_webhook_url
Publishing Files
Config
To publish the config file included with this package to your Laravel project, run:
php artisan vendor:publish --tag=laravel-teams-notification-config
Usage
Sending a Normal Message
To send a normal message, use the sendMessage method:
use Osama\LaravelTeamsNotification\TeamsNotification; $notification = new TeamsNotification(); $message = "System Notification"; $notification->sendMessage($message);
Sending a Normal Message with Additional Details and Color
To send a normal message with additional details, use the sendMessage method with the second parameter:
use Osama\LaravelTeamsNotification\TeamsNotification; $notification = new TeamsNotification(); $message = "System Notification"; $details = [ 'Server' => 'Production', 'Status' => 'Running', 'Uptime' => '24 days' ]; $notification->sendMessage($message, $details);
Sending a Success Message
To send a success message, use the success method:
use Osama\LaravelTeamsNotification\TeamsNotification; $notification = new TeamsNotification(); $message = "Operation completed successfully!"; $details = [ 'Duration' => '2 seconds', 'Processed Items' => '150' ]; $notification->success()->sendMessage($message, $details);
Sending a Warning Message
To send a warning message, use the warning method:
use Osama\LaravelTeamsNotification\TeamsNotification; $notification = new TeamsNotification(); $message = "Warning: High Memory Usage Detected"; $details = [ 'Memory Usage' => '95%', 'Server' => 'Production' ]; $notification->warning()->sendMessage($message, $details);
Sending an Error Message with Trace and Default Attention Color
To send an error message with trace, use the error method and bindTrace method:
use Osama\LaravelTeamsNotification\TeamsNotification; try { // Code that may throw an exception } catch (\Exception $exception) { $notification = new TeamsNotification(); $notification->bindTrace()->error()->sendException($exception); }
Sending a Message with Array as JSON Block and Custom Color
To send a message with an array as a JSON block, use the sendJsonMessage method:
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);
Custom Logging
The package also supports custom logging to Microsoft Teams. To set up custom logging, follow these steps:
-
Configure Logging in Your Laravel Project:
In
config/logging.php, add the following configuration:'channels' => [ // Other channels... 'teams' => [ 'driver' => 'custom', 'via' => \Osama\LaravelTeamsNotification\Logging\TeamsLoggingChannel::class, 'webhook_url' => env('TEAMS_WEBHOOK_URL'), 'level' => env('TEAMS_LOG_LEVEL', 'critical') , ],
-
Use the Custom Log Channel:
To log messages to Teams, use the
teamslog channel:Log::channel('teams')->info('This is an info message'); Log::channel('teams')->error('This is an error message');
Methods
- setColor(string $color): Sets the color of the message. Valid colors are "default", "dark", "light", "accent", "good", "warning", "attention".
- success(): Sets the message color to "good".
- warning(): Sets the message color to "warning".
- error(): Sets the message color to "attention".
- sendMessage($message, array $details = []): Sends a normal message with additional details.
- sendException(\Exception $exception): Sends an exception message with optional trace details.
- bindTrace(): Includes the trace in the exception message.
- sendJsonMessage($message, array $data): Sends a message with an array as a JSON block.
Learn More
For a detailed guide on integrating Microsoft Teams notifications with your Laravel application, check out my Medium article:
Streamlining Laravel Notifications with Microsoft Teams Workflow Integration
License
This package is open-sourced software licensed under the MIT license.
osa-eg/laravel-teams-notification 适用场景与选型建议
osa-eg/laravel-teams-notification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 320.1k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2024 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「notification」 「laravel」 「Teams」 「teams-workflow」 「teams-connector」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 osa-eg/laravel-teams-notification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 osa-eg/laravel-teams-notification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 osa-eg/laravel-teams-notification 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package makes it easy to send notifications via AfricasTalking with Laravel
A Zend Framework module that sets up Monolog for logging in applications.
Apple Push Notification & Feedback Provider
Throttle notifications on a per-channel basis
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
统计信息
- 总下载量: 320.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 71
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-24
