From 05785a468b70381b37a1a2b4de2cf7469644700e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sj=C3=B6sten?= Date: Tue, 14 Jul 2015 17:15:47 +0100 Subject: [PATCH 01/11] Allow overriding icon_url and icon_emoji --- Slack/Client/Actions/ChatPostMessage.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Slack/Client/Actions/ChatPostMessage.php b/Slack/Client/Actions/ChatPostMessage.php index cd855bc..2bd6172 100644 --- a/Slack/Client/Actions/ChatPostMessage.php +++ b/Slack/Client/Actions/ChatPostMessage.php @@ -58,9 +58,13 @@ private function parseAttachments() private function parseIdentity() { - $this->parameter['username'] = $this->parameter['identity']->getUsername(); - $this->parameter['icon_url'] = $this->parameter['identity']->getIconUrl(); - $this->parameter['icon_emoji'] = $this->parameter['identity']->getIconEmoji(); + $this->parameter['username'] = $this->parameter['identity']->getUsername(); + if (empty($this->parameter['icon_url']) && $iconUrl = $this->parameter['identity']->getIconUrl()) { + $this->parameter['icon_url'] = $iconUrl; + } + if (empty($this->parameter['icon_emoji']) && $iconEmoji = $this->parameter['identity']->getIconEmoji()) { + $this->parameter['icon_emoji'] = $iconEmoji; + } unset($this->parameter['identity']); } From fbebf7717a0ebef42d3ddb0e91b02547762eab57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=A0ime=C4=8Dek?= Date: Wed, 26 Aug 2015 12:51:34 +0200 Subject: [PATCH 02/11] Message attachment extended to match full Slack's format. Fixed exception when wrong type is passed. --- .gitignore | 1 + Slack/Client/Actions/ChatPostMessage.php | 2 +- Slack/Entity/MessageAttachment.php | 196 ++++++++++++++++++++++- 3 files changed, 195 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b0def9a..bafea9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock composer.phar vendor +.idea diff --git a/Slack/Client/Actions/ChatPostMessage.php b/Slack/Client/Actions/ChatPostMessage.php index 2bd6172..8651acf 100644 --- a/Slack/Client/Actions/ChatPostMessage.php +++ b/Slack/Client/Actions/ChatPostMessage.php @@ -48,7 +48,7 @@ private function parseAttachments() $attachments = []; foreach ($this->parameter['attachments'] as $attachmentObj) { if (!$attachmentObj instanceof Attachment) { - throw new \Exception('atachments must be instance of \DZunke\SlackBundle\Slack\Messaging\Attachment'); + throw new \Exception('atachments must be instance of \DZunke\SlackBundle\Slack\Entity\MessageAttachment'); } $attachments[] = $attachmentObj->toArray(); diff --git a/Slack/Entity/MessageAttachment.php b/Slack/Entity/MessageAttachment.php index 1e5a0a1..2568140 100644 --- a/Slack/Entity/MessageAttachment.php +++ b/Slack/Entity/MessageAttachment.php @@ -4,6 +4,27 @@ class MessageAttachment { + /** + * Optional link to author icon + * + * @var string URL address + */ + protected $authorIcon; + + /** + * Optional link to author + * + * @var string URL address + */ + protected $authorLink; + + /** + * Optional name of author + * + * @var string + */ + protected $authorName; + /** * Can either be one of 'good', 'warning', 'danger', or any hex color code * @@ -19,6 +40,13 @@ class MessageAttachment */ protected $fallback; + /** + * Optional link to image + * + * @var string URL address + */ + protected $imageUrl; + /** * Optional text that should appear within the attachment * @@ -26,6 +54,27 @@ class MessageAttachment */ protected $text; + /** + * Optional title + * + * @var string + */ + protected $title; + + /** + * Optional title link + * + * @var string + */ + protected $titleLink; + + /** + * Optional link to thumbnail + * + * @var string + */ + protected $thumbUrl; + /** * Optional text that should appear above the formatted data * @@ -44,6 +93,63 @@ class MessageAttachment */ protected $fields = []; + /** + * @return string + */ + public function getAuthorIcon() + { + return $this->authorIcon; + } + + /** + * @param string $authorIcon + * @return $this + */ + public function setAuthorIcon($authorIcon) + { + $this->authorIcon = $authorIcon; + + return $this; + } + + /** + * @return string + */ + public function getAuthorLink() + { + return $this->authorLink; + } + + /** + * @param string $authorLink + * @return $this + */ + public function setAuthorLink($authorLink) + { + $this->authorLink = $authorLink; + + return $this; + } + + /** + * @return string + */ + public function getAuthorName() + { + return $this->authorName; + } + + /** + * @param string $authorName + * @return $this + */ + public function setAuthorName($authorName) + { + $this->authorName = $authorName; + + return $this; + } + /** * @param string $color * @return $this @@ -82,6 +188,25 @@ public function getFallback() return $this->fallback; } + /** + * @return string + */ + public function getImageUrl() + { + return $this->imageUrl; + } + + /** + * @param string $imageUrl + * @return $this + */ + public function setImageUrl($imageUrl) + { + $this->imageUrl = $imageUrl; + + return $this; + } + /** * @param array $fields * @return $this @@ -155,6 +280,63 @@ public function getText() return $this->text; } + /** + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param string $title + * @return $this + */ + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + /** + * @return string + */ + public function getTitleLink() + { + return $this->titleLink; + } + + /** + * @param string $titleLink + * @return $this + */ + public function setTitleLink($titleLink) + { + $this->titleLink = $titleLink; + + return $this; + } + + /** + * @return string + */ + public function getThumbUrl() + { + return $this->thumbUrl; + } + + /** + * @param string $thumbUrl + * @return $this + */ + public function setThumbUrl($thumbUrl) + { + $this->thumbUrl = $thumbUrl; + + return $this; + } + /** * @return array */ @@ -162,9 +344,17 @@ public function toArray() { return [ 'fallback' => $this->getFallback(), - 'pretext' => $this->getPretext(), - 'color' => $this->getColor(), - 'fields' => $this->getFields() + 'color' => $this->getColor(), + 'pretext' => $this->getPretext(), + 'author_name' => $this->getAuthorName(), + 'author_link' => $this->getAuthorLink(), + 'author_icon' => $this->getAuthorIcon(), + 'title' => $this->getTitle(), + 'title_link' => $this->getTitleLink(), + 'text' => $this->getText(), + 'fields' => $this->getFields(), + 'image_url' => $this->getImageUrl(), + 'thumb_url' => $this->getThumbUrl(), ]; } } From 5eafadf0beadd2b0f1a898e353400d7b70c40a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=A0ime=C4=8Dek?= Date: Wed, 26 Aug 2015 13:10:21 +0200 Subject: [PATCH 03/11] Fixed missing return tag on MessageAttachment --- Slack/Entity/MessageAttachment.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Slack/Entity/MessageAttachment.php b/Slack/Entity/MessageAttachment.php index 2568140..e4f64a5 100644 --- a/Slack/Entity/MessageAttachment.php +++ b/Slack/Entity/MessageAttachment.php @@ -232,6 +232,8 @@ public function addField($title, $text, $scale = false) 'value' => (string)$text, 'short' => $scale ]; + + return $this; } /** From 4832369f143788637e3de4271cbf6dfc0e07918a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=A0ime=C4=8Dek?= Date: Wed, 26 Aug 2015 14:09:56 +0200 Subject: [PATCH 04/11] Removed Idea ide folder from gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index bafea9d..b0def9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ composer.lock composer.phar vendor -.idea From 0a33e7264ff1ad5782507deec9a72b0d38a395ac Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 26 Aug 2015 18:17:09 +0200 Subject: [PATCH 05/11] Add information about extended message attachment to changelog --- CHANGELOG | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 4c81f7b..a15edc0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,10 @@ +SlackBundle v1.2.2 +================== +- Patch: MessageAttachment support complete attachment-api (by @shimmi) + SlackBundle v1.2.1 ================== -- Patch Feature: Customize icon_url and icon_emoji on single message-action +- Patch Feature: Customize icon_url and icon_emoji on single message-action (by @tobiassjosten) SlackBundle v1.2.0 ================== From b3ed4c39db25b554914aacb9f6a6e9959e74deb1 Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Mon, 12 Oct 2015 14:14:12 +0200 Subject: [PATCH 06/11] Remove channel lookup in file upload due to api change --- CHANGELOG | 5 +++++ Slack/Messaging.php | 17 +++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a15edc0..c20ae89 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +SlackBundle v1.3.0 +================== +- Feature: File upload service now accepts an array of channels, string usage is deprecated +- Patch: File upload no longer need to lookup channel id due to api changes + SlackBundle v1.2.2 ================== - Patch: MessageAttachment support complete attachment-api (by @shimmi) diff --git a/Slack/Messaging.php b/Slack/Messaging.php index 1120e5e..551ea53 100644 --- a/Slack/Messaging.php +++ b/Slack/Messaging.php @@ -72,7 +72,7 @@ public function message($channel, $message, $identity, array $attachments = []) /** - * @param string $channel + * @param string|array $channel # String is DEPRECATED scince v1.3 - Deliver Array * @param string $title * @param string $file * @param string|null $comment @@ -84,18 +84,15 @@ public function upload($channel, $title, $file, $comment = null) return false; } + if (!is_array($channel)) { + @trigger_error('Channel as String is deprecated scince v1.3, please deliver an Array of Channels', E_USER_DEPRECATED); + $channel = [$channel]; + } + $params = []; $params['title'] = $title; $params['initial_comment'] = $comment; - $params['channels'] = null; - - $channelDiscover = new Channels($this->client); - $channelId = $channelDiscover->getId($channel); - - if (!empty($channelId)) { - $params['channels'] = $channelId; - } - + $params['channels'] = implode(',', $channel); $params['content'] = file_get_contents($file); $params['fileType'] = mime_content_type($file); $params['filename'] = basename($file); From f87db0e46d674fbdf13a73b4e295cefa419338a5 Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 08:55:18 +0100 Subject: [PATCH 07/11] Add links to slack api methods in action classes --- Slack/Client/Actions/ApiTest.php | 3 +++ Slack/Client/Actions/AuthTest.php | 3 +++ Slack/Client/Actions/ChannelsHistory.php | 3 +++ Slack/Client/Actions/ChannelsInfo.php | 3 +++ Slack/Client/Actions/ChannelsList.php | 3 +++ Slack/Client/Actions/ChannelsSetTopic.php | 3 +++ Slack/Client/Actions/ChatPostMessage.php | 3 +++ Slack/Client/Actions/FilesUpload.php | 3 +++ 8 files changed, 24 insertions(+) diff --git a/Slack/Client/Actions/ApiTest.php b/Slack/Client/Actions/ApiTest.php index 6627e46..92238d9 100644 --- a/Slack/Client/Actions/ApiTest.php +++ b/Slack/Client/Actions/ApiTest.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/api.test + */ class ApiTest implements ActionsInterface { diff --git a/Slack/Client/Actions/AuthTest.php b/Slack/Client/Actions/AuthTest.php index 972c8fc..cf38265 100644 --- a/Slack/Client/Actions/AuthTest.php +++ b/Slack/Client/Actions/AuthTest.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/auth.test + */ class AuthTest implements ActionsInterface { diff --git a/Slack/Client/Actions/ChannelsHistory.php b/Slack/Client/Actions/ChannelsHistory.php index 2e43d41..025ec6b 100644 --- a/Slack/Client/Actions/ChannelsHistory.php +++ b/Slack/Client/Actions/ChannelsHistory.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/channels.history + */ class ChannelsHistory implements ActionsInterface { diff --git a/Slack/Client/Actions/ChannelsInfo.php b/Slack/Client/Actions/ChannelsInfo.php index 7703a8d..7ef01f9 100644 --- a/Slack/Client/Actions/ChannelsInfo.php +++ b/Slack/Client/Actions/ChannelsInfo.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/channels.info + */ class ChannelsInfo implements ActionsInterface { diff --git a/Slack/Client/Actions/ChannelsList.php b/Slack/Client/Actions/ChannelsList.php index 2e4bdb2..707daac 100644 --- a/Slack/Client/Actions/ChannelsList.php +++ b/Slack/Client/Actions/ChannelsList.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/channels.list + */ class ChannelsList implements ActionsInterface { diff --git a/Slack/Client/Actions/ChannelsSetTopic.php b/Slack/Client/Actions/ChannelsSetTopic.php index 0df3a2d..39315dd 100644 --- a/Slack/Client/Actions/ChannelsSetTopic.php +++ b/Slack/Client/Actions/ChannelsSetTopic.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/channels.setTopic + */ class ChannelsSetTopic implements ActionsInterface { diff --git a/Slack/Client/Actions/ChatPostMessage.php b/Slack/Client/Actions/ChatPostMessage.php index 8651acf..71964d4 100644 --- a/Slack/Client/Actions/ChatPostMessage.php +++ b/Slack/Client/Actions/ChatPostMessage.php @@ -6,6 +6,9 @@ use DZunke\SlackBundle\Slack\Entity\MessageAttachment as Attachment; use DZunke\SlackBundle\Slack\Messaging\Identity; +/** + * @see https://api.slack.com/methods/chat.postMessage + */ class ChatPostMessage implements ActionsInterface { /** diff --git a/Slack/Client/Actions/FilesUpload.php b/Slack/Client/Actions/FilesUpload.php index f2c1779..661d795 100644 --- a/Slack/Client/Actions/FilesUpload.php +++ b/Slack/Client/Actions/FilesUpload.php @@ -4,6 +4,9 @@ use DZunke\SlackBundle\Slack\Client\Actions; +/** + * @see https://api.slack.com/methods/files.upload + */ class FilesUpload implements ActionsInterface { From 8c720225b76b6b5fae923569a80ffbc940fb6b34 Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 09:19:41 +0100 Subject: [PATCH 08/11] Implement method to invite users to a channel --- CHANGELOG | 1 + Resources/doc/actions-list.md | 17 +++++++ Slack/Client/Actions.php | 2 + Slack/Client/Actions/ChannelsInvite.php | 65 +++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 Slack/Client/Actions/ChannelsInvite.php diff --git a/CHANGELOG b/CHANGELOG index c20ae89..c45eabd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,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 - Patch: File upload no longer need to lookup channel id due to api changes SlackBundle v1.2.2 diff --git a/Resources/doc/actions-list.md b/Resources/doc/actions-list.md index 2ca7ea0..3848ec3 100644 --- a/Resources/doc/actions-list.md +++ b/Resources/doc/actions-list.md @@ -38,6 +38,23 @@ protected $parameter = [ ]; ``` +## channels.invite + +[Slack Documentation](https://api.slack.com/methods/channels.invite) + +Constant: \DZunke\SlackBundle\Slack\Client::ACTION_CHANNELS_INVITE + +Available Parameters: + +Both Parameters have to be the if of the entity and not the raw name + +``` php +protected $parameter = [ + 'channel' => null, + 'user' => null, +]; +``` + ## channels.list [Slack Documentation](https://api.slack.com/methods/channels.list) diff --git a/Slack/Client/Actions.php b/Slack/Client/Actions.php index 163024a..9e88c03 100644 --- a/Slack/Client/Actions.php +++ b/Slack/Client/Actions.php @@ -12,6 +12,7 @@ class Actions const ACTION_AUTH_TEST = 'auth.test'; const ACTION_CHANNELS_SET_TOPIC = 'channels.setTopic'; const ACTION_CHANNELS_INFO = 'channels.info'; + const ACTION_CHANNELS_INVITE = 'channels.invite'; const ACTION_CHANNELS_HISTORY = 'channels.history'; const ACTION_FILES_UPLOAD = 'files.upload'; @@ -25,6 +26,7 @@ class Actions self::ACTION_AUTH_TEST => 'AuthTest', self::ACTION_CHANNELS_SET_TOPIC => 'ChannelsSetTopic', self::ACTION_CHANNELS_INFO => 'ChannelsInfo', + self::ACTION_CHANNELS_INVITE => 'ChannelsInvite', self::ACTION_CHANNELS_HISTORY => 'ChannelsHistory', self::ACTION_FILES_UPLOAD => 'FilesUpload' ]; diff --git a/Slack/Client/Actions/ChannelsInvite.php b/Slack/Client/Actions/ChannelsInvite.php new file mode 100644 index 0000000..a753db5 --- /dev/null +++ b/Slack/Client/Actions/ChannelsInvite.php @@ -0,0 +1,65 @@ + null, + 'user' => null + ]; + + /** + * @return array + * @throws \Exception + */ + public function getRenderedRequestParams() + { + if (is_null($this->parameter['channel']) || is_null($this->parameter['user'])) { + throw new \Exception('both parameters channel and user must be given'); + } + + return $this->parameter; + } + + /** + * @param array $parameter + * @return $this + */ + public function setParameter(array $parameter) + { + foreach ($parameter as $key => $value) { + if (array_key_exists($key, $this->parameter)) { + $this->parameter[$key] = $value; + } + } + + return $this; + } + + /** + * @return string + */ + public function getAction() + { + return Actions::ACTION_CHANNELS_INVITE; + } + + /** + * @param array $response + * @return array + */ + public function parseResponse(array $response) + { + return []; + } +} From 3be8ff7cc02cc218e2fadb7e27b3d61a7c45cf67 Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 10:29:01 +0100 Subject: [PATCH 09/11] Implement new users service to get detailed infos about the users on a team --- CHANGELOG | 1 + Resources/config/services.yml | 4 ++ Resources/doc/actions-list.md | 12 +++++ Resources/doc/services.md | 22 +++++++++ Slack/Client.php | 2 +- Slack/Client/Actions.php | 4 +- Slack/Client/Actions/UsersList.php | 71 ++++++++++++++++++++++++++++ Slack/Users.php | 74 ++++++++++++++++++++++++++++++ 8 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 Slack/Client/Actions/UsersList.php create mode 100644 Slack/Users.php diff --git a/CHANGELOG b/CHANGELOG index c45eabd..1603558 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,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 - Patch: File upload no longer need to lookup channel id due to api changes SlackBundle v1.2.2 diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 7111d19..85a0e42 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -17,3 +17,7 @@ services: dz.slack.channels: class: DZunke\SlackBundle\Slack\Channels arguments: [@dz.slack.client] + + dz.slack.users: + class: DZunke\SlackBundle\Slack\Users + arguments: [@dz.slack.client] diff --git a/Resources/doc/actions-list.md b/Resources/doc/actions-list.md index 3848ec3..2e7eea1 100644 --- a/Resources/doc/actions-list.md +++ b/Resources/doc/actions-list.md @@ -122,3 +122,15 @@ protected $parameter = [ 'channels' => null # If no Channel is given the File will be private to the API-User ]; ``` + +## users.list + +[Slack Documentation](https://api.slack.com/methods/users.list) + +Constant: \DZunke\SlackBundle\Slack\Client::ACTION_USERS_LIST + +``` php +protected $parameter = [ + 'presence' => 1 +]; +``` diff --git a/Resources/doc/services.md b/Resources/doc/services.md index 16bd40a..5999f8d 100644 --- a/Resources/doc/services.md +++ b/Resources/doc/services.md @@ -65,3 +65,25 @@ $service->info($channelId); $service->setTopic($channelId, $newTopic); ``` +## Users + +To get more information about the users in your team there is a service to read user specific things. In lack of an +api method to load single users every method of the service will load the complete list of users in your team. Beware +of using this service without caching informations you often need. + +``` php +$service = $container->get('dz.slack.users'); + +# get the list of all users in your team +$service->getUsers(); + +# get all users that are not deleted +$service->getActiveUsers(); + +# get all deleted users +$service->getDeletedUsers(); + +# get the id of a single user - needed for some actions +$service->getId($username); + +``` diff --git a/Slack/Client.php b/Slack/Client.php index 1bd6753..866f40e 100644 --- a/Slack/Client.php +++ b/Slack/Client.php @@ -30,7 +30,7 @@ public function __construct(Connection $connection) * @param array $parameter * @return Response|bool */ - public function send($action, array $parameter) + public function send($action, array $parameter = []) { if (!$this->connection->isValid()) { return false; diff --git a/Slack/Client/Actions.php b/Slack/Client/Actions.php index 9e88c03..1100ed5 100644 --- a/Slack/Client/Actions.php +++ b/Slack/Client/Actions.php @@ -15,6 +15,7 @@ class Actions const ACTION_CHANNELS_INVITE = 'channels.invite'; const ACTION_CHANNELS_HISTORY = 'channels.history'; const ACTION_FILES_UPLOAD = 'files.upload'; + const ACTION_USERS_LIST = 'users.list'; /** * @var array @@ -28,7 +29,8 @@ class Actions self::ACTION_CHANNELS_INFO => 'ChannelsInfo', self::ACTION_CHANNELS_INVITE => 'ChannelsInvite', self::ACTION_CHANNELS_HISTORY => 'ChannelsHistory', - self::ACTION_FILES_UPLOAD => 'FilesUpload' + self::ACTION_FILES_UPLOAD => 'FilesUpload', + self::ACTION_USERS_LIST => 'UsersList' ]; /** diff --git a/Slack/Client/Actions/UsersList.php b/Slack/Client/Actions/UsersList.php new file mode 100644 index 0000000..4de7771 --- /dev/null +++ b/Slack/Client/Actions/UsersList.php @@ -0,0 +1,71 @@ + 1 + ]; + + /** + * @return array + */ + public function getRenderedRequestParams() + { + return $this->parameter; + } + + /** + * @param array $parameter + * @return $this + */ + public function setParameter(array $parameter) + { + foreach ($parameter as $key => $value) { + if (array_key_exists($key, $this->parameter)) { + $this->parameter[$key] = $value; + } + } + + return $this; + } + + /** + * @return string + */ + public function getAction() + { + return Actions::ACTION_USERS_LIST; + } + + /** + * @param array $response + * @return array + */ + public function parseResponse(array $response) + { + $users = []; + foreach ($response['members'] as $user) { + $users[$user['name']] = [ + 'id' => $user['id'], + 'name' => $user['name'], + 'deleted' => (bool)$user['deleted'], + 'real_name' => $user['real_name'], + 'is_bot' => (bool)$user['is_bot'], + 'presence' => isset($user['presence']) ? $user['presence'] : null + ]; + } + + return $users; + } +} diff --git a/Slack/Users.php b/Slack/Users.php new file mode 100644 index 0000000..c117571 --- /dev/null +++ b/Slack/Users.php @@ -0,0 +1,74 @@ +client = $client; + } + + /** + * @return Client + */ + public function getClient() + { + return $this->client; + } + + /** + * @return array + */ + public function getUsers() + { + return $this->client->send(Actions::ACTION_USERS_LIST)->getData(); + } + + /** + * @return array + */ + public function getActiveUsers() + { + return array_filter( + $this->getUsers(), + function ($user) { + return $user['deleted'] === false; + } + ); + } + + /** + * @return array + */ + public function getDeletedUsers() + { + return array_filter( + $this->getUsers(), + function ($user) { + return $user['deleted'] === true; + } + ); + } + + /** + * @param string $name + * @return null|array + */ + public function getId($name) + { + $users = $this->getUsers(); + + return array_key_exists($name, $users) ? $users[$name]['id'] : null; + } +} From 8a1e59dd6b1f3f6d567b89742316a17c1e41eebf Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 13:30:30 +0100 Subject: [PATCH 10/11] 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; } } From db0247ccf1e67d8830bf16d245c04ae61c705e6d Mon Sep 17 00:00:00 2001 From: Denis Zunke Date: Wed, 28 Oct 2015 13:34:46 +0100 Subject: [PATCH 11/11] Give alias to commands for future remove of dzunke namespace --- CHANGELOG | 1 + Command/BotMessagingCommand.php | 1 + Command/ChangeChannelsTopicCommand.php | 1 + Command/DebugCommand.php | 1 + Command/MessageCommand.php | 1 + Command/UsersCommand.php | 1 + Resources/doc/commands.md | 16 ++++++++-------- 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c64a1d7..4490db5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ SlackBundle v1.3.0 - 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 +- Patch: Set aliases for command names - old names with dzunke: prefix will be removed in the future! SlackBundle v1.2.2 ================== diff --git a/Command/BotMessagingCommand.php b/Command/BotMessagingCommand.php index bc4edac..a0dd2bd 100644 --- a/Command/BotMessagingCommand.php +++ b/Command/BotMessagingCommand.php @@ -22,6 +22,7 @@ protected function configure() { $this ->setName('dzunke:slack:run-bot') + ->setAliases(['slack:run-bot']) ->setDescription('Running the Bot-User to a Channel') ->addArgument( 'channel', diff --git a/Command/ChangeChannelsTopicCommand.php b/Command/ChangeChannelsTopicCommand.php index 4da6472..cd4533a 100644 --- a/Command/ChangeChannelsTopicCommand.php +++ b/Command/ChangeChannelsTopicCommand.php @@ -15,6 +15,7 @@ protected function configure() { $this ->setName('dzunke:slack:channels:topic') + ->setAliases(['slack:channels:topic']) ->setDescription('Changing the Topic of a Channel') ->addOption('discover', 'd', InputOption::VALUE_NONE, 'channel name is given, so discover the id') ->addArgument('channel', InputArgument::REQUIRED, 'an existing channel in your team to change the topic') diff --git a/Command/DebugCommand.php b/Command/DebugCommand.php index d5911cc..98ccaab 100644 --- a/Command/DebugCommand.php +++ b/Command/DebugCommand.php @@ -31,6 +31,7 @@ protected function configure() { $this ->setName('dzunke:slack:debug') + ->setAliases(['slack:debug']) ->setDescription('Gives some Debug Informations about the SlackBundle'); } diff --git a/Command/MessageCommand.php b/Command/MessageCommand.php index 9cdf518..2e2aa84 100644 --- a/Command/MessageCommand.php +++ b/Command/MessageCommand.php @@ -14,6 +14,7 @@ protected function configure() { $this ->setName('dzunke:slack:message') + ->setAliases(['slack:message']) ->setDescription('Sending a Message to a Channel or User') ->addArgument('channel', InputArgument::REQUIRED, 'an existing channel in your team to send to') ->addArgument('username', InputArgument::REQUIRED, 'an username from configured identities to send with') diff --git a/Command/UsersCommand.php b/Command/UsersCommand.php index 03c91aa..5e3cf69 100644 --- a/Command/UsersCommand.php +++ b/Command/UsersCommand.php @@ -14,6 +14,7 @@ protected function configure() { $this ->setName('dzunke:slack:users') + ->setAliases(['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') diff --git a/Resources/doc/commands.md b/Resources/doc/commands.md index 7ec7777..822d859 100644 --- a/Resources/doc/commands.md +++ b/Resources/doc/commands.md @@ -5,7 +5,7 @@ Output of Debug Informations like Connection, Identities, Channels etc. ``` bash -php app/console dzunke:slack:debug +php app/console slack:debug ``` ## Messaging @@ -13,8 +13,8 @@ php app/console dzunke:slack:debug Sending a Message directly from Console ``` bash -php app/console dzunke:slack:message @fooUser BazUser "Lorem ipsum dolor sit amet .." -php app/console dzunke:slack:message "#FooChannel" BazUser "Lorem ipsum dolor sit amet .." +php app/console slack:message @fooUser BazUser "Lorem ipsum dolor sit amet .." +php app/console slack:message "#FooChannel" BazUser "Lorem ipsum dolor sit amet .." ``` ## Switch Topic of a Channel @@ -22,10 +22,10 @@ php app/console dzunke:slack:message "#FooChannel" BazUser "Lorem ipsum dolor si You can switch the Topic of a Channel ``` bash -php app/console dzunke:slack:channels:topic "C02GABTDT" "Lorem ipsum dolor sit amet .." +php app/console slack:channels:topic "C02GABTDT" "Lorem ipsum dolor sit amet .." # 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 +php app/console slack:channels:topic "#foo-channel" "Lorem ipsum dolor sit amet .." -d ``` ## Read userdata from api @@ -34,8 +34,8 @@ you will get a table of userdata. ``` bash # Read all users from your team -php app/console dzunke:slack:users +php app/console slack:users # Read a single user -php app/console dzunke:slack:users --user=nameOfTheUser -``` +php app/console slack:users --user=nameOfTheUser +```