]> git.localhorst.tv Git - alttp.git/commitdiff
twitch bot dummy
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 28 Feb 2023 16:17:33 +0000 (17:17 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 28 Feb 2023 16:17:33 +0000 (17:17 +0100)
.env.example
app/Console/Commands/TwitchBotCommand.php [new file with mode: 0644]
app/TwitchBot/TwitchBot.php [new file with mode: 0644]
config/twitch.php [new file with mode: 0644]

index f5fea82b8428f5a2a5210326b74d698e6a048821..ff8ed295432d272a595b6f7123b7318936f7a92e 100644 (file)
@@ -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 (file)
index 0000000..55ae34b
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\TwitchBot\TwitchBot;
+use Illuminate\Console\Command;
+
+class TwitchBotCommand extends Command {
+
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'twitch:bot';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Runs the twitch bot';
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle() {
+               $bot = new TwitchBot();
+
+               $bot->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 (file)
index 0000000..fd28378
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+
+namespace App\TwitchBot;
+use Illuminate\Support\Facades\Http;
+use Monolog\Handler\StreamHandler;
+use Monolog\Logger;
+use Ratchet\Client\Connector;
+use Ratchet\Client\WebSocket;
+use Ratchet\RFC6455\Messaging\Message;
+use React\EventLoop\Loop;
+
+class TwitchBot {
+
+       public function __construct() {
+               $this->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 (file)
index 0000000..6be7861
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+return [
+       'client_id' => env('TWITCH_CLIENT_ID', ''),
+       'client_secret' => env('TWITCH_CLIENT_SECRET', ''),
+       'code' => env('TWITCH_CODE', ''),
+       'redirect_uri' => env('TWITCH_REDIRECT_URI', ''),
+];