This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-slack-api.php
289 lines (227 loc) · 5.49 KB
/
wp-slack-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* WP Slack API
*
* @link https://api.slack.com/ API Docs
* @package WP-API-Libraries\WP-Slack-API
*/
/*
* Plugin Name: WP Slack API
* Plugin URI: https://github.com/wp-api-libraries/wp-slack-api
* Description: Perform API requests to Slack in WordPress.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-slack-api
* GitHub Branch: master
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) { exit; }
/* Check if class exists. */
if ( ! class_exists( 'SlackAPI' ) ) {
/**
* SlackAPI class.
*/
class SlackAPI {
/**
* Base Slack API URL.
*
* @since 1.0
* @var string
* @access protected
*/
protected $api_url = 'https://slack.com/api/';
/**
* Initialize Slack API library.
*
* @since 1.0
* @access public
* @param string $auth_token Authentication token.
*/
public function __construct( $auth_token ) {
$this->auth_token = $auth_token;
}
/**
* Make API request.
*
* @since 1.0
* @access public
* @param string $path Request path.
* @param array $options Request option.
* @param string $method (default: 'GET') Request method.
*
* @return array
*/
public function make_request( $path, $options = array(), $method = 'GET' ) {
// Get API URL.
$api_url = $this->api_url;
// If team name is defined in request options, add team name to API URL.
if ( in_array( 'team', $options ) ) {
$api_url = str_replace( 'slack.com', $options['team'] . '.slack.com', $api_url );
unset( $options['team'] );
}
// Build base request options string.
$request_options = '?token='. $this->auth_token;
// Add options if this is a GET request.
$request_options .= ( 'GET' === $method && ! empty( $options ) ) ? '&'. http_build_query( $options ) : null;
// Build request URL.
$request_url = $api_url . $path . $request_options;
// Build request arguments.
$args = array(
'body' => 'GET' !== $method ? $options : null,
'method' => $method,
);
// Execute request.
$response = wp_remote_request( $request_url, $args );
// If WP_Error, die. Otherwise, return decoded JSON.
if ( is_wp_error( $response ) ) {
die( 'Request failed. ' . $response->get_error_message() );
} else {
return json_decode( $response['body'], true );
}
}
/**
* Test authentication token.
*
* @since 1.0
* @access public
*
* @return bool
*/
public function auth_test() {
return $this->make_request( 'auth.test' );
}
/**
* Get a channel.
*
* @since 1.0
* @access public
* @param string $channel Channel name.
*
* @return array
*/
public function get_channel( $channel ) {
return $this->make_request( 'channels.info', array( 'channel' => $channel ) );
}
/**
* Get all channels.
*
* @since 1.0
* @access public
*
* @return array
*/
public function get_channels() {
return $this->make_request( 'channels.list' );
}
/**
* Get a group.
*
* @since 1.0
* @access public
* @param string $group Group ID.
*
* @return array
*/
public function get_group( $group ) {
return $this->make_request( 'groups.info', array( 'channel' => $group ) );
}
/**
* Get all groups.
*
* @since 1.0
* @access public
*
* @return array
*/
public function get_groups() {
return $this->make_request( 'groups.list' );
}
/**
* Get current team info.
*
* @since 1.4.1
* @access public
*
* @return array
*/
public function get_team_info() {
return $this->make_request( 'team.info' );
}
/**
* Get a user.
*
* @since 1.0
* @access public
* @param string $user User ID.
*
* @return array
*/
public function get_user( $user ) {
return $this->make_request( 'users.info', array( 'user' => $user ) );
}
/**
* Get all users.
*
* @since 1.0
* @access public
*
* @return array
*/
public function get_users() {
return $this->make_request( 'users.list' );
}
/**
* Invite user to team.
*
* @since 1.4
* @access public
* @param string $team Team name.
* @param string $email Email address.
*
* @return array
*/
public function invite_user( $team, $email ) {
return $this->make_request( 'users.admin.invite', array( 'team' => $team, 'email' => $email, 'set_active' => true ), 'POST' );
}
/**
* Open IM channel.
*
* @since 1.0
* @access public
* @param string $user User ID.
*
* @return array
*/
public function open_im( $user ) {
return $this->make_request( 'users.info', array( 'user' => $user ), 'POST' );
}
/**
* Post message to channel.
*
* @since 1.0
* @access public
* @param array $message Message details.
*
* @return array
*/
public function post_message( $message ) {
return $this->make_request( 'chat.postMessage', $message, 'POST' );
}
/* PRESENCE & STATUS. */
public function set_custom_profile_status( $status_text, $status_emoji ) {
// POST slack.com/api/users.profile.set token=super_secret_token&profile=%7B%22status_text%22%3A%22riding%20a%20train%22%2C%22status_emoji%22%3A%22%3Amountain_railway%3A%22%7D
}
public function list_users(){
return $this->make_request( 'users.list' );
}
public function get_user_profile( $user ){
return $this->make_request( 'users.profile.get', array( 'user' => $user ) );
}
public function set_user_profile( $user, $profile ){
$profile = json_encode( $profile );
$profile = urlencode( $profile );
return $this->make_request( 'users.profile.set', array( 'user' => $user, 'profile' => $profile ), 'POST' );
}
}
}