Overview
  • Namespace
  • Class

Namespaces

  • wataridori
    • ChatworkSDK
      • Exception

Classes

  • wataridori\ChatworkSDK\ChatworkApi
  • wataridori\ChatworkSDK\ChatworkBase
  • wataridori\ChatworkSDK\ChatworkMessage
  • wataridori\ChatworkSDK\ChatworkRequest
  • wataridori\ChatworkSDK\ChatworkRoom
  • wataridori\ChatworkSDK\ChatworkSDK
  • wataridori\ChatworkSDK\ChatworkUser

Exceptions

  • wataridori\ChatworkSDK\Exception\ChatworkSDKException
  • wataridori\ChatworkSDK\Exception\NoChatworkApiKeyException
  • wataridori\ChatworkSDK\Exception\NoChatworkRoomException
  • wataridori\ChatworkSDK\Exception\RequestFailException
  1 <?php
  2 
  3 namespace wataridori\ChatworkSDK;
  4 
  5 use wataridori\ChatworkSDK\Exception\ChatworkSDKException;
  6 
  7 class ChatworkRoom extends ChatworkBase
  8 {
  9     public $room_id = '';
 10     public $name = '';
 11     public $type = '';
 12     public $role = '';
 13     public $sticky = '';
 14     public $unread_num = '';
 15     public $mention_num = '';
 16     public $mytask_num = '';
 17     public $message_num = '';
 18     public $file_num = '';
 19     public $task_num = '';
 20     public $icon_path = '';
 21     public $description = '';
 22 
 23     protected $listMembers = [];
 24 
 25     /**
 26      * Constructor.
 27      *
 28      * @param int|array $room
 29      */
 30     public function __construct($room)
 31     {
 32         $this->init($room);
 33 
 34         parent::__construct($this->room_id);
 35         $this->chatworkApi = new ChatworkApi();
 36     }
 37 
 38     /**
 39      * @param int|array $room
 40      */
 41     public function init($room)
 42     {
 43         if (is_array($room)) {
 44             foreach ($room as $key => $value) {
 45                 if (property_exists($this, $key)) {
 46                     $this->$key = $value;
 47                 }
 48             }
 49         } elseif (is_numeric($room)) {
 50             $this->room_id = $room;
 51         }
 52     }
 53 
 54     /**
 55      * @return array Room Information
 56      */
 57     public function toArray()
 58     {
 59         return [
 60             'room_id' => $this->room_id,
 61             'name' => $this->name,
 62             'type' => $this->type,
 63             'role' => $this->role,
 64             'sticky' => $this->sticky,
 65             'unread_num' => $this->unread_num,
 66             'mention_num' => $this->mention_num,
 67             'mytask_num' => $this->mytask_num,
 68             'message_num' => $this->message_num,
 69             'file_num' => $this->file_num,
 70             'task_num' => $this->task_num,
 71             'icon_path' => $this->icon_path,
 72             'description' => $this->description,
 73         ];
 74     }
 75 
 76     /**
 77      * Get Room Information.
 78      *
 79      * @return array
 80      */
 81     public function get()
 82     {
 83         $room = $this->chatworkApi->getRoomById($this->room_id);
 84         $this->init($room);
 85         return $room;
 86     }
 87 
 88     /**
 89      * Update Room Information.
 90      *
 91      * @param array $params
 92      *
 93      * @return mixed|void
 94      */
 95     public function updateInfo($params = [])
 96     {
 97         return $this->chatworkApi->updateRoomInfo($this->room_id, $params);
 98     }
 99 
100     /**
101      * Get Members list of room.
102      *
103      * @return array
104      */
105     public function getMembers()
106     {
107         $members = [];
108         $results = $this->chatworkApi->getRoomMembersById($this->room_id);
109         foreach ($results as $result) {
110             $members[] = new ChatworkUser($result);
111         }
112 
113         $this->listMembers = $members;
114 
115         return $members;
116     }
117 
118     /**
119      * Update members list of room.
120      *
121      * @param array $members_admin_ids
122      * @param array $params
123      *
124      * @return mixed|void
125      */
126     public function updateMembers($members_admin_ids = [], $params = [])
127     {
128         return $this->chatworkApi->updateRoomMembers($this->room_id, $members_admin_ids, $params);
129     }
130 
131     /**
132      * Get Messages of Room.
133      *
134      * @param bool $force
135      *
136      * @return array
137      */
138     public function getMessages($force = false)
139     {
140         $messages = [];
141         $results = $this->chatworkApi->getRoomMessages($this->room_id, $force);
142         if ($results) {
143             foreach ($results as $result) {
144                 $messages[] = new ChatworkMessage($result);
145             }
146         }
147 
148         return $messages;
149     }
150 
151     /**
152      * Send Message.
153      *
154      * @param null $newMessage
155      */
156     public function sendMessage($newMessage = null)
157     {
158         $message = $newMessage ? $newMessage : $this->message;
159         $this->chatworkApi->createRoomMessage($this->room_id, $message);
160     }
161 
162     /**
163      * Send Message to list of members.
164      *
165      * @param ChatworkUser[] $members
166      * @param string $sendMessage
167      * @param bool $withName
168      * @param bool $newLine
169      * @param bool $usePicon
170      *
171      * @throws ChatworkSDKException
172      */
173     public function sendMessageToList($members, $sendMessage, $withName = true, $newLine = true, $usePicon = false)
174     {
175         $this->resetMessage();
176         foreach ($members as $member) {
177             if (!($member instanceof wataridori\ChatworkSDK\ChatworkUser)) {
178                 $this->appendTo($member, $withName, $newLine, $usePicon);
179             } else {
180                 throw new ChatworkSDKException('Invalid Members list');
181             }
182         }
183         $this->appendMessage($sendMessage);
184         $this->sendMessage();
185     }
186 
187     /**
188      * Send Message To All Members in Room.
189      *
190      * @param null $sendMessage
191      * @param bool $withName
192      * @param bool $newLine
193      * @param bool $usePicon
194      */
195     public function sendMessageToAll($sendMessage, $withName = true, $newLine = true, $usePicon = false)
196     {
197         if (!$this->listMembers) {
198             $this->getMembers();
199         }
200 
201         if ($this->listMembers) {
202             $this->sendMessageToList($this->listMembers, $sendMessage, $withName, $newLine, $usePicon);
203         }
204     }
205 
206     /**
207      * Build a Reply Message.
208      *
209      * @param ChatworkMessage $chatworkMessage
210      * @param bool $newLine
211      *
212      * @return string
213      */
214     public function buildReplyInRoom($chatworkMessage, $newLine = true)
215     {
216         return $this->buildReply($this->room_id, $chatworkMessage, $newLine);
217     }
218 
219     /**
220      * Build a Reply Message and append it to current Message.
221      *
222      * @param ChatworkMessage $chatworkMessage
223      * @param bool $newLine
224      *
225      * @throws \Exception
226      *
227      * @return string $message
228      */
229     public function appendReplyInRoom($chatworkMessage, $newLine = true)
230     {
231         return $this->appendReply($this->room_id, $chatworkMessage, $newLine);
232     }
233 
234     /**
235      * Reply list messages in room.
236      *
237      * @param ChatworkMessage|ChatworkMessage[] $chatworkMessages
238      * @param string $msg
239      * @param bool $newLine
240      * @param bool $resetMessage
241      */
242     public function reply($chatworkMessages, $msg, $newLine = true, $resetMessage = true)
243     {
244         if ($resetMessage) {
245             $this->resetMessage();
246         }
247         if ($chatworkMessages instanceof ChatworkMessage) {
248             $this->appendReplyInRoom($chatworkMessages, $newLine);
249         } else {
250             foreach ($chatworkMessages as $chatworkMessage) {
251                 $this->appendReplyInRoom($chatworkMessage, $newLine);
252             }
253         }
254         $this->appendMessage($msg);
255         $this->sendMessage();
256     }
257 }
258 
API documentation generated by ApiGen