--- /dev/null
+<?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;
+ }
+
+}
+
+?>
--- /dev/null
+<?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;
+
+}
+
+?>