PHP code example of ghostzero / twitch-toolkit

1. Go to this page and download the library: Download ghostzero/twitch-toolkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ghostzero / twitch-toolkit example snippets


use GhostZero\TwitchToolkit\Utils\TwitchUserResolver;
use romanzipp\Twitch\Twitch;

// Fetch the user information from twitch based on the login or id.
// This response will be cached for 12h (because of rate limits).
if ($user = TwitchUserResolver::fetchUser('name or id', app(Twitch::class))) {
	$this->info("Fetched user {$user->login} successfully!");
}

public function boot()  
{  
	...
	
	TwitchExtGuard::register(config('twitch-api.ext_secret'), new TwitchUserProvider);  
}

namespace App\Utils;

use App\User;
use GhostZero\TwitchToolkit\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
  
class TwitchUserProvider extends UserProvider  
{  
	public function retrieveById($identifier): ?Authenticatable  
	{
		/** @var User $user */  
		$user = User::query()->whereKey($identifier)->first();  

		return $user;  
	}  

	public function createFromTwitchToken($decoded): ?Authenticatable  
	{
		return User::createFromTwitchToken($decoded);  
	}  
}



return [
    ...
    'guards' => [
        ...
        'twitch' => [
            'driver' => 'twitch',
            'provider' => 'users', // this is wrong, will be fixed soon
        ],
    ],
    ...
];


$this->middleware('auth:twitch')->only(['update']);

$schedule->command('twitch-toolkit:poll')->everyFiveMinutes();

use GhostZero\TwitchToolkit\Events\WebhookWasCalled;

protected $listen = [  
	StreamUp::class => [  
		StreamUpListener::class,  
	],
	StreamDown::class => [  
		StreamDownListener::class,  
	],
	...
];

$schedule->command('twitch-toolkit:subscribe-webhooks')->everyMinute();

use GhostZero\TwitchToolkit\Events\WebhookWasCalled;

protected $listen = [  
	WebhookWasCalled::class => [  
		StoreWebhookActivity::class,  
	],
	...
];

namespace App\Listeners;

use GhostZero\TwitchToolkit\Events\WebhookWasCalled;  
  
class StoreWebhookActivity  
{  
	public function handle(WebhookWasCalled $event): void  
	{
		// business logic  
    }  
}

use GhostZero\TwitchToolkit\Models\Channel as WebhookChannel;

public function boot(): void
{
	// sometimes twitch-toolkit nction (WebhookChannel $channel) {
        // fetch your user access tokens and fill them in the channel object        
        $channel->forceFill([
            'broadcaster_type' => '...',
            'oauth_access_token' => '...',
            'oauth_refresh_token' => '...',
            'oauth_expires_at' => new CarbonImmutable('...'),
        ])->save();
    });
}

namespace App\Models;

use GhostZero\TwitchToolkit\Models\Channel as WebhookChannel;  
use GhostZero\TwitchToolkit\Models\WebSub;

/**
 * @property string id  
 * @property WebhookChannel webhook_channel  
 * @property WebSub[] web_subs  
 */
class Channel extends Model  
{
	public function webhook_channel(): BelongsTo  
	{  
		return $this->belongsTo(WebhookChannel::class, 'id');  
	}  
	  
	public function web_subs(): HasMany  
	{  
		return $this->hasMany(WebSub::class, 'channel_id');  
	}
}

use GhostZero\TwitchToolkit\Jobs\SubscribeTwitchWebhooks;  
use GhostZero\TwitchToolkit\Models\Channel as WebhookChannel;

// creates a twitch webhook subscription  
$webhookChannel = WebhookChannel::subscribe($channel->getKey(), [  
	WebhookChannel::OPTION_CAPABILITIES => $attributes['capabilities'],  
	WebhookChannel::OPTION_BROADCASTER_TYPE => $attributes['broadcaster_type'],  
]);  
  
if (in_array(WebhookChannel::TYPE_WEBHOOK, $attributes['capabilities'], true)) {  
	dispatch_now(new SubscribeTwitchWebhooks($webhookChannel));  
}  

// check webhook setup
if ($channel->web_subs()->count() < 2) {  
	return response()->json([  
		'message' => 'We need at least 2 WebSubs from Twitch.'  
	], 409);  
}  

// check token generation
if (!$webhookChannel->oauth_access_token) {  
	return response()->json([  
		'message' => 'We couldn\'t get a Twitch access token from our SSO.'  
	], 409);  
}  
  
return response('', 204);