定制 revolution/laravel-notification-discord-webhook 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

revolution/laravel-notification-discord-webhook

最新稳定版本:1.3.1

Composer 安装命令:

composer require revolution/laravel-notification-discord-webhook

包简介

Laravel Notification for Discord(Webhook)

README 文档

README

Maintainability Code Coverage

Introduction

This package allows you to easily send notifications to Discord by simply specifying a webhook. With this Laravel notification channel, you can quickly integrate Discord notifications into your application without dealing with the complexities of Discord's API. Just configure your webhook URL and start sending notifications right away.

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

Requirements

  • PHP >= 8.2
  • Laravel >= 11.0

Installation

composer require revolution/laravel-notification-discord-webhook

Uninstall

composer remove revolution/laravel-notification-discord-webhook

Config

Get the webhook url from your Discord server settings.
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/...

Usage

<?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);
    }
}

On-Demand Notification

use Illuminate\Support\Facades\Notification;

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

User Notification

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

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

Send embeds

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'),
                                  )
                              );
    }

Send attachment files

Send only file. content and filename are required.

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'
                ));
    }

Using files in embed.

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'
                              ));
    }

Send any message

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

LICENSE

MIT

统计信息

  • 总下载量: 6.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-27