Options code tidyup

This commit is contained in:
Deon George 2022-09-04 10:28:39 +10:00
parent b6dc14971f
commit ff00e88417
5 changed files with 29 additions and 44 deletions

View File

@ -39,14 +39,12 @@ abstract class Base extends SlackBase
public function __get(string $key) public function __get(string $key)
{ {
switch ($key) { switch ($key) {
case 'team_id':
return object_get($this->_data,'team.id');
case 'channel_id': case 'channel_id':
return object_get($this->_data,'channel.id'); return object_get($this->_data,'channel.id');
case 'enterprise_id': case 'enterprise_id':
return object_get($this->_data,'team.'.$key); return object_get($this->_data,'team.'.$key);
case 'team_id':
return object_get($this->_data,'team.id');
case 'user_id': case 'user_id':
return object_get($this->_data,'user.id'); return object_get($this->_data,'user.id');
@ -59,23 +57,11 @@ abstract class Base extends SlackBase
} }
/** /**
* Interactive messages can return their output in the incoming HTTP post * Interactive messages can return their output in the incoming HTTP post.
* This function should be overwritten in the parent class, and finish by calling return Message::blank();
* *
* @return Message * @return Message
* @throws \Exception * @throws \Exception
*/ */
public function respond(): Message abstract public function respond(): Message;
{
Log::info(sprintf('%s:Interactive Option - Callback [%s] Name [%s] Value [%s]',self::LOGKEY,$this->callback_id,$this->name,$this->value),['m'=>__METHOD__]);
if (preg_match('/^(.*)\|([0-9]+)/',$this->callback_id)) {
[$action,$id] = explode('|',$this->callback_id,2);
} else {
// If we get here, its an action that we dont know about.
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',self::LOGKEY,$this->callback_id),['m'=>__METHOD__]);
}
return Message::blank();
}
} }

View File

@ -126,7 +126,7 @@ use Slack\Message;
) )
) )
*/ */
class BlockSuggestion extends Base abstract class BlockSuggestion extends Base
{ {
protected const LOGKEY = 'OIM'; protected const LOGKEY = 'OIM';

View File

@ -2,7 +2,6 @@
namespace Slack\Options; namespace Slack\Options;
use Illuminate\Http\Request;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -26,13 +25,15 @@ class Factory {
* @param string $type * @param string $type
* @param array $request * @param array $request
* @return Base * @return Base
* @note Options classes must be defined in the calling application and extend these self::map abstract classes
* otherwise errors will be thrown
*/ */
public static function create(string $type,array $request) public static function create(string $type,array $request)
{ {
$class = Arr::get(config('slack.options',self::map),$type,Unknown::class); $class = Arr::get(config('slack.options',self::map),$type,Unknown::class);
Log::debug(sprintf('%s:Working out Interactive Options Event Class for [%s] as [%s]',static::LOGKEY,$type,$class),['m'=>__METHOD__]); Log::debug(sprintf('%s:Working out Interactive Options Event Class for [%s] as [%s]',static::LOGKEY,$type,$class),['m'=>__METHOD__]);
if (App::environment() == 'dev') if (App::environment() == 'local')
file_put_contents('/tmp/option.'.$type,print_r(json_decode($request->input('payload')),TRUE)); file_put_contents('/tmp/option.'.$type,print_r(json_decode($request->input('payload')),TRUE));
return new $class($request); return new $class($request);

View File

@ -33,7 +33,7 @@ use Slack\Message;
* [attachment_id] => 3 * [attachment_id] => 3
* [token] => Oow8S2EFvrZoS9z8N4nwf9Jo * [token] => Oow8S2EFvrZoS9z8N4nwf9Jo
*/ */
class InteractiveMessage extends Base abstract class InteractiveMessage extends Base
{ {
protected const LOGKEY = 'OIM'; protected const LOGKEY = 'OIM';
@ -49,25 +49,4 @@ class InteractiveMessage extends Base
return parent::__get($key); return parent::__get($key);
} }
} }
/**
* Interactive messages can return their output in the incoming HTTP post
*
* @return Message
* @throws \Exception
*/
public function respond(): Message
{
Log::info(sprintf('%s:Interactive Option - Callback [%s] Name [%s] Value [%s]',static::LOGKEY,$this->callback_id,$this->name,$this->value),['m'=>__METHOD__]);
if (preg_match('/^(.*)\|([0-9]+)/',$this->callback_id)) {
[$action,$id] = explode('|',$this->callback_id,2);
} else {
// If we get here, its an action that we dont know about.
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',static::LOGKEY,$this->callback_id),['m'=>__METHOD__]);
}
return (new Message)->blank();
}
} }

View File

@ -4,6 +4,8 @@ namespace Slack\Options;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Slack\Message;
/** /**
* Catch all unknown slack event that we havent specifically programmed for. * Catch all unknown slack event that we havent specifically programmed for.
* *
@ -11,10 +13,27 @@ use Illuminate\Support\Facades\Log;
*/ */
final class Unknown extends Base final class Unknown extends Base
{ {
protected const LOGKEY = 'OU-';
public function __construct(array $request) public function __construct(array $request)
{ {
Log::notice(sprintf('SOU:UNKNOWN Slack Interaction Option received [%s]',get_class($this)),['m'=>__METHOD__]); Log::notice(sprintf('SOU:UNKNOWN Slack Interaction Option received [%s]',get_class($this)),['m'=>__METHOD__]);
parent::__construct($request); parent::__construct($request);
} }
/**
* Interactive messages can return their output in the incoming HTTP post
*
* @return Message
* @throws \Exception
*/
public function respond(): Message
{
Log::info(sprintf('%s:Unknown Option - Callback [%s] Name [%s] Value [%s]',static::LOGKEY,$this->callback_id,$this->name,$this->value),['m'=>__METHOD__]);
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',static::LOGKEY,$this->callback_id),['m'=>__METHOD__]);
return Message::blank();
}
} }