3 namespace App\TwitchBot;
5 use App\Models\Channel;
6 use App\Models\TwitchBotCommand;
7 use App\Models\TwitchToken;
8 use Monolog\Handler\StreamHandler;
10 use Ratchet\Client\Connector;
11 use Ratchet\Client\WebSocket;
12 use Ratchet\RFC6455\Messaging\Message;
13 use React\EventLoop\Loop;
17 public function __construct($nick) {
19 $this->logger = new Logger('TwitchBot');
20 $this->logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));
22 $this->token = TwitchToken::firstWhere('nick', $nick);
24 throw new \Exception('unable to find access token');
26 if ($this->token->hasExpired()) {
27 $this->token->refresh();
30 $this->connector = new Connector();
35 public function getLogger() {
39 public function getLoop() {
43 public function isReady() {
47 public function run() {
48 $this->shutting_down = false;
49 $this->getLoop()->run();
52 public function stop() {
53 $this->logger->info('shutting down');
54 $this->shutting_down = true;
56 $this->getLoop()->stop();
60 public function connect() {
61 ($this->connector)('wss://irc-ws.chat.twitch.tv:443')->done(
62 [$this, 'handleWsConnect'],
63 [$this, 'handleWsConnectError'],
67 public function disconnect() {
71 public function handleWsConnect(WebSocket $ws) {
72 $this->logger->info('websocket connection established');
74 $ws->on('message', [$this, 'handleWsMessage']);
75 $ws->on('close', [$this, 'handleWsClose']);
76 $ws->on('error', [$this, 'handleWsError']);
77 $ws->send('CAP REQ :twitch.tv/tags twitch.tv/commands');
81 public function handleWsConnectError(WebSocket $ws) {
82 $this->logger->error('failed to establish websocket connection');
85 public function handleWsMessage(Message $message, WebSocket $ws) {
86 $irc_messages = explode("\r\n", rtrim($message->getPayload(), "\r\n"));
87 foreach ($irc_messages as $irc_message) {
88 $this->logger->info('received IRC message '.$irc_message);
89 $this->handleIRCMessage(IRCMessage::fromString($irc_message));
93 public function handleWsClose(int $op, string $reason) {
95 $this->logger->info('websocket connection closed: '.$reason.' ['.$op.']');
96 if (!$this->shutting_down) {
97 $this->logger->info('reconnecting in 5 seconds');
98 Loop::addTimer(5, [$this, 'connect']);
102 public function handleWsError(\Exception $e, WebSocket $ws) {
103 $this->logger->error('websocket error '.$e->getMessage());
107 public function handleIRCMessage(IRCMessage $msg) {
108 $this->last_contact = time();
109 if ($msg->isPing()) {
110 $this->sendIRCMessage($msg->makePong());
113 if ($msg->isPong()) {
116 $this->logMessage($msg);
117 if ($msg->isPrivMsg()) {
118 $this->handlePrivMsg($msg);
121 if ($msg->isWhisper()) {
122 $this->handleWhisper($msg);
125 if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
126 $this->logger->notice('login failed, refreshing access token');
127 $this->token->refresh();
131 if ($msg->command == '001') {
133 $this->joinChannels();
137 if ($msg->command == 'GLOBALUSERSTATE') {
138 // receive own user metadata
139 $this->handleUserState($msg);
144 public function getMessageChannel(IRCMessage $msg) {
145 $target = $msg->getPrivMsgTarget();
146 if (substr($target, 0, 1) !== '#') {
147 $target = '#'.$target;
149 return Channel::firstWhere('twitch_chat', '=', $target);
152 public function logMessage(IRCMessage $msg) {
155 public function handlePrivMsg(IRCMessage $msg) {
158 public function handleUserState(IRCMessage $msg) {
159 if (isset($msg->tags['user-id'])) {
160 $this->user_id = $msg->tags['user-id'];
164 public function handleWhisper(IRCMessage $msg) {
167 public function login() {
168 $this->ws->send('PASS oauth:'.$this->token->access);
169 $this->ws->send('NICK '.$this->nick);
172 public function joinChannels() {
175 private function startPinger() {
176 $this->getLoop()->addPeriodicTimer(15, function () {
177 if (!$this->ready) return;
178 if (time() - $this->last_contact < 60) return;
180 $this->sendIRCMessage(IRCMessage::ping($this->nick));
181 } catch (\Exception $e) {
186 public function sendIRCMessage(IRCMessage $msg) {
187 $irc_message = $msg->encode();
188 $this->logger->info('sending IRC message '.$irc_message);
189 $this->ws->send($irc_message);
190 $this->last_contact = time();
193 public function sendWhisper($to, $msg) {
194 $this->logger->info('sending whisper to '.$to.': '.$msg);
196 $response = $this->token->request()->post('/whispers?from_user_id='.$this->user_id.'&to_user_id='.$to, [
199 if (!$response->successful()) {
200 $this->logger->error('sending whisper to '.$to.': '.$response->status());
202 } catch (\Exception $e) {
203 $this->logger->error('sending whisper to '.$to.': '.$e->getMessage());
208 protected function listenCommands() {
209 $this->getLoop()->addPeriodicTimer(1, function () {
210 if (!$this->isReady()) return;
211 $command = TwitchBotCommand::where('bot_nick', '=', $this->nick)->where('status', '=', 'pending')->oldest()->first();
214 $command->execute($this);
215 } catch (\Exception $e) {
226 private $user_id = '';
230 private $ready = false;
231 private $shutting_down = false;
233 private $last_contact;