diff --git a/src/ApiClient.php b/src/ApiClient.php index 831219e..8393ec9 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -360,9 +360,13 @@ public function postMessage(Message $message) $options = [ 'text' => $message->getText(), 'channel' => $message->data['channel'], - 'as_user' => true, + 'as_user' => $message->isAsUser(), ]; + if ($message->getUsername()) { + $options['username'] = $message->getUsername(); + } + if ($message->hasAttachments()) { $options['attachments'] = json_encode($message->getAttachments()); } diff --git a/src/Message/Message.php b/src/Message/Message.php index 035b3c3..0327cab 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -68,6 +68,27 @@ public function getUser() return $this->client->getUserById($this->data['user']); } + /** + * Send message as user or not. + * By default sends as user. + * + * @return bool + */ + public function isAsUser() + { + return isset($this->data['as_user']) ? $this->data['as_user'] : true; + } + + /** + * Get message's user display name + * + * @return string + */ + public function getUsername() + { + return isset($this->data['username']) ? $this->data['username'] : ''; + } + /** * {@inheritDoc} */ diff --git a/src/RealTimeClient.php b/src/RealTimeClient.php index be0480e..7e66097 100644 --- a/src/RealTimeClient.php +++ b/src/RealTimeClient.php @@ -388,13 +388,12 @@ private function onMessage(WebSocketMessageInterface $message) break; case 'channel_created': - $this->getChannelById($payload['channel']['id'])->then(function (Channel $channel) { - $this->channels[$channel->getId()] = $channel; - }); + $channel = new Channel($this, $payload['channel']); + $this->channels[$channel->getId()] = $channel; break; case 'channel_deleted': - unset($this->channels[$payload['channel']['id']]); + unset($this->channels[$payload['channel']]); break; case 'channel_rename': @@ -403,11 +402,11 @@ private function onMessage(WebSocketMessageInterface $message) break; case 'channel_archive': - $this->channels[$payload['channel']['id']]->data['is_archived'] = true; + $this->channels[$payload['channel']]->data['is_archived'] = true; break; case 'channel_unarchive': - $this->channels[$payload['channel']['id']]->data['is_archived'] = false; + $this->channels[$payload['channel']]->data['is_archived'] = false; break; case 'group_joined': @@ -416,16 +415,16 @@ private function onMessage(WebSocketMessageInterface $message) break; case 'group_rename': - $this->groups[$payload['group']['id']]->data['name'] + $this->groups[$payload['channel']['id']]->data['name'] = $payload['channel']['name']; break; case 'group_archive': - $this->groups[$payload['group']['id']]->data['is_archived'] = true; + $this->groups[$payload['channel']]->data['is_archived'] = true; break; case 'group_unarchive': - $this->groups[$payload['group']['id']]->data['is_archived'] = false; + $this->groups[$payload['channel']]->data['is_archived'] = false; break; case 'im_created':