Ejemplo n.º 1
0
 public static function addBot($bot, Logger $logger)
 {
     if (is_array($bot) && !isset($bot['bot_token'])) {
         throw new \Exception('no_bot_token');
     }
     $loop = Factory::create();
     $client = new WebSocketClient('ws://0.0.0.0:12345/', $loop, $logger);
     $client->on("error", function () use($loop, $logger) {
         $logger->err("Add team incoming webhook : can't connect to websocket server");
         $loop->stop();
     });
     $client->on("connect", function () use($client, $loop, $logger, $bot) {
         $logger->info("Add team incoming webhook : " . json_encode($bot));
         $loop->addTimer(2, function () use($client, $loop, $bot) {
             $message = ['type' => 'add_bot', 'bot' => $bot];
             $client->send(json_encode($message));
             $loop->stop();
         });
     });
     $client->open();
     $loop->run();
 }
Ejemplo n.º 2
0
 public function addSlackClient(Bot $bot)
 {
     if (empty($bot->id)) {
         $this->logger->debug('Try to auth test for bot ' . $bot->bot_token);
         try {
             $auth = $this->authTest($bot->bot_token);
             $bot->setIds($auth->team_id, $auth->user_id);
         } catch (\Exception $e) {
             $this->logger->err('Exception in Bot ' . $bot->bot_token . ' connexion : ' . $e->getMessage() . "\"" . $e->getTraceAsString());
             return;
         }
     }
     $this->updateTeamBot($bot);
     if (!isset($this->bots_connected[$bot->id])) {
         $this->logger->debug('Try to start client for bot ' . $bot->id);
         $this->bots_connected[$bot->id] = false;
         try {
             $start = $this->rtmStart($bot);
             $this->publish($bot, ['type' => 'rtm_start', 'event' => $start]);
             $slack_client = new WebSocketClient($start->url, $this->loop, $this->logger);
             $slack_client->on("message", function (WebSocketMessage $message) use($bot) {
                 $data = json_decode($message->getData(), true);
                 if (!is_array($data) || !isset($data['type'])) {
                     $this->logger->info('Client for bot ' . $bot->id . " receive a message without type:\n" . $message->getData());
                     return;
                 }
                 if ($data['type'] == 'team_migration_started') {
                     $this->logger->notice("Team migration started for bot " . $bot->id);
                     $this->setToRetry($this->bots[$bot->id]);
                 } elseif ($data['type'] == 'message') {
                     $bot->updateRoomLatest($data['channel'], $data['ts']);
                     if (isset($data['subtype']) && $data['subtype'] == 'group_join') {
                         $bot->addRoomMember($data['channel'], $data['user']);
                     }
                     if (isset($data['subtype']) && $data['subtype'] == 'group_leave') {
                         $bot->removeRoomMember($data['channel'], $data['user']);
                     }
                     $this->publish($bot, ['type' => 'message', 'event' => $data]);
                     $this->updateTeamBot($bot);
                 } elseif (!in_array($data['type'], $this->config['events_excluded'])) {
                     if ($data['type'] == 'group_joined') {
                         $bot->rooms[$data['channel']['id']] = new Room(['members' => $data['channel']['members']]);
                     }
                     if ($data['type'] == 'group_left') {
                         unset($bot->rooms[$data['channel']]);
                     }
                     $this->publish($bot, ['type' => $data['type'], 'event' => $data]);
                     $this->updateTeamBot($bot);
                 }
             });
             $slack_client->on("close", function () use($bot) {
                 $this->logger->warn("Client closed for bot " . $bot->id);
                 $this->setToRetry($bot);
             });
             $slack_client->on("connect", function () use($bot, $slack_client) {
                 $this->logger->notice("Client connect for bot " . $bot->id);
                 $this->bots_connected[$bot->id] = true;
                 unset($this->bots_retrying[$bot->id]);
                 $bot->setClient($slack_client);
             });
             foreach ($start->channels as $channel) {
                 if (!$channel->is_archived && $channel->is_member) {
                     if (!$bot->isRoomUpToDate($channel->id, $channel->latest)) {
                         try {
                             $this->channelHistory($bot, $channel->id);
                         } catch (NoMoreTokenException $nmte) {
                             $this->publish($bot, ['type' => 'channel_recovery', 'channel' => $channel->id, 'latest' => $bot->rooms[$channel->id]->latest]);
                         }
                     }
                 }
             }
             foreach ($start->groups as $group) {
                 if (!$group->is_archived) {
                     $bot->initRoom($group->id, $group->members);
                     if (!$bot->isRoomUpToDate($group->id, $group->latest)) {
                         try {
                             $this->groupHistory($bot, $group->id);
                         } catch (NoMoreTokenException $nmte) {
                             $this->publish($bot, ['type' => 'group_recovery', 'group' => $group->id, 'latest' => $bot->rooms[$group->id]->latest]);
                         }
                     }
                 }
             }
             foreach ($start->ims as $im) {
                 if (!$bot->isRoomUpToDate($im->id, $im->latest)) {
                     $this->imHistory($bot, $im->id);
                 }
             }
             foreach ($start->mpims as $mpim) {
                 if (!$bot->isRoomUpToDate($mpim->id, $mpim->latest)) {
                     $this->mpimHistory($bot, $mpim->id);
                 }
             }
             $slack_client->open(15)->otherwise(function ($e) use($bot) {
                 if ($e instanceof \Exception) {
                     $this->logger->err('Client bot ' . $bot->id . ' fail to connect, promise exception during connexion : ' . get_class($e) . ' ' . $e->getMessage() . "\n" . $e->getTraceAsString());
                 } else {
                     $this->logger->err('Client bot ' . $bot->id . ' fail to connect, promise error during connexion' . $e);
                 }
                 $this->setToRetry($bot);
             });
         } catch (InvalidTokenException $ite) {
             $this->logger->err('Bot ' . $bot->id . ' removed.');
             $this->publish($bot, ['type' => 'bot_disabled']);
             $this->removeTeamBot($bot);
             unset($this->bots_connected[$bot->id]);
             unset($this->bots_retrying[$bot->id]);
         } catch (\Exception $e) {
             $this->logger->err('Exception in Bot ' . $bot->id . ' connexion : ' . $e->getMessage() . "\"" . $e->getTraceAsString());
             $this->bots_retrying[$bot->id] = true;
             unset($this->bots_connected[$bot->id]);
         }
     } else {
         if (!empty($this->bots_connected[$bot->id])) {
             $this->logger->debug('Client already connect for bot ' . $bot->id);
         } else {
             $this->logger->debug('Client connection already in progress for bot ' . $bot->id);
         }
     }
 }