iracode-com/filament-notification

安装: 19

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:filament-plugin

1.4.2 2024-07-07 08:18 UTC

This package is auto-updated.

Last update: 2024-09-07 08:38:15 UTC


README

首次安装包

$ composer require iracode-com/filament-notification

将提供者添加到提供者项目

laravel 11 bootstrap/providers.php

<?php

return [
    App\Providers\AppServiceProvider::class,
    App\Providers\Filament\AdminPanelProvider::class,
    \IracodeCom\FilamentNotification\FilamentNotificationServiceProvider::class, // <---
];

Laravel 10 或更高版本 config/app.php

'providers' => ServiceProvider::defaultProviders()->merge( [

    \IracodeCom\FilamentNotification\FilamentNotificationServiceProvider::class, // <---

] )->toArray(),

将提供者文件发布到项目中

php artisan vendor:publish --provider=IracodeCom\FilamentNotification\FilamentNotificationServiceProvider

运行 Artisan 迁移以向 Users 表添加列

php artisan migrate

更新可填充到模型 app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable; // <-- Notifiable is important

    protected $fillable = [
    
        // ...
        'prefers_bale',
        'prefers_telegram',
        'prefers_sms',
        'telegram_chat_id',
        'bale_chat_id',
        'phone'
        // ...
        
    ];
    
    public function routeNotificationForSms()
    {
        return $this->phone;
    }

    public function routeNotificationForBale()
    {
        return $this->bale_chat_id;
    }
    
    public function routeNotificationForTelegram()
    {
        return $this->telegram_chat_id;
    }
    
}

将插件 Filament 添加到列表 app/Providers/Filament/AdminPanelProvider.php

<?php

namespace App\Providers\Filament;

use IracodeCom\FilamentNotification\FilamentNotificationPlugin;
use Filament\PanelProvider;
use Filament\Panel;

class AdminPanelProvider extends PanelProvider
{
    public function panel( Panel $panel ) : Panel
    {
        return $panel
            ->plugins( [

                FilamentNotificationPlugin::make(), // <---
                
                FilamentNotificationPlugin::make()
                                            ->setGridColumnConfig() // optional
                                            ->setGridColumnNotify() // optional
                                            ->setSectionColumnSpan() // optional
                                            ->usingPage() // optional

            ] )
        ;
    }
}

配置 .env 文件

SMS_DRIVER=log
SMS_USERNAME=
SMS_PASSWORD=
SMS_NUMBER=

TELEGRAM_BOT_TOKEN=
BALE_BOT_TOKEN=

如何使用

例如,创建一个 Laravel 的 Notification 表单

您也可以创建自定义通知

<?php

namespace IracodeCom\FilamentNotification\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use IracodeCom\FilamentNotification\Notifications\Channels\BaleChannel;
use IracodeCom\FilamentNotification\Notifications\Channels\FilamentChannel;
use IracodeCom\FilamentNotification\Notifications\Channels\FarazSmsPatternChannel;
use IracodeCom\FilamentNotification\Notifications\Channels\TelegramChannel;


class UserNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public function __construct( protected string $message, protected bool $bale = true, protected bool $telegram = true, protected bool $sms = true )
    {
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via( $notifiable ) : array
    {
        $channels = [ FilamentChannel::class ];

        if ( $notifiable->prefers_telegram && $this->telegram )
        {
            $channels[] = TelegramChannel::class;
        }

        if ( $notifiable->prefers_sms && $this->sms )
        {
            $channels[] = FarazSmsPatternChannel::class;
        }

        if ( $notifiable->prefers_bale && $this->bale )
        {
            $channels[] = BaleChannel::class;
        }

        return $channels;
    }


    public function toTelegram( $notifiable ) : array
    {
        return [
            'text' => $this->message,
        ];
    }

    public function toSms( $notifiable ) : array
    {
        return [
            'body' => $this->message,
        ];
    }

    public function toBale( $notifiable ) : array
    {
        return [
            'text' => $this->message,
        ];
    }

    public function toFilament( $notifiable ) : array
    {
        return [
            'body' => $this->message,
        ];
    }

}

示例代码使用

use IracodeCom\FilamentNotification\Notifications\UserNotification;

$user = \App\Models\User::find( 1 );
$user->notify(
    new UserNotification( 'Hello' )
);
use IracodeCom\FilamentNotification\Notifications\UserNotification;
use \App\Models\User;

foreach ( User::all() as $user )
{

    $user->notify( new UserNotification( 'Welcome' ) );

}

// Or

User::get()->each->notify( new UserNotification( 'Welcome' ) );

如果您想创建新的通知,可以使用此命令

php artisan make:notification YOUR_NOTIFICATION_NAME

设置 Telegram WebHook

运行此 URL

domain.com/telegram/set-webhook

安全性

如果您发现任何安全问题,请通过电子邮件 aliw1382@gmail.com 联系我们,而不是使用问题跟踪器。

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。