awssat / discord-notification-channel
laravel的Discord通知渠道
1.4.4
2024-07-06 23:15 UTC
Requires
- php: ^7.1.3|^8.0
- guzzlehttp/guzzle: ^6.0|^7.0
- illuminate/notifications: ~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- laravel/slack-notification-channel: ^2.0|^3.2
Requires (Dev)
- mockery/mockery: ^1.4 || ^1.6
- phpunit/phpunit: ^7.0|^8.0|^9.0|^10.1
README
简介
通过Laravel通知渠道发送Discord或Slack消息的有效负载
特性
- 支持使用
new (new SlackMessage)
或$this->toSlack($notifiable)
支持Slack有效负载 - 支持Discord Webhook有效负载
- 易于使用
安装
通过Composer
composer require awssat/discord-notification-channel
使用方法
在通知中,您应该在via方法中定义discord
渠道
public function via($notifiable) { return ['mail', 'discord']; }
您应该有一个toDiscord
方法
public function toDiscord($notifiable) { return (new DiscordMessage) ->from('Laravel') ->content('Content') ->embed(function ($embed) { $embed->title('Discord is cool')->description('Slack nah') ->field('Laravel', '9.0.0', true) ->field('PHP', '8.0.0', true); }); }
toDiscord
方法可以接收DiscordMessage
或SlackMessage
Slack消息示例
public function toDiscord($notifiable) { return (new SlackMessage) ->content('One of your invoices has been paid!'); }
或者,如果您想,也可以从toSlack
方法运行它
public function toSlack($notifiable) { return (new SlackMessage) ->content('One of your invoices has been paid!'); } public function toDiscord($notifiable) { return $this->toSlack($notifiable); }
有关Laravel Slack消息的更多示例,请参阅https://laravel.net.cn/docs/6.x/notifications#slack-notifications
路由Discord通知
为了将Discord通知路由到正确的位置,在您的notifiable实体上定义一个routeNotificationForDiscord
方法。这应该返回通知应该交付的Webhook URL。在此处阅读Webhook Discord文档https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks
<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; /** * Route notifications for the Discord channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForDiscord($notification) { return 'https://discordapp.com/api/webhooks/.......'; } }