Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/snoeren-development/laravel-discord-webhook-channel
Send notifications to a Discord webhook.
https://github.com/snoeren-development/laravel-discord-webhook-channel
discord laravel laravel-package notifications-channel php
Last synced: 7 days ago
JSON representation
Send notifications to a Discord webhook.
- Host: GitHub
- URL: https://github.com/snoeren-development/laravel-discord-webhook-channel
- Owner: snoeren-development
- License: mit
- Created: 2020-01-10T13:10:08.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-25T07:40:50.000Z (9 months ago)
- Last Synced: 2024-05-01T09:47:18.232Z (8 months ago)
- Topics: discord, laravel, laravel-package, notifications-channel, php
- Language: PHP
- Homepage:
- Size: 433 KB
- Stars: 14
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Discord Webhook Channel
[![Latest version on Packagist](https://img.shields.io/packagist/v/snoeren-development/laravel-discord-webhook-channel.svg?style=flat-square)](https://packagist.org/packages/snoeren-development/laravel-discord-webhook-channel)
[![Software License](https://img.shields.io/github/license/snoeren-development/laravel-discord-webhook-channel?style=flat-square)](LICENSE)
[![Build status](https://img.shields.io/github/actions/workflow/status/snoeren-development/laravel-discord-webhook-channel/php.yml?branch=main&style=flat-square)](https://github.com/snoeren-development/laravel-discord-webhook-channel/actions)
[![Downloads](https://img.shields.io/packagist/dt/snoeren-development/laravel-discord-webhook-channel?style=flat-square)](https://packagist.org/packages/snoeren-development/laravel-discord-webhook-channel)## Installation
You can install the package using Composer:
```bash
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;
```php
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.
```php
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
```bash
$ composer test
```## Credits
- [Michael Snoeren](https://github.com/MSnoeren)
- [All Contributors](https://github.com/snoeren-development/laravel-discord-webhook-channel/graphs/contributors)## License
The MIT license. See [LICENSE](LICENSE) for more information.