iracode-com / filament-notification
1.4.2
2024-07-07 08:18 UTC
Requires
- php: >=8.1
- filament/filament: >=3.2
- guzzlehttp/guzzle: ^7.8
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- irazasyed/telegram-bot-sdk: ^3.14
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.16.4
- tzsk/sms: ^8.0
Requires (Dev)
- ext-soap: *
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^7.0|^8.0|^9.0
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)。请参阅 许可证文件 了解更多信息。