From 3bac7dcf6babda08177b2123af7c3fee25c06ea9 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 2 Sep 2022 23:08:55 +1000 Subject: [PATCH] More functionality with emphemeral messages --- src/Interactive/BlockActions.php | 11 +++++++- src/Jobs/DeleteResponse.php | 48 ++++++++++++++++++++++++++++++++ src/Message.php | 17 ++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/Jobs/DeleteResponse.php diff --git a/src/Interactive/BlockActions.php b/src/Interactive/BlockActions.php index b745914..b28d573 100644 --- a/src/Interactive/BlockActions.php +++ b/src/Interactive/BlockActions.php @@ -76,6 +76,7 @@ final class BlockActions extends Base return $this->action('value'); case 'actions': + case 'response_url': return object_get($this->_data,$key); case 'block_id': @@ -87,6 +88,7 @@ final class BlockActions extends Base // For app hometab, the callback is in the view->callback_id array. return object_get($this->_data,sprintf('%s.%s',$this->container_type,$key)); + case 'message': case 'message_attachment': return NULL; @@ -105,7 +107,14 @@ final class BlockActions extends Base // For Block Actions that are messages case 'message_ts': - return object_get($this->_data,'message.ts'); + switch ($this->container_type) { + // Ephemeral messages + case 'message': + return object_get($this->_data,'container.message_ts'); + + default: + return object_get($this->_data,'message.ts'); + } case 'team_id': // view.team_id represent workspace publishing view return object_get($this->_data,'user.team_id'); diff --git a/src/Jobs/DeleteResponse.php b/src/Jobs/DeleteResponse.php new file mode 100644 index 0000000..5b7794d --- /dev/null +++ b/src/Jobs/DeleteResponse.php @@ -0,0 +1,48 @@ +_data['url'] = $url; + } + + /** + * Execute the job. + * + * @return void + * @throws \Exception + */ + public function handle() + { + Log::info(sprintf('%s:Start - Delete Response [%s]',static::LOGKEY,$this->url),['m'=>__METHOD__]); + + $o = new Message; + $o->text('') + ->delete_original(); + + try { + $o->respond($this->url); + + Log::debug(sprintf('%s:Deleted Slack Message: %s',static::LOGKEY,$this->url),['m'=>__METHOD__]); + + } catch (SlackException $e) { + Log::error(sprintf('%s:Failed to delete slack message [%s] [%s]',static::LOGKEY,$this->url,$e->getMessage()),['m'=>__METHOD__]); + } + } +} \ No newline at end of file diff --git a/src/Message.php b/src/Message.php index f910215..6ebb981 100644 --- a/src/Message.php +++ b/src/Message.php @@ -106,6 +106,19 @@ final class Message extends BlockKit return $this; } + /** + * For messages that we interact with via a response_url + * + * @param bool $bool + * @return Message + */ + public function delete_original(bool $bool=TRUE): self + { + $this->delete_original = $bool; + + return $this; + } + public function forgetTS(): self { $this->_data->forget('ts'); @@ -187,13 +200,15 @@ final class Message extends BlockKit if (! $result) throw new \Exception('CURL exec returned an empty response: '.serialize(curl_getinfo($request))); + $result = json_decode($result); + } catch (\Exception $e) { Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]); throw new \Exception($e->getMessage()); } - if ($result !== 'ok') { + if (! $result->ok) { switch ($result) { default: Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$result]);