示例#1
0
 public function handle()
 {
     $token = Seat::get('slack_token');
     if ($token == null) {
         throw new SlackSettingException("missing slack_token in settings");
     }
     // call rtm method in order to get a fresh new WSS uri
     $api = new SlackApi($token);
     $wss = $api->rtmStart();
     // start a loop event which will handle RTM daemon
     $loop = Factory::create();
     $connector = new Connector($loop);
     // prepare the event catcher (we only care about members join and channels)
     $connector($wss)->then(function (WebSocket $conn) {
         // trigger on RTM message event
         $conn->on('message', function (MessageInterface $msg) use($conn) {
             // since Slack RTM return json message, decode it first
             $slackMessage = json_decode($msg, true);
             // then, process to channel, groups and member case
             switch ($slackMessage['type']) {
                 // if the event is of type "team_join", then update our Slack user table using the new slack user id
                 // common element between SeAT and Slack is mail address
                 case 'team_join':
                     $this->newMember($slackMessage['user']);
                     break;
                     // if the event is of type "channel_created"
                     // then update our Slack channel table using new slack channel id
                 // if the event is of type "channel_created"
                 // then update our Slack channel table using new slack channel id
                 case 'group_joined':
                 case 'channel_created':
                     $this->createChannel($slackMessage['channel']);
                     break;
                     // if the event is of type "channel_delete", then remove the record from our Slack channel table
                 // if the event is of type "channel_delete", then remove the record from our Slack channel table
                 case 'channel_deleted':
                 case 'group_archive':
                     SlackChannel::destroy($slackMessage['channel']);
                     break;
                 case 'group_unarchive':
                     Log::debug('[Slackbot][Daemon][group_unarchive] ' . print_r($slackMessage, true));
                     $this->restoreGroup($slackMessage['channel']);
                     break;
                 case 'channel_rename':
                 case 'group_rename':
                     $this->renameChannel($slackMessage['channel']);
                     break;
             }
         });
     }, function (\Exception $e) use($loop) {
         echo $e->getMessage();
         $loop->stop();
     });
     $loop->run();
     return;
 }