1 <?php
2
3 namespace wataridori\ChatworkSDK;
4
5 class ChatworkApi
6 {
7 /**
8 * @var string apiKey
9 */
10 public static $apiKey = '';
11
12 /**
13 * Constructor.
14 */
15 public function __construct()
16 {
17 self::$apiKey = ChatworkSDK::getApiKey();
18 }
19
20 /**
21 * Get user own information.
22 *
23 * @return array
24 *
25 * @see http://developer.chatwork.com/ja/endpoint_me.html#GET-me
26 */
27 public function me()
28 {
29 return $this->api('me');
30 }
31
32 /**
33 * Get user own statics information.
34 *
35 * @return array
36 *
37 * @see http://developer.chatwork.com/ja/endpoint_my.html#GET-my-status
38 */
39 public function getMyStatus()
40 {
41 return $this->api('my/status');
42 }
43
44 /**
45 * Get user own task information.
46 *
47 * @param array $params
48 *
49 * @return array
50 *
51 * @see http://developer.chatwork.com/ja/endpoint_my.html#GET-my-tasks
52 */
53 public function getMyTasks($params = [])
54 {
55 return $this->api('my/tasks', ChatworkRequest::REQUEST_METHOD_GET, $params);
56 }
57
58 /**
59 * Get user contacts list.
60 *
61 * @return array
62 *
63 * @see http://developer.chatwork.com/ja/endpoint_contacts.html#GET-contacts
64 */
65 public function getContacts()
66 {
67 return $this->api('contacts');
68 }
69
70 /**
71 * Get user rooms list.
72 *
73 * @return array.
74 *
75 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms
76 */
77 public function getRooms()
78 {
79 return $this->api('rooms');
80 }
81
82 /**
83 * Create new room.
84 *
85 * @param string $name
86 * @param array $members_admin_ids
87 * @param array $params
88 *
89 * @return array
90 *
91 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms
92 */
93 public function createRoom($name, $members_admin_ids = [], $params = [])
94 {
95 $params['members_admin_ids'] = implode(',', $members_admin_ids);
96 $params['name'] = implode(',', $name);
97
98 $params['members_admin_ids'] = implode(',', $params['members_admin_ids']);
99 if (isset($params['members_members_id'])) {
100 $params['members_members_id'] = implode(',', $params['members_members_id']);
101 }
102 if (isset($params['members_readonly_ids'])) {
103 $params['members_readonly_ids'] = implode(',', $params['members_readonly_ids']);
104 }
105
106 return $this->api(
107 'rooms',
108 ChatworkRequest::REQUEST_METHOD_POST,
109 $params
110 );
111 }
112
113 /**
114 * Get room information.
115 *
116 * @param int $room_id
117 *
118 * @return array
119 *
120 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id
121 */
122 public function getRoomById($room_id)
123 {
124 return $this->api(
125 sprintf('rooms/%d', $room_id)
126 );
127 }
128
129 /**
130 * Update room information.
131 *
132 * @param int $room_id
133 * @param array $params
134 *
135 * @return mixed|void
136 *
137 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id
138 */
139 public function updateRoomInfo($room_id, $params = [])
140 {
141 return $this->api(
142 sprintf('rooms/%d', $room_id),
143 ChatworkRequest::REQUEST_METHOD_PUT,
144 $params
145 );
146 }
147
148 /**
149 * Delete room.
150 *
151 * @param int $room_id
152 *
153 * @return array
154 *
155 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id
156 */
157 public function deleteRoom($room_id)
158 {
159 return $this->api(
160 sprintf('rooms/%d', $room_id),
161 ChatworkRequest::REQUEST_METHOD_DELETE,
162 ['action_type' => 'delete']
163 );
164 }
165
166 /**
167 * Leave room.
168 *
169 * @param int $room_id
170 *
171 * @return array
172 *
173 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#DELETE-rooms-room_id
174 */
175 public function leaveRoom($room_id)
176 {
177 return $this->api(
178 sprintf('rooms/%d', $room_id),
179 ChatworkRequest::REQUEST_METHOD_DELETE,
180 ['action_type' => 'leave']
181 );
182 }
183
184 /**
185 * Get all members of a room.
186 *
187 * @param int $room_id
188 *
189 * @return array
190 *
191 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-members
192 */
193 public function getRoomMembersById($room_id)
194 {
195 return $this->api(
196 sprintf('rooms/%d/members', $room_id)
197 );
198 }
199
200 /**
201 * Update current room members.
202 *
203 * @param int $room_id
204 * @param array $members_admin_ids
205 * @param array $params
206 *
207 * @return mixed|void
208 *
209 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#PUT-rooms-room_id-members
210 */
211 public function updateRoomMembers($room_id, $members_admin_ids = [], $params = [])
212 {
213 $params = array_merge([
214 'members_admin_ids' => $members_admin_ids,
215 ], $params);
216
217 if (isset($params['members_member_ids'])) {
218 $params['members_admin_ids'] = implode(',', $params['members_admin_ids']);
219 }
220 if (isset($params['members_member_ids'])) {
221 $params['members_member_ids'] = implode(',', $params['members_member_ids']);
222 }
223 if (isset($params['members_readonly_ids'])) {
224 $params['members_readonly_ids'] = implode(',', $params['members_readonly_ids']);
225 }
226
227 return $this->api(
228 sprintf('rooms/%d', $room_id),
229 ChatworkRequest::REQUEST_METHOD_PUT,
230 $params
231 );
232 }
233
234 /**
235 * Get messages of a room.
236 *
237 * @param int $room_id
238 * @param bool $force
239 *
240 * @return array
241 *
242 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages
243 */
244 public function getRoomMessages($room_id, $force = false)
245 {
246 return $this->api(
247 sprintf('rooms/%d/messages', $room_id),
248 ChatworkRequest::REQUEST_METHOD_GET,
249 ['force' => $force ? 1 : 0]
250 );
251 }
252
253 /**
254 * Create a message.
255 *
256 * @param int $room_id
257 * @param string $body
258 *
259 * @return array
260 *
261 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages
262 */
263 public function createRoomMessage($room_id, $body)
264 {
265 return $this->api(
266 sprintf('rooms/%d/messages', $room_id),
267 ChatworkRequest::REQUEST_METHOD_POST,
268 ['body' => $body]
269 );
270 }
271
272 /**
273 * Get a message.
274 *
275 * @param int $room_id
276 * @param string $message_id
277 *
278 * @return array
279 *
280 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-messages-message_id
281 */
282 public function getRoomMessageByMessageId($room_id, $message_id)
283 {
284 return $this->api(
285 sprintf('rooms/%d/messages/%s', $room_id, $message_id)
286 );
287 }
288
289 /**
290 * Get tasks of a room.
291 *
292 * @param int $room_id
293 * @param array $params
294 *
295 * @return mixed|void
296 *
297 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks
298 */
299 public function getRoomTasks($room_id, $params = [])
300 {
301 return $this->api(
302 sprintf('rooms/%d/tasks', $room_id),
303 ChatworkRequest::REQUEST_METHOD_GET,
304 $params
305 );
306 }
307
308 /**
309 * Get a task of a room.
310 *
311 * @param int $room_id
312 * @param int $task_id
313 *
314 * @return mixed|void
315 *
316 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-tasks-task_id
317 */
318 public function getRoomTaskById($room_id, $task_id)
319 {
320 return $this->api(
321 sprintf('rooms/%d/tasks/%d', $room_id, $task_id)
322 );
323 }
324
325 /**
326 * Add new task.
327 *
328 * @param int $room_id
329 * @param array $to_ids
330 * @param string $body
331 * @param null|string $limit
332 *
333 * @return mixed|void
334 *
335 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#POST-rooms-room_id-tasks
336 */
337 public function addTask($room_id, $to_ids, $body, $limit = null)
338 {
339 $params = [
340 'to_ids' => implode(',', $to_ids),
341 'body' => $body,
342 'limit' => $limit,
343 ];
344
345 return $this->api(
346 sprintf('rooms/%d/tasks', $room_id),
347 ChatworkRequest::REQUEST_METHOD_POST,
348 $params
349 );
350 }
351
352 /**
353 * Get files of a room.
354 *
355 * @param int $room_id
356 * @param array $params
357 *
358 * @return array
359 *
360 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files
361 */
362 public function getRoomFiles($room_id, $params = [])
363 {
364 return $this->api(
365 sprintf('rooms/%d/files', $room_id),
366 ChatworkRequest::REQUEST_METHOD_GET,
367 $params
368 );
369 }
370
371 /**
372 * Get file of a room.
373 *
374 * @param int $room_id
375 * @param int $file_id
376 * @param bool $create_download_url
377 *
378 * @return mixed|void
379 *
380 * @see http://developer.chatwork.com/ja/endpoint_rooms.html#GET-rooms-room_id-files-file_id
381 */
382 public function getRoomFileById($room_id, $file_id, $create_download_url = false)
383 {
384 return $this->api(
385 sprintf('rooms/%d/files/%d', $room_id, $file_id),
386 ChatworkRequest::REQUEST_METHOD_GET,
387 ['create_download_url' => $create_download_url ? 1 : 0]
388 );
389 }
390
391 /**
392 * Get Friend Request list.
393 *
394 * @return array
395 *
396 * @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#GET-incoming_requests
397 */
398 public function getIncomingRequests()
399 {
400 return $this->api('incoming_requests');
401 }
402
403 /**
404 * Accept Friend Request.
405 *
406 * @param int request_id
407 *
408 * @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#PUT-incoming_requests-request_id
409 */
410 public function acceptFriendRequest($request_id)
411 {
412 return $this->api(sprintf('incoming_requests/%d', $request_id), ChatworkRequest::REQUEST_METHOD_PUT);
413 }
414
415 /**
416 * Delete Friend Request.
417 *
418 * @param int request_id
419 *
420 * @see http://developer.chatwork.com/ja/endpoint_incoming_requests.html#DELETE-incoming_requests-request_id
421 */
422 public function deleteFriendRequest($request_id)
423 {
424 return $this->api(sprintf('incoming_requests/%d', $request_id), ChatworkRequest::REQUEST_METHOD_DELETE);
425 }
426
427 /**
428 * Call Chatwork API.
429 *
430 * @param string $endPoint
431 * @param string $method
432 * @param array $params
433 *
434 * @return array
435 */
436 protected function api($endPoint, $method = ChatworkRequest::REQUEST_METHOD_GET, $params = [])
437 {
438 $request = new ChatworkRequest(self::$apiKey);
439 $request->setEndPoint($endPoint);
440 $request->setMethod($method);
441 $request->setParams($params);
442
443 $response = $request->send();
444
445 return $response['response'];
446 }
447 }
448