From c4835dcd53401c4e618ba077726d655d476a82c8 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 28 Feb 2023 17:17:33 +0100 Subject: [PATCH] twitch bot dummy --- .env.example | 5 + app/Console/Commands/TwitchBotCommand.php | 43 +++++++++ app/TwitchBot/TwitchBot.php | 107 ++++++++++++++++++++++ config/twitch.php | 8 ++ 4 files changed, 163 insertions(+) create mode 100644 app/Console/Commands/TwitchBotCommand.php create mode 100644 app/TwitchBot/TwitchBot.php create mode 100644 config/twitch.php diff --git a/.env.example b/.env.example index f5fea82..ff8ed29 100644 --- a/.env.example +++ b/.env.example @@ -69,3 +69,8 @@ AOS_CLI= AOS_HOSTNAME=aos.localhorst.tv AOS_SURGE_URL=https://aosrando.surge.sh AOS_URL=https://aos.localhorst.tv + +TWITCH_CLIENT_ID= +TWITCH_CLIENT_SECRET= +TWITCH_CODE= +TWITCH_REDIRECT_URI= diff --git a/app/Console/Commands/TwitchBotCommand.php b/app/Console/Commands/TwitchBotCommand.php new file mode 100644 index 0000000..55ae34b --- /dev/null +++ b/app/Console/Commands/TwitchBotCommand.php @@ -0,0 +1,43 @@ +getLoop()->addSignal(SIGINT, function() use ($bot) { + $bot->stop(); + }); + + $bot->run(); + + return 0; + } + +} + +?> diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php new file mode 100644 index 0000000..fd28378 --- /dev/null +++ b/app/TwitchBot/TwitchBot.php @@ -0,0 +1,107 @@ +logger = new Logger('TwitchBot'); + $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO)); + + $this->fetchToken(); + + $this->connector = new Connector(); + $this->connect(); + } + + public function getLogger() { + return $this->logger; + } + + public function getLoop() { + return Loop::get(); + } + + public function run() { + $this->getLoop()->run(); + } + + public function stop() { + $this->disconnect(); + $this->getLoop()->stop(); + } + + + public function fetchToken() { + $this->logger->info('acquiring token'); + $rsp = Http::post('https://id.twitch.tv/oauth2/token', [ + 'client_id' => config('twitch.client_id'), + 'client_secret' => config('twitch.client_secret'), + 'code' => config('twitch.code'), + 'grant_type' => 'authorization_code', + 'redirect_uri' => config('twitch.redirect_uri'), + ]); + var_dump($rsp); + var_dump($rsp->body()); + $this->token = $rsp->json(); + } + + + public function connect() { + ($this->connector)('wss://irc-ws.chat.twitch.tv:443')->done( + [$this, 'handleWsConnect'], + [$this, 'handleWsConnectError'], + ); + } + + public function disconnect() { + $this->ws->close(); + } + + public function handleWsConnect(WebSocket $ws) { + $this->logger->info('websocket connection estblished'); + $this->ws = $ws; + $ws->on('message', [$this, 'handleWsMessage']); + $ws->on('close', [$this, 'handleWsClose']); + $ws->on('error', [$this, 'handleWsError']); + $ws->send('CAP REQ :twitch.tv/tags twitch.tv/commands'); + $ws->send('PASS oauth:'.$this->token->access_token); + $ws->send('NICK localhorsttv'); + } + + public function handleWsConnectError(WebSocket $ws) { + $this->logger->error('failed to establish websocket connection'); + } + + public function handleWsMessage(Message $message, WebSocket $ws) { + $this->logger->info('websocket message received'); + var_dump($message->getPayload()); + } + + public function handleWsClose(int $op, string $reason) { + $this->logger->info('websocket connection closed: '.$reason.' ['.$op.']'); + } + + public function handleWsError(\Exception $e, WebSocket $ws) { + $this->logger->error('websocket error '.$e->getMessage()); + } + + + private $logger; + private $loop; + + private $token; + + private $connector; + private $ws; + +} + +?> diff --git a/config/twitch.php b/config/twitch.php new file mode 100644 index 0000000..6be7861 --- /dev/null +++ b/config/twitch.php @@ -0,0 +1,8 @@ + env('TWITCH_CLIENT_ID', ''), + 'client_secret' => env('TWITCH_CLIENT_SECRET', ''), + 'code' => env('TWITCH_CODE', ''), + 'redirect_uri' => env('TWITCH_REDIRECT_URI', ''), +]; -- 2.39.2