katzen48 / laravel-twitch-eventsub
本包提供订阅Twitch EventSub的支持。它基于laravel-twitch,并且完全兼容。
Requires
- php: ^8.0
- romanzipp/laravel-twitch: ^4.0.10
Requires (Dev)
- laravel/framework: ^v8.0
- laravel/pint: ^0.2.0 || ^1.0.0
README
简介
本包提供订阅Twitch EventSub的支持。它基于laravel-twitch,并且完全兼容。
特性
- 通过webhooks创建Twitch EventSub订阅
- 通过专用类按事件订阅接收的事件
- 在特定模型保存到数据库时,定义要订阅的事件
安装
首先,安装laravel-twitch。
请参阅其文档。
安装和配置laravel-twitch后,使用composer安装laravel-twitch-eventsub
composer require katzen48/laravel-twitch-eventsub
可以使用以下命令发布配置
php artisan vendor:publish --tag=config
配置
发布配置后,您可以在config/twitch-eventsub.php中找到它,其外观如下
<?php return [ 'callback_url' => env('TWITCH_HELIX_EVENTSUB_CALLBACK_URL', '/twitch/eventsub/webhook'), // Endpoint, the webhooks get sent to ];
消费EventSub事件
即使您不使用此库订阅eventsub,也会注册一个路由,默认为{APP_URL}/twitch/eventsub/webhook。当您订阅EventSub并使用此URL作为回调时,接收的事件将被解析到专用的事件类中,事件监听器可以单独订阅这些类。
要订阅事件,首先使用php artisan make:listener <Name>创建一个监听器,并将其应订阅的事件作为参数添加到handle()函数。
<?php namespace App\Listeners; use katzen48\Twitch\EventSub\Events\Channel\Subscription\ChannelSubscriptionGiftEvent; class SubscriptionListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param object $event * @return void */ public function handle(ChannelSubscriptionGiftEvent $event) { // Do something } }
要将监听器绑定到事件,将监听器添加到您的EventServiceProvider中的$listen数组
<?php namespace App\Providers; use App\Listeners\SubscriptionListener;use Illuminate\Auth\Events\Registered;use Illuminate\Auth\Listeners\SendEmailVerificationNotification;use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;use katzen48\Twitch\EventSub\Events\Channel\Subscription\ChannelSubscriptionGiftEvent; class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ Registered::class => [ SendEmailVerificationNotification::class, ], ChannelSubscriptionGiftEvent::class => [ // The Event, to subscribe to SubscriptionListener::class, // The Listener ], ]; /** * Register any events for your application. * * @return void */ public function boot() { // } }
就这样。您的监听器现在可以接收到来自Twitch EventSub的类型为channel.subscribe的传入事件。
在模型保存时订阅Twitch EventSub
有时需要新创建的模型实例触发对Twitch EventSub的订阅,例如首次登录的用户。此行为可以通过静态SubscribesEventSubs特质自动化。
将特质SubscribesEventSubs添加到您的模型中。接下来,在模型中添加一个名为$eventSubs的静态变量,其中包含要订阅的事件及其与模型属性的条件映射。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use katzen48\Twitch\EventSub\Traits\SubscribesEventSubs; use romanzipp\Twitch\Enums\EventSubType; class User extends Authenticatable { use HasFactory, Notifiable, SubscribesEventSubs; public static array $eventSubs = [ EventSubType::CHANNEL_UPDATE => [ // The Event Type (from laravel-twitch) 'broadcaster_user_id' => 'id', // The conditions from the EventSub documentation and the model attributes ], ]; protected $keyType = 'string'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'id', 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'id' => 'string', 'email_verified_at' => 'datetime', ]; }
现在,每当该模型的created事件被触发时,就会创建对channel.update事件类型的订阅。模型的id属性将自动插入到broadcaster_user_id中。