revolution/laravel-notification-discord-webhook

Laravel Discord(Webhook)通知

1.2.0 2024-09-24 04:41 UTC

This package is auto-updated.

Last update: 2024-09-25 04:05:10 UTC


README

https://discord.com/developers/docs/resources/webhook#execute-webhook

要求

  • PHP >= 8.1
  • Laravel >= 10.0

安装

composer require revolution/laravel-notification-discord-webhook

卸载

composer remove revolution/laravel-notification-discord-webhook

配置

从您的Discord服务器设置中获取webhook URL。
https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks

config/services.php

    'discord' => [
        'webhook' => env('DISCORD_WEBHOOK'),
    ],

.env

DISCORD_WEBHOOK=https://discord.com/api/webhooks/...

使用方法

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordChannel;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;

class DiscordNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(protected string $content)
    {
        //
    }

    public function via($notifiable): array
    {
        return [DiscordChannel::class];
    }

    public function toDiscordWebhook(object $notifiable): DiscordMessage
    {
        return DiscordMessage::create(content: $this->content);
    }
}

按需通知

use Illuminate\Support\Facades\Notification;

Notification::route('discord-webhook', config('services.discord.webhook'))
            ->notify(new DiscordNotification('test'));

用户通知

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDiscordWebhook($notification): string
    {
        return $this->discord_webhook;
    }
}
$user->notify(new DiscordNotification('test'));

发送嵌入消息

use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordEmbed;

    public function toDiscordWebhook(object $notifiable): DiscordMessage
    {
        return DiscordMessage::create()
                              ->embed(
                                  DiscordEmbed::make(
                                      title: 'INFO',
                                      description: $this->content,
                                      url: route('home'),
                                  )
                              );
    }

发送附件文件

只发送文件。需要 contentfilename

use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordAttachment;
use Illuminate\Support\Facades\Storage;

    public function toDiscordWebhook(object $notifiable): DiscordMessage
    {
        return DiscordMessage::create()
            ->file(
                DiscordAttachment::make(
                    content: Storage::get('test.png'),
                    filename: 'test.png',
                    description: 'test',
                    filetype: 'image/png'
                ));
    }

在嵌入中使用文件。

use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordAttachment;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordEmbed;
use Illuminate\Support\Facades\Storage;

    public function toDiscordWebhook(object $notifiable): DiscordMessage
    {
        return DiscordMessage::create()
                              ->embed(
                                    DiscordEmbed::make(
                                        title: 'test',
                                        description: $this->content,
                                        image: 'attachment://test.jpg',
                                        thumbnail: 'attachment://test2.jpg',
                                    )
                              );
                              ->file(DiscordAttachment::make(
                                   content: Storage::get('test.jpg'),
                                   filename: 'test.jpg', 
                                   description: 'test', 
                                   filetype: 'image/jpg'
                              ))
                              ->file(new DiscordAttachment(
                                   content: Storage::get('test2.jpg'),
                                   filename: 'test2.jpg', 
                                   description: 'test2', 
                                   filetype: 'image/jpg'
                              ));
    }

发送任何消息

    public function toDiscordWebhook(object $notifiable): DiscordMessage
    {
        return DiscordMessage::create()
                              ->with([
                                  'content' => $this->content,
                                  'embeds' => [[]],
                               ]);
    }

许可证

MIT