diff --git a/src/ActionConfirmation.php b/src/ActionConfirmation.php index 4926d51..c95cc06 100644 --- a/src/ActionConfirmation.php +++ b/src/ActionConfirmation.php @@ -140,11 +140,17 @@ public function setDismissText($dismissText) */ public function toArray() { - return [ + $ret = [ 'title' => $this->getTitle(), 'text' => $this->getText(), 'ok_text' => $this->getOkText(), 'dismiss_text' => $this->getDismissText(), ]; + + $ret = array_filter($ret, function($item) { + return !empty($item); + }); + + return $ret; } } diff --git a/src/Attachment.php b/src/Attachment.php index 734a01b..450ff7b 100644 --- a/src/Attachment.php +++ b/src/Attachment.php @@ -13,6 +13,13 @@ class Attachment */ protected $fallback; + /** + * The callback_id + * + * @var string + */ + protected $callback_id; + /** * Optional text that should appear within the attachment. * @@ -139,6 +146,10 @@ public function __construct(array $attributes) $this->setFallback($attributes['fallback']); } + if (isset($attributes['callback_id'])) { + $this->setCallbackId($attributes['callback_id']); + } + if (isset($attributes['text'])) { $this->setText($attributes['text']); } @@ -227,6 +238,29 @@ public function setFallback($fallback) return $this; } + + /** + * Get the callback_id. + * + * @return string + */ + public function getCallbackId() { + return $this->callback_id; + } + + + /** + * Set the callback_id. + * + * @param string $fallback + * @return $this + */ + public function setCallbackId($callback_id) { + $this->callback_id = $callback_id; + + return $this; + } + /** * Get the optional text to appear within the attachment. * @@ -679,18 +713,19 @@ public function addAction($action) public function toArray() { $data = [ - 'fallback' => $this->getFallback(), - 'text' => $this->getText(), - 'pretext' => $this->getPretext(), - 'color' => $this->getColor(), - 'footer' => $this->getFooter(), + 'fallback' => $this->getFallback(), + 'callback_id' => $this->getCallbackId(), + 'text' => $this->getText(), + 'pretext' => $this->getPretext(), + 'color' => $this->getColor(), + 'footer' => $this->getFooter(), 'footer_icon' => $this->getFooterIcon(), - 'ts' => $this->getTimestamp() ? $this->getTimestamp()->getTimestamp() : null, - 'mrkdwn_in' => $this->getMarkdownFields(), - 'image_url' => $this->getImageUrl(), - 'thumb_url' => $this->getThumbUrl(), - 'title' => $this->getTitle(), - 'title_link' => $this->getTitleLink(), + 'ts' => $this->getTimestamp() ? $this->getTimestamp()->getTimestamp() : null, + 'mrkdwn_in' => $this->getMarkdownFields(), + 'image_url' => $this->getImageUrl(), + 'thumb_url' => $this->getThumbUrl(), + 'title' => $this->getTitle(), + 'title_link' => $this->getTitleLink(), 'author_name' => $this->getAuthorName(), 'author_link' => $this->getAuthorLink(), 'author_icon' => $this->getAuthorIcon(), @@ -699,6 +734,10 @@ public function toArray() $data['fields'] = $this->getFieldsAsArrays(); $data['actions'] = $this->getActionsAsArrays(); + $data = array_filter($data, function($item) { + return !empty($item); + }); + return $data; } diff --git a/src/AttachmentAction.php b/src/AttachmentAction.php index fe38073..9e8864a 100644 --- a/src/AttachmentAction.php +++ b/src/AttachmentAction.php @@ -216,13 +216,23 @@ public function setConfirm($confirm) */ public function toArray() { - return [ - 'name' => $this->getName(), - 'text' => $this->getText(), - 'style' => $this->getStyle(), - 'type' => $this->getType(), - 'value' => $this->getValue(), - 'confirm' => $this->getConfirm()->toArray(), + $_confirm = $this->getConfirm(); + $ret = [ + 'name' => $this->getName(), + 'text' => $this->getText(), + 'style' => $this->getStyle(), + 'type' => $this->getType(), + 'value' => $this->getValue(), ]; + + if ($_confirm) { + $ret['confirm'] = $_confirm->toArray(); + } + + $ret = array_filter($ret, function($item) { + return !empty($item); + }); + + return $ret; } } diff --git a/src/AttachmentField.php b/src/AttachmentField.php index 166e711..784f0bf 100644 --- a/src/AttachmentField.php +++ b/src/AttachmentField.php @@ -125,10 +125,16 @@ public function setShort($value) */ public function toArray() { - return [ + $ret = [ 'title' => $this->getTitle(), 'value' => $this->getValue(), 'short' => $this->getShort(), ]; + + $ret = array_filter($ret, function($item) { + return !empty($item); + }); + + return $ret; } } diff --git a/src/Client.php b/src/Client.php index 75497f4..251354a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -360,19 +360,30 @@ public function createMessage() * Send a message. * * @param \Maknz\Slack\Message $message - * @return void + * @param bool $web_api Incoming web hook or Web API + * @return \Psr\Http\Message\ResponseInterface */ - public function sendMessage(Message $message) + public function sendMessage(Message $message, $web_api = false) { $payload = $this->preparePayload($message); - $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE); - - if ($encoded === false) { - throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg())); + if ($web_api) { + foreach ($payload as $key => $p) { + if (is_array($p)) { + $payload[$key] = json_encode($p, JSON_UNESCAPED_UNICODE); + } + } + return $this->guzzle->post($this->endpoint, ['form_params' => $payload]); } + else { + $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE); + + if ($encoded === false) { + throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg())); + } - $this->guzzle->post($this->endpoint, ['body' => $encoded]); + return $this->guzzle->post($this->endpoint, ['body' => $encoded]); + } } /** @@ -399,6 +410,10 @@ public function preparePayload(Message $message) $payload['attachments'] = $this->getAttachmentsAsArrays($message); + $payload = array_filter($payload, function($item) { + return !empty($item); + }); + return $payload; }