From c69e75809365a56a653099c8f5652ae7236b26e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lonsk=C3=BD?= Date: Sun, 22 Oct 2017 14:36:43 +0200 Subject: [PATCH 1/6] return response --- src/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 75497f4..b504d55 100644 --- a/src/Client.php +++ b/src/Client.php @@ -360,7 +360,7 @@ public function createMessage() * Send a message. * * @param \Maknz\Slack\Message $message - * @return void + * @return \Psr\Http\Message\ResponseInterface */ public function sendMessage(Message $message) { @@ -372,7 +372,7 @@ public function sendMessage(Message $message) 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]); } /** From 8a1d27865788aa571516520992342dd4bfc18f7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lonsk=C3=BD?= Date: Mon, 23 Oct 2017 00:26:54 +0200 Subject: [PATCH 2/6] allow web api --- src/Client.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index b504d55..219d171 100644 --- a/src/Client.php +++ b/src/Client.php @@ -360,19 +360,25 @@ public function createMessage() * Send a message. * * @param \Maknz\Slack\Message $message + * @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) { + 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())); + } - return $this->guzzle->post($this->endpoint, ['body' => $encoded]); + return $this->guzzle->post($this->endpoint, ['body' => $encoded]); + } } /** From f2232c698c3b39b26a01cf7f02926e6f7b57d3dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lonsk=C3=BD?= Date: Mon, 23 Oct 2017 01:38:02 +0200 Subject: [PATCH 3/6] serialize attachments --- src/Client.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Client.php b/src/Client.php index 219d171..1be5548 100644 --- a/src/Client.php +++ b/src/Client.php @@ -368,6 +368,11 @@ public function sendMessage(Message $message, $web_api = false) $payload = $this->preparePayload($message); 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 { From edd6818416f26da401bcab388e9738dd415ae07e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lonsk=C3=BD?= Date: Wed, 1 Nov 2017 17:16:46 +0100 Subject: [PATCH 4/6] confirmation could not be presented --- src/AttachmentAction.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/AttachmentAction.php b/src/AttachmentAction.php index fe38073..3a03a73 100644 --- a/src/AttachmentAction.php +++ b/src/AttachmentAction.php @@ -216,13 +216,19 @@ public function setConfirm($confirm) */ public function toArray() { - return [ + $confirm = $this->getConfirm(); + $ret = [ 'name' => $this->getName(), 'text' => $this->getText(), 'style' => $this->getStyle(), 'type' => $this->getType(), 'value' => $this->getValue(), - 'confirm' => $this->getConfirm()->toArray(), ]; + + if($confirm){ + $ret['confirm'] = $confirm->toArray(); + } + + return $ret; } } From ff805c7ec4336a43ae5a691caf5ed93cc7116ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lonsk=C3=BD?= Date: Thu, 9 Nov 2017 00:11:53 +0100 Subject: [PATCH 5/6] Filter empty values, Fix actions in attachments with missing callback_id --- src/ActionConfirmation.php | 8 ++++- src/Attachment.php | 61 +++++++++++++++++++++++++++++++------- src/AttachmentAction.php | 18 +++++------ src/AttachmentField.php | 8 ++++- src/Client.php | 4 +++ 5 files changed, 77 insertions(+), 22 deletions(-) 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 3a03a73..bb19043 100644 --- a/src/AttachmentAction.php +++ b/src/AttachmentAction.php @@ -216,18 +216,18 @@ public function setConfirm($confirm) */ public function toArray() { - $confirm = $this->getConfirm(); $ret = [ - 'name' => $this->getName(), - 'text' => $this->getText(), - 'style' => $this->getStyle(), - 'type' => $this->getType(), - 'value' => $this->getValue(), + 'name' => $this->getName(), + 'text' => $this->getText(), + 'style' => $this->getStyle(), + 'type' => $this->getType(), + 'value' => $this->getValue(), + 'confirm' => $this->getConfirm()->toArray(), ]; - 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 1be5548..251354a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -410,6 +410,10 @@ public function preparePayload(Message $message) $payload['attachments'] = $this->getAttachmentsAsArrays($message); + $payload = array_filter($payload, function($item) { + return !empty($item); + }); + return $payload; } From 2c62c5aba74826b9ff24e5a403cadb8e66c20333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lonsk=C3=BD?= Date: Sat, 9 Dec 2017 12:30:45 +0100 Subject: [PATCH 6/6] cofirm is not needed for action --- src/AttachmentAction.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/AttachmentAction.php b/src/AttachmentAction.php index bb19043..9e8864a 100644 --- a/src/AttachmentAction.php +++ b/src/AttachmentAction.php @@ -216,15 +216,19 @@ public function setConfirm($confirm) */ public function toArray() { + $_confirm = $this->getConfirm(); $ret = [ 'name' => $this->getName(), 'text' => $this->getText(), 'style' => $this->getStyle(), 'type' => $this->getType(), 'value' => $this->getValue(), - 'confirm' => $this->getConfirm()->toArray(), ]; + if ($_confirm) { + $ret['confirm'] = $_confirm->toArray(); + } + $ret = array_filter($ret, function($item) { return !empty($item); });