项目作者: snoeren-development

项目描述 :
Send notifications to a Discord webhook.
高级语言: PHP
项目地址: git://github.com/snoeren-development/laravel-discord-webhook-channel.git
创建时间: 2020-01-10T13:10:08Z
项目社区:https://github.com/snoeren-development/laravel-discord-webhook-channel

开源协议:MIT License

下载


Laravel Discord Webhook Channel

Latest version on Packagist
Software License
Build status
Downloads

Installation

You can install the package using Composer:

  1. 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;

  1. use Illuminate\Database\Eloquent\Model;
  2. use Illuminate\Notifications\Notifiable;
  3. class User extends Model
  4. {
  5. use Notifiable;
  6. /**
  7. * Route the notification for Discord.
  8. *
  9. * @return string
  10. */
  11. public function routeNotificationForDiscord(): string
  12. {
  13. return $this->discord_webhook;
  14. }
  15. }

The webhook URL can be created and retrieved via the Discord channel server Webhooks settings. The notification needs the full URL which looks like

  1. https://discordapp.com/api/webhooks/1234567890123456789/1Px6cK9-9346g0CbOYArYjr1jj6X9rvRcCpRi3s7HePN0POeCSvuF1Iagb-Wjiq78BnT

You may now send notifications through Laravel to Discord webhooks using the via method.

  1. use SnoerenDevelopment\DiscordWebhook\DiscordMessage;
  2. use SnoerenDevelopment\DiscordWebhook\DiscordWebhookChannel;
  3. class DiscordNotification extends Notification
  4. {
  5. /**
  6. * Get the notification's delivery channels.
  7. *
  8. * @param mixed $notifiable The notifiable model.
  9. * @return array
  10. */
  11. public function via($notifiable)
  12. {
  13. return [DiscordWebhookChannel::class];
  14. }
  15. /**
  16. * Get the Discord representation of the notification.
  17. *
  18. * @param mixed $notifiable The notifiable model.
  19. * @return \SnoerenDevelopment\DiscordWebhook\DiscordMessage
  20. */
  21. public function toDiscord($notifiable): DiscordMessage
  22. {
  23. return DiscordMessage::create()
  24. ->username('My Laravel App')
  25. ->content('The message body.')
  26. ->avatar('https://domain.com/avatar.jpg')
  27. ->tts(false);
  28. }
  29. }

Testing

  1. $ composer test

Credits

License

The MIT license. See LICENSE for more information.