snoeren-development / laravel-discord-webhook-channel
Send notifications to a Discord webhook.
Fund package maintenance!
bunq.me/SnoerenDevelopment
Installs: 28 576
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 3
Forks: 4
Open Issues: 2
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.9.2
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- larastan/larastan: ^2.9
- orchestra/testbench: v9.5.2
- phpunit/phpunit: ^11.4.3
- spatie/macroable: ^2.0.0
This package is auto-updated.
Last update: 2024-11-13 07:39:18 UTC
README
Installation
You can install the package using Composer:
composer require snoeren-development/laravel-discord-webhook-channel
Requirements
This package requires at least PHP 8.2 and Laravel 10.
Usage
In every notifiable model you wish to notify via Discord, you need to add the routeNotificationForDiscord
method;
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; } }
The webhook URL can be created and retrieved via the Discord channel server Webhooks settings. The notification needs the full URL which looks like
https://discordapp.com/api/webhooks/1234567890123456789/1Px6cK9-9346g0CbOYArYjr1jj6X9rvRcCpRi3s7HePN0POeCSvuF1Iagb-Wjiq78BnT
You may now send notifications through Laravel to Discord webhooks using the via
method.
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); } }
Testing
$ composer test
Credits
License
The MIT license. See LICENSE for more information.