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; } ?>