From 8a1e59dd6b1f3f6d567b89742316a17c1e41eebf Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 13:30:30 +0100 Subject: [PATCH] Implement command to read userdata from api in shell --- CHANGELOG | 1 + Command/UsersCommand.php | 74 ++++++++++++++++++++++++++++++ Resources/doc/commands.md | 12 +++++ Slack/Client/Actions/UsersList.php | 3 +- Slack/Users.php | 15 +++++- 5 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 Command/UsersCommand.php diff --git a/CHANGELOG b/CHANGELOG index 1603558..c64a1d7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ SlackBundle v1.3.0 - Feature: File upload service now accepts an array of channels, string usage is deprecated - Feature: Implement action to invite users to a channel - Feature: Implement action and service to get details about users on the team +- Feature: Implement console command to read userdata from api - Patch: File upload no longer need to lookup channel id due to api changes SlackBundle v1.2.2 diff --git a/Command/UsersCommand.php b/Command/UsersCommand.php new file mode 100644 index 0000000..03c91aa --- /dev/null +++ b/Command/UsersCommand.php @@ -0,0 +1,74 @@ +setName('dzunke:slack:users') + ->setDescription('work with the users of your team') + ->addOption('only-active', 'a', InputOption::VALUE_NONE, 'lists only active users') + ->addOption('only-deleted', 'd', InputOption::VALUE_NONE, 'lists only deleted users') + ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'get a single user'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $formatter = $this->getHelper('formatter'); + + try { + $api = $this->getContainer()->get('dz.slack.users'); + + if ($input->getOption('only-active')) { + $response = $api->getActiveUsers(); + } elseif ($input->getOption('only-deleted')) { + $response = $api->getDeletedUsers(); + } elseif ($input->getOption('user')) { + $response = $api->getUser($input->getOption('user')); + + if (is_array($response)) { + $response = [$response['name'] => $response]; + } + } else { + $response = $api->getUsers(); + } + + if (empty($response)) { + $output->writeln($formatter->formatBlock('✘ no data found', 'error')); + return; + } + + array_walk( + $response, + function (&$row) { + foreach ($row as &$col) { + if (is_bool($col)) { + $col = $col ? 'true' : 'false'; + } + } + } + ); + + $table = $this->getHelper('table'); + $table->setHeaders(array_keys(reset($response)))->setRows($response); + $table->render($output); + + + } catch (\Exception $e) { + $output->writeln( + $formatter->formatBlock( + sprintf('✘ there was an error with your request: "%s"', $e->getMessage()), + 'error' + ) + ); + } + } +} diff --git a/Resources/doc/commands.md b/Resources/doc/commands.md index 490d46b..7ec7777 100644 --- a/Resources/doc/commands.md +++ b/Resources/doc/commands.md @@ -27,3 +27,15 @@ php app/console dzunke:slack:channels:topic "C02GABTDT" "Lorem ipsum dolor sit a # If you don't have the ChannelId it must be discovered while processing php app/console dzunke:slack:channels:topic "#foo-channel" "Lorem ipsum dolor sit amet .." -d ``` + +## Read userdata from api + +you will get a table of userdata. + +``` bash +# Read all users from your team +php app/console dzunke:slack:users + +# Read a single user +php app/console dzunke:slack:users --user=nameOfTheUser +``` diff --git a/Slack/Client/Actions/UsersList.php b/Slack/Client/Actions/UsersList.php index 4de7771..815bd94 100644 --- a/Slack/Client/Actions/UsersList.php +++ b/Slack/Client/Actions/UsersList.php @@ -60,7 +60,8 @@ public function parseResponse(array $response) 'id' => $user['id'], 'name' => $user['name'], 'deleted' => (bool)$user['deleted'], - 'real_name' => $user['real_name'], + 'real_name' => $user['profile']['real_name'], + 'email' => $user['profile']['email'], 'is_bot' => (bool)$user['is_bot'], 'presence' => isset($user['presence']) ? $user['presence'] : null ]; diff --git a/Slack/Users.php b/Slack/Users.php index c117571..fa9770b 100644 --- a/Slack/Users.php +++ b/Slack/Users.php @@ -65,10 +65,21 @@ function ($user) { * @param string $name * @return null|array */ - public function getId($name) + public function getUser($name) { $users = $this->getUsers(); - return array_key_exists($name, $users) ? $users[$name]['id'] : null; + return array_key_exists($name, $users) ? $users[$name] : null; + } + + /** + * @param string $name + * @return null|array + */ + public function getId($name) + { + $user = $this->getUser($name); + + return !is_null($user) ? $user['id'] : null; } }