snoeren-development / laravel-discord-webhook-channel
向Discord webhook发送通知。
v1.10.0
2024-03-25 07:39 UTC
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.8.1
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^v9.0.1
- phpunit/phpunit: ^11.0.8
- spatie/macroable: ^2.0.0
This package is auto-updated.
Last update: 2024-09-25 08:54:45 UTC
README
安装
您可以使用Composer安装此包
composer require snoeren-development/laravel-discord-webhook-channel
要求
此包需要至少PHP 8.2和Laravel 10。
使用
在您希望通过Discord通知的每个可通知模型中,您需要添加routeNotificationForDiscord
方法;
use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable; /** * Route the notification for Discord. * * @return string */ public function routeNotificationForDiscord(): string { return $this->discord_webhook; } }
webhook URL可以通过Discord频道服务器的Webhooks设置创建和检索。通知需要完整的URL,其外观如下
https://discordapp.com/api/webhooks/1234567890123456789/1Px6cK9-9346g0CbOYArYjr1jj6X9rvRcCpRi3s7HePN0POeCSvuF1Iagb-Wjiq78BnT
您现在可以通过Laravel的via
方法向Discord webhook发送通知。
use SnoerenDevelopment\DiscordWebhook\DiscordMessage; use SnoerenDevelopment\DiscordWebhook\DiscordWebhookChannel; class DiscordNotification extends Notification { /** * Get the notification's delivery channels. * * @param mixed $notifiable The notifiable model. * @return array */ public function via($notifiable) { return [DiscordWebhookChannel::class]; } /** * Get the Discord representation of the notification. * * @param mixed $notifiable The notifiable model. * @return \SnoerenDevelopment\DiscordWebhook\DiscordMessage */ public function toDiscord($notifiable): DiscordMessage { return DiscordMessage::create() ->username('My Laravel App') ->content('The message body.') ->avatar('https://domain.com/avatar.jpg') ->tts(false); } }
测试
$ composer test
致谢
许可协议
MIT许可协议。有关更多信息,请参阅LICENSE。