├── tests
└── .gitkeep
├── .gitignore
├── src
└── Vluzrmos
│ └── SlackApi
│ ├── Contracts
│ ├── SlackOAuth.php
│ ├── SlackRealTimeMessage.php
│ ├── SlackOAuthV2.php
│ ├── SlackTeam.php
│ ├── SlackStar.php
│ ├── SlackGroup.php
│ ├── SlackSearch.php
│ ├── SlackUserAdmin.php
│ ├── SlackFile.php
│ ├── SlackUser.php
│ ├── SlackChat.php
│ ├── SlackApi.php
│ ├── SlackInstantMessage.php
│ └── SlackChannel.php
│ ├── config
│ └── slack-api.php
│ ├── Facades
│ ├── SlackApi.php
│ ├── SlackChat.php
│ ├── SlackFile.php
│ ├── SlackStar.php
│ ├── SlackTeam.php
│ ├── SlackUser.php
│ ├── SlackGroup.php
│ ├── SlackOAuth.php
│ ├── SlackChannel.php
│ ├── SlackSearch.php
│ ├── SlackOAuthV2.php
│ ├── SlackUserAdmin.php
│ ├── SlackInstantMessage.php
│ └── SlackRealTimeMessage.php
│ ├── helpers.php
│ ├── Methods
│ ├── RealTimeMessage.php
│ ├── OAuth.php
│ ├── OAuthV2.php
│ ├── Team.php
│ ├── Star.php
│ ├── UserAdmin.php
│ ├── Group.php
│ ├── Search.php
│ ├── InstantMessage.php
│ ├── File.php
│ ├── SlackMethod.php
│ ├── Chat.php
│ ├── User.php
│ └── Channel.php
│ ├── SlackApiServiceProvider.php
│ └── SlackApi.php
├── .travis.yml
├── phpunit.xml
├── composer.json
├── .php_cs
└── readme.md
/tests/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | /.idea
3 | composer.phar
4 | composer.lock
5 | .DS_Store
6 | tmp*.tmp
--------------------------------------------------------------------------------
/src/Vluzrmos/SlackApi/Contracts/SlackOAuth.php:
--------------------------------------------------------------------------------
1 | false
8 | ];
9 |
--------------------------------------------------------------------------------
/src/Vluzrmos/SlackApi/Contracts/SlackTeam.php:
--------------------------------------------------------------------------------
1 | load($method) : $slack;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Vluzrmos/SlackApi/Methods/RealTimeMessage.php:
--------------------------------------------------------------------------------
1 | method('start');
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
37 | * [ 38 | * "sort_dir" => desc, //Change sort direction to ascending (asc) or descending (desc). 39 | * "highlight" => 1, //Pass a value of 1 to enable query highlight markers (see below). 40 | * "count" => 100, //Number of items to return per page. 41 | * "page" => 1 //Page number of results to return. 42 | * ] 43 | *44 | *@return array 45 | */ 46 | public function files($query, $sort = 'timestamp', $options = []) 47 | { 48 | return $this->method('files', array_merge(compact('query', 'sort'), $options)); 49 | } 50 | 51 | /** 52 | * This method returns messages matching a search query. 53 | * 54 | * @param string $query 55 | * @param string $sort 56 | * @param array $options
57 | * [ 58 | * "sort_dir" => desc, //Change sort direction to ascending (asc) or descending (desc). 59 | * "highlight" => 1, //Pass a value of 1 to enable query highlight markers (see below). 60 | * "count" => 100, //Number of items to return per page. 61 | * "page" => 1 //Page number of results to return. 62 | * ] 63 | *64 | *@return array 65 | */ 66 | public function messages($query, $sort = 'timestamp', $options = []) 67 | { 68 | return $this->method('messages', array_merge(compact('query', 'sort'), $options)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/Methods/InstantMessage.php: -------------------------------------------------------------------------------- 1 | method('close', compact('channel')); 21 | } 22 | 23 | /** 24 | * This method returns a portion of messages/events from the specified channel. 25 | * To read the entire history for a channel, call the method with no `latest` or `oldest` arguments, 26 | * and then continue paging using the instructions below. 27 | * @see https://api.slack.com/methods/conversations.history 28 | * 29 | * @param string $channel Channel to fetch history for. 30 | * @param int $limit Number of messages to return, between 1 and 1000. 31 | * @param string $latest End of time range of messages to include in results. 32 | * @param int|string $oldest Start of time range of messages to include in results. 33 | * @param int $inclusive Include messages with latest or oldest timestamp in results. 34 | * 35 | * @return array 36 | */ 37 | public function history($channel, $limit = 100, $latest = null, $oldest = 0, $inclusive = 1) 38 | { 39 | return $this->method('history', compact('channel', 'limit', 'latest', 'oldest', 'inclusive')); 40 | } 41 | 42 | /** 43 | * This method returns a list of all im channels that the user has. 44 | * 45 | * @return array 46 | */ 47 | public function lists() 48 | { 49 | return $this->method('list'); 50 | } 51 | 52 | /** 53 | * This method returns a list of all im channels that the user has. 54 | * 55 | * @return array 56 | */ 57 | public function all() 58 | { 59 | return $this->method('list'); 60 | } 61 | 62 | /** 63 | * This method moves the read cursor in a direct message channel. 64 | * 65 | * @param string $channel 66 | * @param int|string $ts 67 | * 68 | * @return array 69 | */ 70 | public function mark($channel, $ts) 71 | { 72 | return $this->method('mark', compact('channel', 'ts')); 73 | } 74 | 75 | /** 76 | * This method opens a direct message channel with another member of your Slack team. 77 | * @param string $user 78 | * 79 | * @return array 80 | */ 81 | public function open($user) 82 | { 83 | return $this->method('open', compact('user')); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/Methods/File.php: -------------------------------------------------------------------------------- 1 | method('delete', compact('file')); 21 | } 22 | 23 | /** 24 | * This method returns information about a file in your team. 25 | * 26 | * @param string $file File to fetch info for 27 | * @param array $options ['count' = 100, 'page' => 1] 28 | */ 29 | public function info($file, $options = []) 30 | { 31 | $this->method('info', array_merge(compact('file'), $options)); 32 | } 33 | 34 | /** 35 | * This method returns a list of files within the team. It can be filtered and sliced in various ways. 36 | * 37 | * @param array $options
38 | * [ 39 | * "user" => "U123S567D90", //Filter files created by a single user. 40 | * "ts_from" => 123456789, //Filter files created after this timestamp (inclusive). 41 | * "ts_to" => "now", //Filter files created before this timestamp (inclusive). 42 | * "types" => "all", //Filter files by type: all, posts, snippets, images, gdocs, zips, pdfs 43 | * //You can pass multiple values in the types argument, like types=posts,snippets.The default value is all, which does not filter the list. 44 | * "count" => 100, //Number of items to return per page. 45 | * "page" => 1 //Page number of results to return. 46 | * ] 47 | *48 | * 49 | * @return array 50 | */ 51 | public function lists($options = []) 52 | { 53 | return $this->method('list', $options); 54 | } 55 | 56 | /** 57 | * Alias to lists. 58 | * 59 | * @param array $options 60 | * 61 | * @return array 62 | */ 63 | public function all($options = []) 64 | { 65 | return $this->lists($options); 66 | } 67 | 68 | /** 69 | * This method allows you to create or upload an existing file. 70 | * 71 | * @param array $options available options:
72 | * "file" => "..." //File contents via multipart/form-data. br 73 | * "content" => "..." //File contents via a POST var. 74 | * "filetype" => "php" //Slack-internal file type identifier. 75 | * "filename" => "filename.txt" //Filename of file. 76 | * "title" => "My File", //Title of file 77 | * "initial_comment" => "The best!", //Initial comment to add to file. 78 | * "channels" => "channel1,channel2", //Comma separated list of channels to share the file into. 79 | *80 | * 81 | * @return array 82 | */ 83 | public function upload($options = []) 84 | { 85 | return $this->method('upload', $options); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/SlackApiServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom( 61 | __DIR__ . '/config/slack-api.php', 62 | 'slack-api' 63 | ); 64 | 65 | /* Lumen autoload services configs */ 66 | if (Str::contains($this->app->version(), 'Lumen')) { 67 | $this->app->configure('services'); 68 | } 69 | 70 | $this->app->singleton('Vluzrmos\SlackApi\Contracts\SlackApi', function () { 71 | $api = new SlackApi(null, config('services.slack.token')); 72 | 73 | return $api; 74 | }); 75 | 76 | $this->app->alias('Vluzrmos\SlackApi\Contracts\SlackApi', 'slack.api'); 77 | 78 | foreach ($this->methods as $method) { 79 | $this->registerSlackMethod($method); 80 | } 81 | 82 | $this->app->alias('Vluzrmos\SlackApi\Contracts\SlackInstantMessage', 'slack.im'); 83 | 84 | $this->app->alias('Vluzrmos\SlackApi\Contracts\SlackRealTimeMessage', 'slack.rtm'); 85 | } 86 | 87 | public function boot() 88 | { 89 | $this->publishes([ 90 | __DIR__ . '/config/slack-api.php' => config_path('slack-api.php'), 91 | ]); 92 | } 93 | 94 | /** 95 | * Get the services provided by the provider. 96 | * 97 | * @return array 98 | */ 99 | public function provides() 100 | { 101 | return ['slack.api']; 102 | } 103 | 104 | public function registerSlackMethod($name) 105 | { 106 | $contract = Str::finish($this->contractsNamespace, '\\') . "Slack{$name}"; 107 | $shortcut = $this->shortcutPrefix . Str::snake($name); 108 | $class = Str::finish($this->methodsNamespace, '\\') . $name; 109 | 110 | $this->registerSlackSingletons($contract, $class, $shortcut); 111 | } 112 | 113 | /** 114 | * @param $contract 115 | * @param $class 116 | * @param $shortcut 117 | */ 118 | public function registerSlackSingletons($contract, $class, $shortcut = null) 119 | { 120 | $this->app->singleton($contract, function () use ($class) { 121 | return new $class($this->app['slack.api'], $this->app['cache.store']); 122 | }); 123 | 124 | if ($shortcut) { 125 | $this->app->alias($contract, $shortcut); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/Methods/SlackMethod.php: -------------------------------------------------------------------------------- 1 | api = $api; 36 | $this->cache = $cache; 37 | } 38 | 39 | /** 40 | * Sends a http request. 41 | * 42 | * @param string $method short method of the api (only the suffix after ".") 43 | * @param array $params params to the given method 44 | * @param string $http http verb 45 | * 46 | * @return array 47 | */ 48 | public function method($method, $params = [], $http = 'post') 49 | { 50 | return call_user_func([$this->getApi(), $http], $this->methodsGroup . $method, $params); 51 | } 52 | 53 | /** 54 | * Returns the api. 55 | * @return \Vluzrmos\SlackApi\Contracts\SlackApi 56 | */ 57 | public function getApi() 58 | { 59 | return $this->api; 60 | } 61 | 62 | /** 63 | * Cache a value. 64 | * 65 | * @param string $key 66 | * @param mixed $value 67 | * @param int $minutes Default 1 68 | * 69 | * @return mixed 70 | */ 71 | public function cachePut($key, $value, $minutes = 1) 72 | { 73 | $this->cache->put($this->cachePrefix($key), $value, Carbon::now()->addMinutes($minutes)); 74 | 75 | return $value; 76 | } 77 | 78 | /** 79 | * Remember the result value for a given closure. 80 | * @param $key 81 | * @param $minutes 82 | * @param $callback 83 | * 84 | * @return mixed 85 | */ 86 | public function cacheRemember($key, $minutes, $callback) 87 | { 88 | return $this->cache->remember($this->cachePrefix($key), Carbon::now()->addMinutes($minutes), $callback); 89 | } 90 | 91 | /** 92 | * Remember the result value for a closure forever. 93 | * @param $key 94 | * @param $callback 95 | * 96 | * @return mixed 97 | */ 98 | public function cacheRememberForever($key, $callback) 99 | { 100 | return $this->cache->rememberForever($this->cachePrefix($key), $callback); 101 | } 102 | 103 | /** 104 | * Get a cache for a given key. 105 | * @param string $key 106 | * @param null $default 107 | * 108 | * @return mixed 109 | */ 110 | public function cacheGet($key, $default = null) 111 | { 112 | return $this->cache->get($this->cachePrefix($key), $default); 113 | } 114 | 115 | /** 116 | * Cache a value forever. 117 | * @param $key 118 | * @param $value 119 | */ 120 | public function cacheForever($key, $value) 121 | { 122 | $this->cache->forever($this->cachePrefix($key), $value); 123 | } 124 | 125 | /** 126 | * Forget a value for a given key. 127 | * @param $key 128 | */ 129 | public function cacheForget($key) 130 | { 131 | $this->cache->forget($this->cachePrefix($key)); 132 | } 133 | 134 | /** 135 | * Get the default key prefix. 136 | * 137 | * @param string|null $key 138 | * 139 | * @return string 140 | */ 141 | protected function cachePrefix($key = null) 142 | { 143 | return $this->cachePrefix . $key; 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/Methods/Chat.php: -------------------------------------------------------------------------------- 1 | method('delete', compact('channel', 'ts')); 24 | } 25 | 26 | /** 27 | * This method posts a message to a channel. 28 | * 29 | * @see https://api.slack.com/methods/chat.postMessage 30 | * 31 | * @param string $channel Channel to send message to. Can be a public channel, private group or IM channel. Can be an encoded ID, or a name. 32 | * @param string $text Text of the message to send. See below for an explanation of formatting. 33 | * @param array $options 34 | * 35 | * @example 36 | *
37 | * [ 38 | * "username" => "My Bot", //Name of bot. 39 | * "as_user" => true, //Pass true to post the message as the authed user, instead of as a bot 40 | * "parse" => "full", //Change how messages are treated. See below. 41 | * "link_names" => 1, //Find and link channel names and usernames. 42 | * "attachments" => ["pretext" => "pre-hello", "text" => "text-world"], //Structured message attachments. 43 | * "unfurl_links" => true, //Pass true to enable unfurling of primarily text-based content. 44 | * "unfurl_media" => true, //Pass false to disable unfurling of media content. 45 | * "icon_url" => "http://lorempixel.com/48/48", //URL to an image to use as the icon for this message 46 | * "icon_emoji"=> ":chart_with_upwards_trend:" //emoji to use as the icon for this message. Overrides icon_url. 47 | * ] 48 | *49 | * @return array 50 | */ 51 | public function message($channel, $text, $options = []) 52 | { 53 | return $this->method('postMessage', array_merge(compact('channel', 'text'), ['as_user' => !isset($options['username'])], $options)); 54 | } 55 | 56 | /** 57 | * Alias to message(). 58 | * @see https://api.slack.com/methods/chat.postMessage 59 | * 60 | * @param string $channel Channel to send message to. Can be a public channel, private group or IM channel. Can be an encoded ID, or a name. 61 | * @param string $text Text of the message to send. See below for an explanation of formatting. 62 | * @param array $options 63 | * 64 | * @return array 65 | */ 66 | public function postMessage($channel, $text, $options = []) 67 | { 68 | return $this->message($channel, $text, $options); 69 | } 70 | 71 | /** 72 | * This method updates a message in a channel. 73 | * 74 | * @param string $channel Channel containing the message to be updated. 75 | * @param string $text New text for the message, using the default formatting rules. 76 | * @param int|string $ts Timestamp of the message to be updated. 77 | * @param array $options 78 | * 79 | * @example 80 | *
81 | * [ 82 | * "username" => "My Bot", //Name of bot. 83 | * "as_user" => true, //Pass true to post the message as the authed user, instead of as a bot 84 | * "parse" => "full", //Change how messages are treated. See below. 85 | * "link_names" => 1, //Find and link channel names and usernames. 86 | * "attachments" => ["pretext" => "pre-hello", "text" => "text-world"], //Structured message attachments. 87 | * "unfurl_links" => true, //Pass true to enable unfurling of primarily text-based content. 88 | * "unfurl_media" => true, //Pass false to disable unfurling of media content. 89 | * "icon_url" => "http://lorempixel.com/48/48", //URL to an image to use as the icon for this message 90 | * "icon_emoji"=> ":chart_with_upwards_trend:" //emoji to use as the icon for this message. Overrides icon_url. 91 | * ] 92 | *93 | * @return array 94 | */ 95 | public function update($channel, $text, $ts, $options = []) 96 | { 97 | return $this->method('update', array_merge(compact('channel', 'text', 'ts'), $options)); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/Methods/User.php: -------------------------------------------------------------------------------- 1 | method('getPresence', compact('user')); 22 | } 23 | 24 | /** 25 | * This method returns information about a team member. 26 | * 27 | * @param string $user User to get info on 28 | * 29 | * @return array 30 | */ 31 | public function info($user) 32 | { 33 | $user = $this->getUsersIDsByNicks($user); 34 | 35 | return $this->method('info', ['user' => isset($user[0]) ? $user[0] : null]); 36 | } 37 | 38 | /** 39 | * This method returns a list of all users in the team. This includes deleted/deactivated users. 40 | * 41 | * @return array 42 | */ 43 | public function lists() 44 | { 45 | return $this->method('list'); 46 | } 47 | 48 | /** 49 | * Alias to lists. 50 | * 51 | * @return array 52 | */ 53 | public function all() 54 | { 55 | return $this->lists(); 56 | } 57 | 58 | /** 59 | * This method lets the slack messaging server know that the authenticated user is currently active. 60 | * Consult the presence documentation for more details. 61 | * 62 | * @return array 63 | */ 64 | public function setActive() 65 | { 66 | return $this->method('setActive'); 67 | } 68 | 69 | /** 70 | * This method return user info search by email 71 | * 72 | * @param $email 73 | * @return array 74 | */ 75 | public function lookupByEmail($email) 76 | { 77 | return $this->method('lookupByEmail', ['email' => $email]); 78 | } 79 | 80 | /** 81 | * This method lets you set the calling user's manual presence. 82 | * Consult the presence documentation for more details. 83 | * 84 | * @param $presence 85 | * 86 | * @return array 87 | */ 88 | public function setPresence($presence) 89 | { 90 | return $this->method('setPresence', compact('presence')); 91 | } 92 | 93 | /** 94 | * Get an array of users id's by nicks. 95 | * 96 | * @param string|array $nicks 97 | * @param bool $force force to reload the users list 98 | * 99 | * @param int $cacheMinutes Minutes or a Date to cache the results, default 1 minute 100 | * 101 | * @return array 102 | */ 103 | public function getUsersIDsByNicks($nicks, $force = false, $cacheMinutes = 1) 104 | { 105 | $users = $this->cacheGet('list'); 106 | 107 | if (!$users || $force) { 108 | $users = $this->cachePut('list', $this->lists(), $cacheMinutes); 109 | } 110 | 111 | if (!is_array($nicks)) { 112 | $nicks = preg_split('/, ?/', $nicks); 113 | } 114 | 115 | $usersIds = []; 116 | 117 | foreach ($users->members as $user) { 118 | foreach ($nicks as $nick) { 119 | if ($this->isUserNick($user, $nick)) { 120 | $usersIds[] = $user->id; 121 | } elseif ($this->isSlackbotNick($nick)) { 122 | $usersIds[] = 'USLACKBOT'; 123 | } 124 | } 125 | } 126 | 127 | return $usersIds; 128 | } 129 | 130 | /** 131 | * Verify if a given nick is for the user. 132 | * 133 | * @param array|object $user 134 | * @param string $nick 135 | * 136 | * @return bool 137 | */ 138 | protected function isUserNick($user, $nick) 139 | { 140 | $nick = str_replace('@', '', $nick); 141 | $user = (object) $user; 142 | 143 | if (!empty($user->name) && $nick === $user->name) { 144 | return true; 145 | } 146 | 147 | return !empty($user->id) && $nick === $user->id; 148 | } 149 | 150 | /** 151 | * Check if a given nick is for the slackbot. 152 | * 153 | * @param string $nick 154 | * 155 | * @return bool 156 | */ 157 | protected function isSlackbotNick($nick) 158 | { 159 | $names = ['slackbot', '@slackbot', 'USLACKBOT']; 160 | 161 | return in_array($nick, $names, true); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/SlackApi.php: -------------------------------------------------------------------------------- 1 | setClient($client); 40 | $this->setToken($token); 41 | } 42 | 43 | /** 44 | * Send a GET Request. 45 | * 46 | * @param $apiMethod 47 | * @param array $parameters 48 | * 49 | * @return \GuzzleHttp\Message\ResponseInterface 50 | */ 51 | public function get($apiMethod, $parameters = []) 52 | { 53 | $url = $this->getUrl($apiMethod); 54 | $parameters = $this->mergeParameters($parameters); 55 | 56 | return $this->http('get', $url, $parameters); 57 | } 58 | 59 | /** 60 | * Send a POST Request. 61 | * 62 | * @param $apiMethod 63 | * @param array $parameters 64 | * 65 | * @return \GuzzleHttp\Message\ResponseInterface 66 | */ 67 | public function post($apiMethod, $parameters = []) 68 | { 69 | $url = $this->getUrl($apiMethod); 70 | $parameters = $this->mergeParameters($parameters); 71 | 72 | return $this->http('post', $url, $parameters); 73 | } 74 | 75 | /** 76 | * Send a PUT Request. 77 | * 78 | * @param $apiMethod 79 | * @param array $parameters 80 | * 81 | * @return \GuzzleHttp\Message\ResponseInterface 82 | */ 83 | public function put($apiMethod, $parameters = []) 84 | { 85 | $url = $this->getUrl($apiMethod); 86 | $parameters = $this->mergeParameters($parameters); 87 | 88 | return $this->http('put', $url, $parameters); 89 | } 90 | 91 | /** 92 | * Send a DELETE Request. 93 | * 94 | * @param $apiMethod 95 | * @param array $parameters 96 | * 97 | * @return \GuzzleHttp\Message\ResponseInterface 98 | */ 99 | public function delete($apiMethod, $parameters = []) 100 | { 101 | $url = $this->getUrl($apiMethod); 102 | $parameters = $this->mergeParameters($parameters); 103 | 104 | return $this->http('delete', $url, $parameters); 105 | } 106 | 107 | /** 108 | * Send a PATCH Request. 109 | * 110 | * @param $apiMethod 111 | * @param array $parameters 112 | * 113 | * @return \GuzzleHttp\Message\ResponseInterface 114 | */ 115 | public function patch($apiMethod, $parameters = []) 116 | { 117 | $url = $this->getUrl($apiMethod); 118 | $parameters = $this->mergeParameters($parameters); 119 | 120 | return $this->http('patch', $url, $parameters); 121 | } 122 | 123 | /** 124 | * Loads an Slack Method by its contract short name. 125 | * 126 | * @param $method 127 | * 128 | * @example $slack->load('Channel')->lists() 129 | * 130 | * @return mixed 131 | */ 132 | public function load($method) 133 | { 134 | if (Str::contains($method, '.')) { 135 | return app($method); 136 | } 137 | 138 | $contract = __NAMESPACE__ . '\\Contracts\\Slack' . Str::studly($method); 139 | 140 | if (class_exists($contract)) { 141 | return app($contract); 142 | } 143 | 144 | return app('slack.' . Str::snake($method)); 145 | } 146 | 147 | /** 148 | * Alias to ::load. 149 | * 150 | * @param $method 151 | * 152 | * @return mixed 153 | */ 154 | public function __invoke($method) 155 | { 156 | return $this->load($method); 157 | } 158 | 159 | /** 160 | * Set the token of your slack team member (be sure is admin token). 161 | * 162 | * @param $token 163 | */ 164 | public function setToken($token) 165 | { 166 | $this->token = $token; 167 | } 168 | 169 | /** 170 | * Configures the Guzzle Client. 171 | * 172 | * @param \GuzzleHttp\Client|Callback|null $client 173 | */ 174 | public function setClient($client = null) 175 | { 176 | if (is_callable($client)) { 177 | $this->client = value($client); 178 | } elseif (is_null($client) && is_null($this->client)) { 179 | $this->client = new Client(['verify' => false]); 180 | } else { 181 | $this->client = $client; 182 | } 183 | 184 | if (method_exists($this->client, 'setDefaultOption')) { 185 | $this->client->setDefaultOption('verify', false); 186 | } 187 | } 188 | 189 | /** 190 | * Performs an HTTP Request. 191 | * @param string $verb HTTP Verb 192 | * @param string $url Url to the request 193 | * @param array $parameters parameters to send 194 | * 195 | * @return array 196 | */ 197 | protected function http($verb = 'get', $url = '', $parameters = []) 198 | { 199 | $client = $this->getHttpClient(); 200 | 201 | /* @var \GuzzleHttp\Psr7\Response|\GuzzleHttp\Message\Response $response */ 202 | try { 203 | $response = $client->$verb($url, $parameters); 204 | } catch (\InvalidArgumentException $e) { 205 | $parameters['body'] = $parameters['form_params']; 206 | 207 | unset($parameters['form_params']); 208 | 209 | $response = $client->$verb($url, $parameters); 210 | } 211 | 212 | /** @var $contents */ 213 | $contents = $this->responseToJson($response); 214 | 215 | return $contents; 216 | } 217 | 218 | /** 219 | * @param \GuzzleHttp\Psr7\Response|\GuzzleHttp\Message\Response $response 220 | * @return array 221 | */ 222 | protected function responseToJson($response) 223 | { 224 | return json_decode($response->getBody()->getContents(), config('slack-api.response_to_assoc_array')); 225 | } 226 | 227 | /** 228 | * Merge parameters of the request with token and timestamp string. 229 | * 230 | * @param $parameters 231 | * 232 | * @return array 233 | */ 234 | protected function mergeParameters($parameters = []) 235 | { 236 | $options = [ 237 | 'query' => [ 238 | 't' => time() 239 | ], 240 | 'headers' => [ 241 | 'Authorization' => "Bearer {$this->getToken()}" 242 | ] 243 | ]; 244 | 245 | if (isset($parameters['attachments']) && is_array($parameters['attachments'])) { 246 | $parameters['attachments'] = json_encode($parameters['attachments']); 247 | } 248 | 249 | $options['form_params'] = $parameters; 250 | 251 | return $options; 252 | } 253 | 254 | /** 255 | * Get the Guzzle Client. 256 | * 257 | * @return Client 258 | */ 259 | protected function getHttpClient() 260 | { 261 | return $this->client; 262 | } 263 | 264 | /** 265 | * Generate the url with the api $method. 266 | * 267 | * @param string|null $method 268 | * 269 | * @return string 270 | */ 271 | protected function getUrl($method = null) 272 | { 273 | return Str::finish($this->url, '/') . $method; 274 | } 275 | 276 | /** 277 | * Get the user token. 278 | * 279 | * @return null|string 280 | */ 281 | protected function getToken() 282 | { 283 | return $this->token; 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /src/Vluzrmos/SlackApi/Methods/Channel.php: -------------------------------------------------------------------------------- 1 | method('archive', ['channel' => $channel]); 21 | } 22 | 23 | /** 24 | * This method crate a channel with a given name. 25 | * 26 | * @param string $name Name of channel to create 27 | * 28 | * @return array 29 | */ 30 | public function create($name) 31 | { 32 | return $this->method('create', ['name' => $name]); 33 | } 34 | 35 | /** 36 | * This method returns a portion of messages/events from the specified channel. 37 | * To read the entire history for a channel, call the method with no `latest` or `oldest` arguments, 38 | * and then continue paging using the instructions below. 39 | * @see https://api.slack.com/methods/conversations.history 40 | * 41 | * @param string $channel Channel to fetch history for. 42 | * @param int $limit Number of messages to return, between 1 and 1000. 43 | * @param string $latest End of time range of messages to include in results. 44 | * @param int $oldest Start of time range of messages to include in results. 45 | * @param int $inclusive Include messages with latest or oldest timestamp in results. 46 | * 47 | * @return array 48 | */ 49 | public function history($channel, $limit = 100, $latest = null, $oldest = 0, $inclusive = 1) 50 | { 51 | return $this->method('history', compact('channel', 'limit', 'latest', 'oldest', 'inclusive')); 52 | } 53 | 54 | /** 55 | * This method returns information about a team channel. 56 | * 57 | * @see https://api.slack.com/methods/conversations.info 58 | * 59 | * @param string $channel Channel to get info on 60 | * 61 | * @return array 62 | */ 63 | public function info($channel) 64 | { 65 | return $this->method('info', ['channel' => $channel]); 66 | } 67 | 68 | /** 69 | * This method is used to invite a user to a channel. The calling user must be a member of the channel. 70 | * 71 | * @see https://api.slack.com/methods/conversations.invite 72 | * 73 | * @param string $channel 74 | * @param string $user 75 | * 76 | * @return array 77 | */ 78 | public function invite($channel, $user) 79 | { 80 | return $this->method('invite', compact('channel', 'user')); 81 | } 82 | 83 | /** 84 | * This method is used to join a channel. If the channel does not exist, it is created. 85 | * 86 | * @see https://api.slack.com/methods/conversations.join 87 | * 88 | * @param string $name Channel name to join in 89 | * 90 | * @return array 91 | */ 92 | public function join($name) 93 | { 94 | return $this->method('join', ['channel' => $name]); 95 | } 96 | 97 | /** 98 | * This method allows a user to remove another member from a team channel. 99 | * 100 | * @see https://api.slack.com/methods/conversations.kick 101 | * 102 | * @param string $channel 103 | * @param string $user 104 | * 105 | * @return array 106 | */ 107 | public function kick($channel, $user) 108 | { 109 | return $this->method('kick', compact('channel', 'user')); 110 | } 111 | 112 | /** 113 | * This method is used to leave a channel. 114 | * 115 | * @see https://api.slack.com/methods/conversations.leave 116 | * 117 | * @param string $channel 118 | * 119 | * @return array 120 | */ 121 | public function leave($channel) 122 | { 123 | return $this->method('leave', ['channel' => $channel]); 124 | } 125 | 126 | /** 127 | * This method returns a list of all channels in the team. This includes channels the caller is in, channels they are not currently in, and archived conversations. 128 | * The number of (non-deactivated) members in each channel is also returned. 129 | * 130 | * @see https://api.slack.com/methods/conversations.list 131 | * 132 | * @param int $exclude_archived Don't return archived conversations. 133 | * @param array $options ['limit' = 100, 'cursor' => "dXNlcjpVMDYxTkZUVDI=", types => "public_channel", "team_id"=>,T1234567890 ] 134 | * @return array 135 | */ 136 | public function all($exclude_archived = 1, $options = []) 137 | { 138 | return $this->method('list', array_merge(compact('exclude_archived'), $options)); 139 | } 140 | 141 | /** 142 | * This method returns a list of all channels in the team. This includes channels the caller is in, channels they are not currently in, and archived conversations. 143 | * The number of (non-deactivated) members in each channel is also returned. 144 | * 145 | * @see https://api.slack.com/methods/conversations.list 146 | * 147 | * @param int $exclude_archived Don't return archived conversations. 148 | * @param array $options ['limit' = 100, 'cursor' => "dXNlcjpVMDYxTkZUVDI=", types => "public_channel", "team_id"=>,T1234567890 ] 149 | * 150 | * @return array 151 | */ 152 | public function lists($exclude_archived = 1, $options = []) 153 | { 154 | return $this->all($exclude_archived, $options); 155 | } 156 | 157 | /** 158 | * This method moves the read cursor in a channel. 159 | * 160 | * @see https://api.slack.com/methods/conversations.mark 161 | * 162 | * @param string $channel Channel to set reading cursor in. 163 | * @param string|int $ts Timestamp of the most recently seen message. 164 | * 165 | * @return array 166 | */ 167 | public function mark($channel, $ts) 168 | { 169 | return $this->method('mark', compact($channel, $ts)); 170 | } 171 | 172 | /** 173 | * This method renames a team channel. 174 | * 175 | * The only people who can rename a channel are team admins, or the person that originally 176 | * created the channel. Others will recieve a "not_authorized" error. 177 | * 178 | * @see https://api.slack.com/methods/conversations.rename 179 | * 180 | * @param string $channel Channel to rename 181 | * 182 | * @param string $name New name for channel 183 | * 184 | * @return array 185 | */ 186 | public function rename($channel, $name) 187 | { 188 | return $this->method('rename', compact($channel, $name)); 189 | } 190 | 191 | /** 192 | * This method is used to change the purpose of a channel. The calling user must be a member of the channel. 193 | * 194 | * @see https://api.slack.com/methods/conversations.setPurpose 195 | * 196 | * @param string $channel Channel to set the purpose of 197 | * @param string $purpose The new purpose 198 | * 199 | * @return array 200 | */ 201 | public function setPurpose($channel, $purpose) 202 | { 203 | return $this->method('setPurpose', compact($channel, $purpose)); 204 | } 205 | 206 | /** 207 | * This method is used to change the topic of a channel. The calling user must be a member of the channel. 208 | * 209 | * @see https://api.slack.com/methods/conversations.setTopic 210 | * 211 | * @param string $channel 212 | * @param string $topic 213 | * 214 | * @return array 215 | */ 216 | public function setTopic($channel, $topic) 217 | { 218 | return $this->method('setPurpose', compact($channel, $topic)); 219 | } 220 | 221 | /** 222 | * This method unarchives a channel. The calling user is added to the channel. 223 | * 224 | * @see https://api.slack.com/methods/conversations.unarchive 225 | * 226 | * @param string $channel Channel to unarchive 227 | * 228 | * @return array 229 | */ 230 | public function unarchive($channel) 231 | { 232 | return $this->method('unarchive', compact($channel)); 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Laravel e Lumen - Slack API 2 | 3 | [](https://gitter.im/vluzrmos/laravel-slack-api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | This package provides a simple way to use [Slack API](https://api.slack.com/web#methods). 6 | 7 | [](https://packagist.org/packages/vluzrmos/slack-api) [](https://packagist.org/packages/vluzrmos/slack-api) [](https://packagist.org/packages/vluzrmos/slack-api) [](https://packagist.org/packages/vluzrmos/slack-api) 8 | 9 | ## Instalation 10 | 11 | `composer require vluzrmos/slack-api` 12 | 13 | ## Instalation on Laravel 14 | Add to `config/app.php`: 15 | 16 | This package uses auto-discovery laravel's feature, the service provider and all the facades will be automatic discovered. 17 | 18 | Service Provider: `\Vluzrmos\SlackApi\SlackApiServiceProvider::class` 19 | 20 | Facades: 21 | 22 | ```php 23 | [ 24 | 'SlackApi' => Vluzrmos\SlackApi\Facades\SlackApi::class, 25 | 'SlackChannel' => Vluzrmos\SlackApi\Facades\SlackChannel::class, 26 | 'SlackChat' => Vluzrmos\SlackApi\Facades\SlackChat::class, 27 | 'SlackGroup' => Vluzrmos\SlackApi\Facades\SlackGroup::class, 28 | 'SlackFile' => Vluzrmos\SlackApi\Facades\SlackFile::class, 29 | 'SlackSearch' => Vluzrmos\SlackApi\Facades\SlackSearch::class, 30 | 'SlackInstantMessage' => Vluzrmos\SlackApi\Facades\SlackInstantMessage::class, 31 | 'SlackUser' => Vluzrmos\SlackApi\Facades\SlackUser::class, 32 | 'SlackStar' => Vluzrmos\SlackApi\Facades\SlackStar::class, 33 | 'SlackUserAdmin' => Vluzrmos\SlackApi\Facades\SlackUserAdmin::class, 34 | 'SlackRealTimeMessage' => Vluzrmos\SlackApi\Facades\SlackRealTimeMessage::class, 35 | 'SlackTeam' => Vluzrmos\SlackApi\Facades\SlackTeam::class, 36 | 'SlackOAuth' => Vluzrmos\SlackApi\Facades\SlackOAuth::class, 37 | 'SlackOAuthV2' => Vluzrmos\SlackApi\Facades\SlackOAuthV2::class, 38 | ] 39 | ``` 40 | 41 | ## Instalation on Lumen 42 | 43 | Add that line on `bootstrap/app.php`: 44 | 45 | ```php 46 | register('App\Providers\AppServiceProvider'); (by default that comes commented) 48 | $app->register('Vluzrmos\SlackApi\SlackApiServiceProvider'); 49 | 50 | ?> 51 | ``` 52 | 53 | If you want to use facades, add this lines on
bootstrap/app.php
54 |
55 | ```php
56 |
67 | ```
68 |
69 | Otherwise, just use the singleton shortcuts:
70 |
71 | ```php
72 |
95 | ```
96 |
97 | ## Slack OAuth Token
98 |
99 | To get your slack token, you must create an app on [Slack Apps](https://api.slack.com/apps) and then give the permissions that you need at your app page on side menu "Features" -> "OAuth & Permissions", and then go to "Scopes" section, the token can be a `Bot Token` or `User Token` as you need.
100 |
101 | Then re/install the app to your workspace.
102 |
103 | > Note: If you edit any permission you must reinstall the app to your workspace.
104 |
105 | ## Configuration
106 |
107 | Configure your slack team token in config/services.php
108 |
109 | ```php
110 | [
115 | 'token' => 'your token here'
116 | ]
117 | ]
118 |
119 | ?>
120 | ```
121 |
122 | By default all api methods will return objects, to change it to associative array first publish slack-api config, and then set `response_to_assoc_array` to true
123 |
124 | ```bash
125 | php artisan vendor:publish --provider="Vluzrmos\SlackApi\SlackApiServiceProvider"
126 | ```
127 |
128 | ## Usage
129 |
130 | ```php
131 | 'John',
145 | 'last_name' => 'Doe'
146 | ]);
147 |
148 | //Send a message to someone or channel or group
149 | SlackChat::message('#general', 'Hello my friends!');
150 |
151 | //Upload a file/snippet
152 | SlackFile::upload([
153 | 'filename' => 'sometext.txt',
154 | 'title' => 'text',
155 | 'content' => 'Nice contents',
156 | 'channels' => 'C0440SZU6' //can be channel, users, or groups ID
157 | ]);
158 |
159 | // Search for files or messages
160 | SlackSearch::all('my message');
161 |
162 | // Search for files
163 | SlackSearch::files('my file');
164 |
165 | // Search for messages
166 | SlackSearch::messages('my message');
167 |
168 | // or just use the helper
169 |
170 | //Autoload the api
171 | slack()->post('chat.postMessage', [...]);
172 |
173 | //Autoload a Slack Method
174 | slack('Chat')->message([...]);
175 | slack('Team')->info();
176 |
177 | ?>
178 | ```
179 |
180 | ## Using Dependency Injection
181 |
182 | ```php
183 | slackUser = $slackUser;
195 | }
196 |
197 | public function controllerMethod(){
198 | $usersList = $this->slackUser->lists();
199 | }
200 | }
201 |
202 | ?>
203 | ```
204 |
205 | ## All Injectable Contracts:
206 |
207 | ### Generic API
208 | `Vluzrmos\SlackApi\Contracts\SlackApi`
209 |
210 | Allows you to do generic requests to the api with the following http verbs:
211 | `get`, `post`, `put`, `patch`, `delete` ... all allowed api methods you could see here: [Slack Web API Methods](https://api.slack.com/methods).
212 |
213 | And is also possible load a SlackMethod contract:
214 |
215 | ```php
216 | load('Channel');
220 | $channel->lists();
221 |
222 | /** @var SlackChat $chat **/
223 | $chat = $slack->load('Chat');
224 | $chat->message('D98979F78', 'Hello my friend!');
225 |
226 | /** @var SlackUserAdmin $chat **/
227 | $admin = $slack('UserAdmin'); //Minimal syntax (invokable)
228 | $admin->invite('jhon.doe@example.com');
229 |
230 | ?>
231 | ```
232 |
233 | ### Channels API
234 | `Vluzrmos\SlackApi\Contracts\SlackChannel`
235 |
236 | Allows you to operate channels:
237 | `invite`, `archive`, `rename`, `join`, `kick`, `setPurpose` ...
238 |
239 |
240 | ### Chat API
241 | `Vluzrmos\SlackApi\Contracts\SlackChat`
242 |
243 | Allows you to send, update and delete messages with methods:
244 | `delete`, `message`, `update`.
245 |
246 | ### Files API
247 | `Vluzrmos\SlackApi\Contracts\SlackFile`
248 |
249 | Allows you to send, get info, delete, or just list files:
250 | `info`, `lists`, `upload`, `delete`.
251 |
252 | ### Groups API
253 | `Vluzrmos\SlackApi\Contracts\SlackGroup`
254 |
255 | Same methods of the SlackChannel, but that operates with groups and have adicional methods:
256 | `open`, `close`, `createChild`
257 |
258 | ### Instant Messages API (Direct Messages)
259 | `Vluzrmos\SlackApi\Contracts\SlackInstantMessage`
260 |
261 | Allows you to manage direct messages to your team members.
262 |
263 | ### Real Time Messages API
264 | `Vluzrmos\SlackApi\Contracts\SlackRealTimeMessage`
265 |
266 | Allows you list all channels and user presence at the moment.
267 |
268 |
269 | ### Search API
270 | `Vluzrmos\SlackApi\Contracts\SlackSearch`
271 |
272 | Find messages or files.
273 |
274 | ### Stars API
275 | `Vluzrmos\SlackApi\Contracts\SlackStar`
276 |
277 | List all of starred itens.
278 |
279 | ### Team API
280 | `Vluzrmos\SlackApi\Contracts\SlackTeam`
281 |
282 | Get information about your team.
283 |
284 | ### Users API
285 | `Vluzrmos\SlackApi\Contracts\SlackUser`
286 |
287 | Get information about an user on your team or just check your presence ou status.
288 |
289 | ### Users Admin API
290 | `Vluzrmos\SlackApi\Contracts\SlackUserAdmin`
291 |
292 | Invite new members to your team.
293 |
294 | ### OAuth API
295 | `Vluzrmos\SlackApi\Contracts\SlackOAuth`
296 |
297 | Methods in oauth slack api namespace.
298 |
299 | ### OAuthV2 API
300 | `Vluzrmos\SlackApi\Contracts\SlackOAuthV2`
301 |
302 | Methods in oauth v2 slack api namespace.
303 |
304 |
305 | ## License
306 |
307 | [DBAD License](https://dbad-license.org).
308 |
--------------------------------------------------------------------------------