Skip to content

Instantly share code, notes, and snippets.

@DanielHe4rt
Created January 3, 2022 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielHe4rt/2bfd7b1930fbe323b89e82445e864ec0 to your computer and use it in GitHub Desktop.
Save DanielHe4rt/2bfd7b1930fbe323b89e82445e864ec0 to your computer and use it in GitHub Desktop.
Twitch Service
<?php
namespace App\Services;
use App\Contracts\OAuthServiceContract;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
class TwitchService implements OAuthServiceContract
{
private $client;
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://api.twitch.tv/kraken/',
'timeout' => 10.0
]);
}
public function auth(string $code): array
{
$uri = "https://id.twitch.tv/oauth2/token";
$response = $this->client->request('POST', $uri, [
'form_params' => [
'client_id' => env('TWITCH_OAUTH_ID'),
'client_secret' => env('TWITCH_OAUTH_SECRET'),
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => env('TWITCH_REDIRECT_STREAMER_URI')
]
]);
return json_decode($response->getBody(), true);
}
public function getAuthenticatedUser(string $token): array
{
$uri = "/helix/users";
try {
$response = $this->client->request('GET', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/vnd.twitchtv.v5+json'
]
]);
$result = json_decode($response->getBody(), true)['data'][0];
return [
'id' => $result['id'],
'email' => $result['email'],
'name' => $result['login'],
'image_url' => $result['profile_image_url'],
'elegible' => in_array($result['broadcaster_type'], ['affiliate', 'partner', 'staff'])
];
} catch (\Exception $exception) {
dd($exception->getMessage());
}
}
public function getStreamInfo(int $channelId = 227168488): array
{
$uri = "/kraken/streams/$channelId";
try {
$response = $this->client->request('GET', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json'
],
]);
return json_decode($response->getBody(), true);
} catch (GuzzleException $e) {
Log::alert('[Dev Stream Error] ' . $e->getMessage() );
return [];
}
}
public function fetchSubscriptions($page = ''): array
{
$uri = "/helix/subscriptions?broadcaster_id=" . env('TWITCH_BROADCASTER_ID') . "&first=100" . (!empty($page) ? '&after=' . $page : "");
try {
$response = $this->client->request('GET', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . env('TWITCH_BROADCASTER_TOKEN')
],
]);
return json_decode($response->getBody(), true);
} catch (GuzzleException $e) {
return [
'message' => $e->getMessage()
];
}
}
public function createReward(string $token, int $streamerId, $name = "Game Megaphone"): array
{
$uri = "/helix/channel_points/custom_rewards?broadcaster_id=" . $streamerId;
$response = $this->client->request('POST', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . $token
],
'form_params' => [
'title' => $name,
'cost' => 10,
'prompt' => 'This is some megaphone that show your character on the screen =). Use it wisely.',
'is_user_input_required' => true
]
]);
return json_decode($response->getBody(), true)['data'][0];
}
public function fetchRewards(string $token, int $streamerId): array
{
$uri = "/helix/channel_points/custom_rewards?broadcaster_id=" . $streamerId;
$response = $this->client->request('GET', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . $token
]
]);
return json_decode($response->getBody(), true);
}
public function deleteReward(string $token, int $streamerId, string $rewardId): bool
{
$uri = "/helix/channel_points/custom_rewards?broadcaster_id=" . $streamerId . "&id=" . $rewardId;
try {
$this->client->request('DELETE', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . $token
]
]);
return true;
} catch (\Exception $e) {
return false;
}
}
public function registerCustomRedemptionWebhook($streamerId, $customWebhook): array
{
$uri = '/helix/eventsub/subscriptions';
$response = $this->client->request('POST', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . env('TWITCH_APP_KEY')
],
'json' => [
'type' => 'channel.channel_points_custom_reward_redemption.add',
'version' => '1',
'condition' => [
'broadcaster_user_id' => "$streamerId",
'reward_id' => "$customWebhook"
],
'transport' => [
'method' => 'webhook',
'callback' => env('APP_URL') . '/auth/webhook/challenge',
// 'callback' => 'https://basementheroes.live/auth/webhook/challengxe',
'secret' => 'eae123fodase'
]
]
]);
return json_decode($response->getBody(), true);
}
public function registerStreamStatusWebhook($streamerId, string $status): array
{
$uri = '/helix/eventsub/subscriptions';
try {
$response = $this->client->request('POST', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . env('TWITCH_APP_KEY')
],
'json' => [
'type' => 'streamer.' . $status,
'version' => '1',
'condition' => [
'broadcaster_user_id' => $streamerId
],
'transport' => [
'method' => 'webhook',
'callback' => env('APP_URL') . '/auth/webhook/challenge',
'secret' => 'eae123fodase'
]
]
]);
return json_decode($response->getBody(), true);
} catch (GuzzleException $e) {
return [
'message' => $e->getMessage()
];
}
}
public function fetchWebhooks(): array
{
$uri = '/helix/eventsub/subscriptions';
try {
$response = $this->client->request('GET', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . env('TWITCH_APP_KEY')
]
]);
return json_decode($response->getBody(), true);
} catch (GuzzleException $e) {
return [
'message' => $e->getMessage()
];
}
}
public function deleteStreamStatusWebhook($eventsubId)
{
$uri = '/helix/eventsub/subscriptions?id=' . $eventsubId;
$response = $this->client->request('DELETE', $uri, [
'headers' => [
'Client-ID' => env('TWITCH_OAUTH_ID'),
'Accept' => 'application/vnd.twitchtv.v5+json',
'Authorization' => 'Bearer ' . env('TWITCH_APP_KEY')
]
]);
return json_decode($response->getBody(), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment