More functionality with emphemeral messages

This commit is contained in:
Deon George 2022-09-02 23:08:55 +10:00
parent e9a4eae7d1
commit 3bac7dcf6b
3 changed files with 74 additions and 2 deletions

View File

@ -76,6 +76,7 @@ final class BlockActions extends Base
return $this->action('value'); return $this->action('value');
case 'actions': case 'actions':
case 'response_url':
return object_get($this->_data,$key); return object_get($this->_data,$key);
case 'block_id': case 'block_id':
@ -87,6 +88,7 @@ final class BlockActions extends Base
// For app hometab, the callback is in the view->callback_id array. // For app hometab, the callback is in the view->callback_id array.
return object_get($this->_data,sprintf('%s.%s',$this->container_type,$key)); return object_get($this->_data,sprintf('%s.%s',$this->container_type,$key));
case 'message':
case 'message_attachment': case 'message_attachment':
return NULL; return NULL;
@ -105,7 +107,14 @@ final class BlockActions extends Base
// For Block Actions that are messages // For Block Actions that are messages
case 'message_ts': 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 case 'team_id': // view.team_id represent workspace publishing view
return object_get($this->_data,'user.team_id'); return object_get($this->_data,'user.team_id');

View File

@ -0,0 +1,48 @@
<?php
namespace Slack\Jobs;
use Illuminate\Support\Facades\Log;
use Slack\Exceptions\SlackException;
use Slack\Message;
final class DeleteResponse extends Job
{
private const LOGKEY = 'JDC';
/**
* Create a new job instance.
*
* @param string $url
* @throws \Exception
*/
public function __construct(string $url)
{
$this->_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__]);
}
}
}

View File

@ -106,6 +106,19 @@ final class Message extends BlockKit
return $this; 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 public function forgetTS(): self
{ {
$this->_data->forget('ts'); $this->_data->forget('ts');
@ -187,13 +200,15 @@ final class Message extends BlockKit
if (! $result) if (! $result)
throw new \Exception('CURL exec returned an empty response: '.serialize(curl_getinfo($request))); throw new \Exception('CURL exec returned an empty response: '.serialize(curl_getinfo($request)));
$result = json_decode($result);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]); Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]);
throw new \Exception($e->getMessage()); throw new \Exception($e->getMessage());
} }
if ($result !== 'ok') { if (! $result->ok) {
switch ($result) { switch ($result) {
default: default:
Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$result]); Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$result]);