Added in HTTP interactive options messages and Controller

This commit is contained in:
Deon George 2022-02-24 12:27:36 +11:00
parent 29d3591125
commit c5a13046ed
7 changed files with 65 additions and 16 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace Slack\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Slack\Client\Payload;
use Slack\Options\Factory as SlackOptionsFactory;
use App\Http\Controllers\Controller;
class InteractiveOptionsController extends Controller
{
private const LOGKEY = 'CIO';
/**
* Fire slack event
*
* @param Request $request
* @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory
*/
public function fire(Request $request)
{
$event = SlackOptionsFactory::make(new Payload(json_decode($request->payload,TRUE),TRUE));
Log::debug(sprintf('%s:Firing Event [%s] and responding [%s]',static::LOGKEY,get_class($event),$event->respondNow));
if ($event->respondNow) {
if (! method_exists($event,'respond')) {
Log::alert(sprintf('%s:Cant respond to Event [%s], no respond method',static::LOGKEY,get_class($event)),['m'=>__METHOD__]);
} else {
return ($x=$event->respond())->isEmpty() ? NULL : $x;
}
} else {
event($event);
Log::info(sprintf('%s:Dispatched Event [%s]',static::LOGKEY,get_class($event)),['m'=>__METHOD__]);
return response('IO Event Processed',200);
}
}
}

View File

@ -8,8 +8,7 @@ use Illuminate\Support\Facades\Log;
use Slack\Client\Payload; use Slack\Client\Payload;
use Slack\Event\Factory as EventFactory; use Slack\Event\Factory as EventFactory;
use Slack\Interactive\Factory as InteractiveFactory; use Slack\Interactive\Factory as InteractiveFactory;
use Slack\Options\Factory as OptionsFactory;
use App\Slack\Options\Factory as OptionsFactory;
class CheckRequest class CheckRequest
{ {
@ -47,7 +46,7 @@ class CheckRequest
break; break;
case 'api/imsgopt': case 'api/imsgopt':
$event = OptionsFactory::make($request); $event = OptionsFactory::make(new Payload(json_decode($request->payload,TRUE),TRUE));
break; break;
case 'api/imsg': case 'api/imsg':

View File

@ -298,11 +298,12 @@ final class Message extends BlockKit
* *
* @param Collection $collection * @param Collection $collection
* @return Message * @return Message
* @todo - Check this is a valid option
*/ */
public function option_groups(Collection $collection): self public function option_groups(Collection $collection): self
{ {
$this->option_groups = $collection; $this->option_groups = $collection;
return $this;
} }
/** /**

View File

@ -9,14 +9,16 @@ use Slack\Base as SlackBase;
abstract class Base extends SlackBase abstract class Base extends SlackBase
{ {
// Does the event respond with a reply to the HTTP request, or via a post with a trigger // Does the event respond with a reply to the HTTP request, or via a post with a trigger
// Child class should have a respond() function.
// (There should be a local implementation of the child class should respondNow = TRUE)
public $respondNow = TRUE; public $respondNow = TRUE;
public function __construct(Request $request) public function __construct(array $request)
{ {
Log::info(sprintf('SOb:Slack INTERACTIVE MESSAGE Initialised [%s]',get_class($this)),['m'=>__METHOD__]); Log::info(sprintf('SOb:Slack INTERACTIVE MESSAGE Initialised [%s]',get_class($this)),['m'=>__METHOD__]);
// Our data is in a payload value // Our data is in a payload value
$this->_data = json_decode($request->input('payload')); parent::__construct($request);
} }
/** /**

View File

@ -6,6 +6,8 @@ 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;
use Slack\Client\Payload;
use Slack\Interactive\Unknown;
class Factory { class Factory {
private const LOGKEY = 'SOf'; private const LOGKEY = 'SOf';
@ -14,7 +16,7 @@ class Factory {
* @var array event type to event class mapping * @var array event type to event class mapping
*/ */
public const map = [ public const map = [
'interactive_message'=>InteractiveMessage::class, //'interactive_message' => InteractiveMessage::class,
]; ];
/** /**
@ -24,9 +26,9 @@ class Factory {
* @param Request $request * @param Request $request
* @return Base * @return Base
*/ */
public static function create(string $type,Request $request) public static function create(string $type,array $request)
{ {
$class = Arr::get(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() == 'dev')
@ -35,16 +37,15 @@ class Factory {
return new $class($request); return new $class($request);
} }
public static function make(Request $request): Base public static function make(Payload $request): Base
{ {
// During the life of the event, this method is called twice - once during Middleware processing, and finally by the Controller. // During the life of the event, this method is called twice - once during Middleware processing, and finally by the Controller.
static $o = NULL; static $o = NULL;
static $or = NULL; static $or = NULL;
if (! $o OR ($or != $request)) { if (! $o OR ($or != $request)) {
$data = json_decode($request->input('payload'));
$or = $request; $or = $request;
$o = self::create($data->type,$request); $o = self::create(Arr::get($request->getData(),'payload.type'),Arr::get($request->getData(),'payload'));
} }
return $o; return $o;

View File

@ -34,7 +34,7 @@ use Slack\Message;
*/ */
class InteractiveMessage extends Base class InteractiveMessage extends Base
{ {
private const LOGKEY = 'OIM'; protected const LOGKEY = 'OIM';
public function __get($key) public function __get($key)
{ {
@ -69,5 +69,4 @@ class InteractiveMessage extends Base
return (new Message)->blank(); return (new Message)->blank();
} }
} }

View File

@ -33,4 +33,9 @@ app('router')
'uses' => 'InteractiveMessageController@fire', 'uses' => 'InteractiveMessageController@fire',
'as' => 'imsg', 'as' => 'imsg',
]); ]);
$router->post('imsgopt', [
'uses' => 'InteractiveOptionsController@fire',
'as' => 'imsgopt',
]);
}); });