staffkojinpro / microsoft-teams
适用于 Microsoft Teams 的 Laravel 通知通道
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^6.3 || ^7.0
- illuminate/notifications: ~5.5 || ~6.0 || ~7.0 || ^8.0 || ^9.0
- illuminate/support: ~5.5 || ~6.0 || ~7.0 || ^8.0 || ^9.0
Requires (Dev)
- mockery/mockery: ^1.2.3
- phpunit/phpunit: ^8.0|^9.5
This package is auto-updated.
Last update: 2024-10-01 00:14:51 UTC
README
此包使您能够轻松使用 Laravel 5.5+、6.x、7.x、8.x、9.x 和 10.x 通过 Microsoft Teams 发送通知
return MicrosoftTeamsMessage::create() ->to(config('services.microsoft_teams.sales_url')) ->type('success') ->title('Subscription Created') ->content('Yey, you got a **new subscription**. Maybe you want to contact him if he needs any support?') ->button('Check User', 'https://foo.bar/users/123');
内容
安装
您可以通过 composer 安装此包
composer require laravel-notification-channels/microsoft-teams
接下来,如果您使用的是不带自动发现的 Laravel,请将服务提供者添加到 config/app.php
'providers' => [ // ... Staffkojinpro\MicrosoftTeams\MicrosoftTeamsServiceProvider::class, ],
设置连接器
请查看此链接以设置和添加 Webhook 连接器到您的团队频道。支持基本 Markdown,也请查看消息卡片参考文章,该文章更详细地介绍了应该做什么和不应该做什么。
设置 MicrosoftTeams 服务
然后,配置您的 Webhook URL
将以下代码添加到您的 config/services.php
// config/services.php ... 'microsoft_teams' => [ 'webhook_url' => env('TEAMS_WEBHOOK_URL'), ], ...
如果您有多个团队或频道,您也可以添加多个 Webhook,这取决于您。
// config/services.php ... 'microsoft_teams' => [ 'sales_url' => env('TEAMS_SALES_WEBHOOK_URL'), 'dev_url' => env('TEAMS_DEV_WEBHOOK_URL'), ], ...
使用
现在您可以在通知内的 via()
方法中使用该通道
use Illuminate\Notifications\Notification; use Staffkojinpro\MicrosoftTeams\MicrosoftTeamsChannel; use Staffkojinpro\MicrosoftTeams\MicrosoftTeamsMessage; class SubscriptionCreated extends Notification { public function via($notifiable) { return [MicrosoftTeamsChannel::class]; } public function toMicrosoftTeams($notifiable) { return MicrosoftTeamsMessage::create() ->to(config('services.microsoft_teams.sales_url')) ->type('success') ->title('Subscription Created') ->content('Yey, you got a **new subscription**. Maybe you want to contact him if he needs any support?') ->button('Check User', 'https://foo.bar/users/123'); } }
除了为收件人添加 to($url)
方法外,您还可以在 Notifiable 模型内添加 routeNotificationForMicrosoftTeams
方法。此方法需要返回 Webhook URL。
public function routeNotificationForMicrosoftTeams(Notification $notification) { return config('services.microsoft_teams.sales_url') }
按需通知使用
要使用按需通知,您可以在通知外观上使用 route
方法。
Notification::route(MicrosoftTeamsChannel::class,null) ->notify(new SubscriptionCreated());
可用的消息方法
to(string $webhookUrl)
:收件人的 Webhook URL。title(string $title)
:消息标题。summary(string $summary)
:消息摘要。type(string $type)
:用作主题颜色的类型(任何有效的十六进制代码或以下之一:primary|secondary|accent|error|info|success|warning)。content(string $content)
:消息内容(支持 Markdown)。button(string $text, string $url = '', array $params = [])
:按钮的文本和 URL。用于潜在动作的包装器。action(string $text, $type = 'OpenUri', array $params = [])
:潜在动作的文本和类型。根据动作可以添加更多参数。有关不同类型的更多信息,请参阅此链接。options(array $options, $sectionId = null)
:向消息有效负载对象传递附加选项。
部分
您可以在消息卡片内部定义一个或多个部分。以下方法可以在部分中使用
addStartGroupToSection($sectionId = 'standard_section')
:添加 startGroup 属性,该属性标记信息的逻辑组开始。activity(string $activityImage = '', string $activityTitle = '', string $activitySubtitle = '', string $activityText = '', $sectionId = 'standard_section')
:向部分添加活动。fact(string $name, string $value, $sectionId = 'standard_section')
:向部分添加事实(支持 Markdown)。image(string $imageUri, string $title = '', $sectionId = 'standard_section')
:向部分添加图像。heroImage(string $imageUri, string $title = '', $sectionId = 'standard_section')
:向一个部分添加英雄图片。
此外,标题、内容、按钮和操作也可以通过可选的params
值添加到部分中。
title(string $title, array $params = ['section' => 'my-section'])
:消息的标题,并添加到my-section
。content(string $content, array $params = ['section' => 'my-section'])
:消息的内容,并添加到my-section
(支持Markdown)。button(string $text, string $url = '', array $params = ['section' => 'my-section'])
:按钮的文本和URL,并添加到my-section
。action(string $text, $type = 'OpenUri', array $params = ['section' => 'my-section'])
:潜在操作的文本和类型,并添加到my-section
。
变更日志
有关最近更改的更多信息,请参阅变更日志。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件tobias.madner@gmx.at而不是使用问题跟踪器。
贡献
有关详细信息,请参阅贡献指南。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可文件。