Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
Implement command to read userdata from api in shell
Browse files Browse the repository at this point in the history
  • Loading branch information
DZunke committed Oct 28, 2015
1 parent 1975052 commit 8a1e59d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions Command/UsersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace DZunke\SlackBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UsersCommand extends ContainerAwareCommand
{

protected function configure()
{
$this
->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'
)
);
}
}
}
12 changes: 12 additions & 0 deletions Resources/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 2 additions & 1 deletion Slack/Client/Actions/UsersList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
Expand Down
15 changes: 13 additions & 2 deletions Slack/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 8a1e59d

Please sign in to comment.