Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1f4c5c9774 | ||
|
67f78b2f5d | ||
|
6c6d32b4eb | ||
|
1923f4486b | ||
|
b289f29948 | ||
|
e9980ff9fd | ||
|
145e322317 | ||
|
1b55d7ab52 | ||
|
be26c3c0a3 | ||
|
521de13d92 | ||
|
a1be3ccd09 | ||
|
2c791ccead | ||
|
1529f470fa | ||
|
905c207956 | ||
|
15a6933026 | ||
|
4ff944cb3a | ||
|
4c7d18c6b0 | ||
|
ff00e88417 | ||
|
b6dc14971f | ||
|
89c13bcb73 | ||
|
a68c7936a6 |
477
src/API.php
477
src/API.php
@ -4,6 +4,7 @@ namespace Slack;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Blockkit\Modal;
|
||||
@ -19,7 +20,7 @@ use Slack\Exceptions\{SlackAlreadyPinnedException,
|
||||
SlackNotInChannelException,
|
||||
SlackThreadNotFoundException,
|
||||
SlackTokenScopeException};
|
||||
use Slack\Models\{Team,User};
|
||||
use Slack\Models\{Team,Token,User};
|
||||
use Slack\Response\ChannelList;
|
||||
use Slack\Response\Generic;
|
||||
use Slack\Response\User as ResponseUser;
|
||||
@ -55,7 +56,7 @@ final class API
|
||||
];
|
||||
|
||||
// Our slack token to use
|
||||
private $_token;
|
||||
private Token $_token;
|
||||
|
||||
public function __construct(Team $o)
|
||||
{
|
||||
@ -74,242 +75,128 @@ final class API
|
||||
/**
|
||||
* Delete a message in a channel
|
||||
*
|
||||
* @param $channel
|
||||
* @param $timestamp
|
||||
* @param string $channel
|
||||
* @param string $timestamp
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function deleteChat($channel,$timestamp): Generic
|
||||
public function deleteChat(string $channel,string $timestamp): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Delete Message [%s] in [%s]',static::LOGKEY,$timestamp,$channel),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('chat.delete',['channel'=>$channel,'ts'=>$timestamp]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Messages on a channel from a specific timestamp
|
||||
*
|
||||
* @param $channel
|
||||
* @param $timestamp
|
||||
* @param int $limit
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getChannelHistory($channel,$timestamp,$limit=20): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Message History for Channel [%s] from Timestamp [%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('conversations.history',['channel'=>$channel,'oldest'=>$timestamp,'limit'=>$limit]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information on a channel.
|
||||
*
|
||||
* @param $channel
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getChannelInfo($channel): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Channel Information [%s]',static::LOGKEY,$channel),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('conversations.info',['channel'=>$channel]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of channels.
|
||||
*
|
||||
* @param int $limit
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getChannelList(int $limit=100): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Channel List',static::LOGKEY),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('conversations.list',['limit'=>$limit]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all messages from a thread
|
||||
*
|
||||
* @param string $channel
|
||||
* @param string $thread_ts
|
||||
* @return Chat
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getMessageHistory(string $channel,string $thread_ts): Chat
|
||||
{
|
||||
Log::debug(sprintf('%s:Get Message Threads for Message [%s] on Channel [%s]',static::LOGKEY,$thread_ts,$channel),['m'=>__METHOD__]);
|
||||
|
||||
return new Chat($this->execute('conversations.replies',['channel'=>$channel,'ts'=>$thread_ts]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information on a user
|
||||
*
|
||||
* @param string $team_id
|
||||
* @return ResponseTeam
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getTeam(string $team_id): ResponseTeam
|
||||
{
|
||||
Log::debug(sprintf('%s:Team Info [%s]',static::LOGKEY,$team_id),['m'=>__METHOD__]);
|
||||
|
||||
return new ResponseTeam($this->execute('team.info',['team'=>$team_id]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information on a user
|
||||
*
|
||||
* @param $user_id
|
||||
* @return ResponseUser
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getUser($user_id): ResponseUser
|
||||
{
|
||||
Log::debug(sprintf('%s:User Info [%s]',static::LOGKEY,$user_id),['m'=>__METHOD__]);
|
||||
|
||||
return new ResponseUser($this->execute('users.info',['user'=>$user_id]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a dialogue with the user
|
||||
*
|
||||
* @param string $trigger
|
||||
* @param string $dialog
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function dialogOpen(string $trigger,string $dialog): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Open a Dialog',static::LOGKEY),['m'=>__METHOD__,'d'=>$dialog,'t'=>$trigger]);
|
||||
|
||||
return new Generic($this->execute('dialog.open',json_encode(['dialog'=>$dialog,'trigger_id'=>$trigger])));
|
||||
return new Generic($this->execute('dialog.open',['dialog'=>$dialog,'trigger_id'=>$trigger]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate users to Enterprise IDs
|
||||
* Get Messages on a channel from a specific timestamp
|
||||
*
|
||||
* @param array $users
|
||||
* @param string $channel
|
||||
* @param string $timestamp
|
||||
* @param int $limit
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function migrationExchange(array $users): Generic
|
||||
public function getChannelHistory(string $channel,string $timestamp,int $limit=20): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Migrate Exchange [%s] users',static::LOGKEY,count($users)),['m'=>__METHOD__]);
|
||||
Log::debug(sprintf('%s:Message History for Channel [%s] from Timestamp [%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('migration.exchange',['users'=>join(',',$users)]));
|
||||
return new Generic($this->execute('conversations.history',['channel'=>$channel,'oldest'=>$timestamp,'limit'=>$limit],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Pin a message in a channel
|
||||
* Get information on a channel.
|
||||
*
|
||||
* @param $channel
|
||||
* @param $timestamp
|
||||
* @param string $channel
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function pinMessage(string $channel,string $timestamp): Generic
|
||||
public function getChannelInfo(string $channel): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Pin Message [%s|%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
|
||||
Log::debug(sprintf('%s:Channel Information [%s]',static::LOGKEY,$channel),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('pins.add',json_encode(['channel'=>$channel,'timestamp'=>$timestamp])));
|
||||
return new Generic($this->execute('conversations.info',['channel'=>$channel],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a Slack Message to a user as an ephemeral message
|
||||
* Get a list of channels.
|
||||
*
|
||||
* @param Message $request
|
||||
* @param int $limit
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function postEphemeral(Message $request): Generic
|
||||
public function getChannelList(int $limit=100): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Post a Slack Ephemeral Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
Log::debug(sprintf('%s:Channel List',static::LOGKEY),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('chat.postEphemeral',json_encode($request)));
|
||||
return new Generic($this->execute('conversations.list',['limit'=>$limit],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a Slack Message
|
||||
* Get all messages from a thread
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @param string $channel
|
||||
* @param string $thread_ts
|
||||
* @return Chat
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function postMessage(Message $request): Generic
|
||||
public function getMessageHistory(string $channel,string $thread_ts): Chat
|
||||
{
|
||||
Log::debug(sprintf('%s:Post a Slack Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
Log::debug(sprintf('%s:Get Message Threads for Message [%s] on Channel [%s]',static::LOGKEY,$thread_ts,$channel),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('chat.postMessage',json_encode($request)));
|
||||
return new Chat($this->execute('conversations.replies',['channel'=>$channel,'ts'=>$thread_ts],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a slack message
|
||||
* Get information on a user
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @param string $team_id
|
||||
* @return ResponseTeam
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function scheduleMessage(Message $request): Generic
|
||||
public function getTeam(string $team_id): ResponseTeam
|
||||
{
|
||||
Log::debug(sprintf('%s:Scheduling a Slack Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
Log::debug(sprintf('%s:Team Info [%s]',static::LOGKEY,$team_id),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('chat.scheduleMessage',json_encode($request)));
|
||||
return new ResponseTeam($this->execute('team.info',['team'=>$team_id],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the scheduled messages
|
||||
* Get information on a user
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @param string $user_id
|
||||
* @return ResponseUser
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function scheduleMessagesList(string $request=NULL): Generic
|
||||
public function getUser(string $user_id): ResponseUser
|
||||
{
|
||||
Log::debug(sprintf('%s:Get the Scheduled Messages in Slack',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
Log::debug(sprintf('%s:User Info [%s]',static::LOGKEY,$user_id),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('chat.scheduledMessages.list',$request ? ['channel'=>$request] : []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Pin from a message
|
||||
*
|
||||
* @param $channel
|
||||
* @param $timestamp
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function unpinMessage($channel,$timestamp): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Remove Pin from Message [%s|%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('pins.remove',json_encode(['channel'=>$channel,'timestamp'=>$timestamp])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a Slack Message
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function updateMessage(Message $request): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Update a Slack Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
|
||||
return new Generic($this->execute('chat.update',json_encode($request)));
|
||||
return new ResponseUser($this->execute('users.info',['user'=>$user_id],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of channels for a user (the bot normally)
|
||||
*
|
||||
* @param User $uo
|
||||
* @param int $limit
|
||||
* @param User $uo
|
||||
* @param int $limit
|
||||
* @param string|null $cursor
|
||||
* @return ChannelList
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function getUserChannels(User $uo,int $limit=100,string $cursor=NULL): ChannelList
|
||||
{
|
||||
@ -325,105 +212,205 @@ final class API
|
||||
if ($cursor)
|
||||
$args->put('cursor',$cursor);
|
||||
|
||||
return new ChannelList($this->execute('users.conversations',$args->toArray()));
|
||||
return new ChannelList($this->execute('users.conversations',$args->toArray(),TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate users to Enterprise IDs
|
||||
*
|
||||
* @param array $users
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function migrationExchange(array $users): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Migrate Exchange [%s] users',static::LOGKEY,count($users)),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('migration.exchange',['users'=>join(',',$users)],TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Pin a message in a channel
|
||||
*
|
||||
* @param string $channel
|
||||
* @param string $timestamp
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function pinMessage(string $channel,string $timestamp): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Pin Message [%s|%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('pins.add',['channel'=>$channel,'timestamp'=>$timestamp]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a Slack Message to a user as an ephemeral message
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function postEphemeral(Message $request): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Post a Slack Ephemeral Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
|
||||
return new Generic($this->execute('chat.postEphemeral',$request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Post a Slack Message
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function postMessage(Message $request): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Post a Slack Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
|
||||
return new Generic($this->execute('chat.postMessage',$request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a slack message
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function scheduleMessage(Message $request): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Scheduling a Slack Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
|
||||
return new Generic($this->execute('chat.scheduleMessage',$request));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the scheduled messages
|
||||
*
|
||||
* @param string|null $request
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function scheduleMessagesList(string $request=NULL): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Get the Scheduled Messages in Slack',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
|
||||
return new Generic($this->execute('chat.scheduledMessages.list',$request ? ['channel'=>$request] : []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a Pin from a message
|
||||
*
|
||||
* @param string $channel
|
||||
* @param string $timestamp
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function unpinMessage(string $channel,string $timestamp): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Remove Pin from Message [%s|%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
|
||||
|
||||
return new Generic($this->execute('pins.remove',['channel'=>$channel,'timestamp'=>$timestamp]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a Slack Message
|
||||
*
|
||||
* @param Message $request
|
||||
* @return Generic
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function updateMessage(Message $request): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Update a Slack Message',static::LOGKEY),['m'=>__METHOD__,'r'=>$request]);
|
||||
|
||||
return new Generic($this->execute('chat.update',$request));
|
||||
}
|
||||
|
||||
public function viewOpen(string $trigger,Modal $view): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Open a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]);
|
||||
|
||||
return new Generic($this->execute('views.open',json_encode(['trigger_id'=>$trigger,'view'=>$view])));
|
||||
return new Generic($this->execute('views.open',['trigger_id'=>$trigger,'view'=>$view]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish a view
|
||||
*
|
||||
* @param string $user
|
||||
* @param string $view
|
||||
* @param Modal $view
|
||||
* @param string $hash
|
||||
* @return Generic
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
* @todo Add some smarts to detect if the new view is the same as the current view, and thus no need to post.
|
||||
*/
|
||||
public function viewPublish(string $user,Modal $view,string $hash=''): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Publish a view',static::LOGKEY),['m'=>__METHOD__,'u'=>$user,'h'=>$hash]);
|
||||
|
||||
return new Generic($this->execute('views.publish',json_encode($hash ? ['user_id'=>$user,'view'=>$view,'hash'=>$hash] : ['user_id'=>$user,'view'=>$view])));
|
||||
return new Generic($this->execute('views.publish',$hash ? ['user_id'=>$user,'view'=>$view,'hash'=>$hash] : ['user_id'=>$user,'view'=>$view]));
|
||||
}
|
||||
|
||||
public function viewPush(string $trigger,Modal $view): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Push a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]);
|
||||
|
||||
return new Generic($this->execute('views.push',json_encode(['trigger_id'=>$trigger,'view'=>$view])));
|
||||
return new Generic($this->execute('views.push',['trigger_id'=>$trigger,'view'=>$view]));
|
||||
}
|
||||
|
||||
public function viewUpdate(string $view_id,Modal $view): Generic
|
||||
{
|
||||
Log::debug(sprintf('%s:Update a view',static::LOGKEY),['m'=>__METHOD__,'id'=>$view_id]);
|
||||
|
||||
return new Generic($this->execute('views.update',json_encode(['view_id'=>$view_id,'view'=>$view])));
|
||||
return new Generic($this->execute('views.update',['view_id'=>$view_id,'view'=>$view]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the Slack API
|
||||
*
|
||||
* @param string $method
|
||||
* @param null $parameters
|
||||
* @param mixed $parameters
|
||||
* @param bool $asForm
|
||||
* @return object
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
private function execute(string $method,$parameters = NULL): object
|
||||
private function execute(string $method,mixed $parameters,bool $asForm=FALSE): object
|
||||
{
|
||||
switch (config('app.env')) {
|
||||
case 'dev': $url = 'http://steno:3000';
|
||||
case 'steno': $url = 'http://steno:3000';
|
||||
break;
|
||||
case 'testing': $url = 'http://localhost:3000';
|
||||
break;
|
||||
case 'testing-l': $url = 'http://steno_replay:3000';
|
||||
case 'replay': $url = 'http://steno_replay:3000';
|
||||
break;
|
||||
default:
|
||||
$url = 'https://slack.com';
|
||||
}
|
||||
|
||||
$url .= '/api/'.$method;
|
||||
|
||||
// If we dont have a scope definition, or if the scope definition is not in the token
|
||||
if (is_null($x=Arr::get(self::scopes,$method)) OR (($x !== '') AND ! $this->_token->hasScope($x))) {
|
||||
throw new SlackTokenScopeException(sprintf('Token [%d:%s] doesnt have the required scope: [%s] for [%s]',$this->_token->id,$this->_token->token_hidden,serialize($x),$method));
|
||||
}
|
||||
|
||||
// If we are passed an array, we'll do a normal post.
|
||||
if (is_array($parameters)) {
|
||||
$parameters['token'] = $this->_token->token;
|
||||
$request = $this->prepareRequest(
|
||||
$url,
|
||||
$parameters
|
||||
);
|
||||
$http = Http::baseUrl($url);
|
||||
$http
|
||||
->withToken($this->_token->token)
|
||||
->acceptJson();
|
||||
|
||||
// If we are json, then we'll do an application/json post
|
||||
} elseif (is_json($parameters)) {
|
||||
$request = $this->prepareRequest(
|
||||
$url,
|
||||
$parameters,
|
||||
[
|
||||
'Content-Type: application/json; charset=utf-8',
|
||||
'Content-Length: '.strlen($parameters),
|
||||
'Authorization: Bearer '.$this->_token->token,
|
||||
]
|
||||
);
|
||||
if ($asForm) {
|
||||
if (! is_array($parameters))
|
||||
throw new SlackException('Parameters are not an array for a form submission');
|
||||
|
||||
} else {
|
||||
throw new \Exception('Parameters unknown');
|
||||
$http->asForm();
|
||||
|
||||
} elseif ($parameters) {
|
||||
$http->withBody((is_array($parameters) || ($parameters instanceof BlockKit)) ? json_encode($parameters) : $parameters,'application/json');
|
||||
}
|
||||
|
||||
try {
|
||||
$response = curl_exec($request);
|
||||
if (! $response)
|
||||
throw new \Exception('CURL exec returned an empty response: '.serialize(curl_getinfo($request)));
|
||||
|
||||
$result = json_decode($response);
|
||||
$request = $http->post(sprintf('/api/%s',$method),$asForm ? $parameters : NULL)->throw();
|
||||
$response = $request->object();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]);
|
||||
@ -431,73 +418,49 @@ final class API
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
|
||||
if (! $result) {
|
||||
Log::error(sprintf('%s:Our result shouldnt be empty',static::LOGKEY),['m'=>__METHOD__,'r'=>$request,'R'=>$response]);
|
||||
throw new SlackException('Slack Result is Empty');
|
||||
}
|
||||
|
||||
if (! $result->ok) {
|
||||
switch ($result->error) {
|
||||
if ($response->ok)
|
||||
return $response;
|
||||
else
|
||||
switch ($response->error) {
|
||||
case 'already_pinned':
|
||||
throw new SlackAlreadyPinnedException('Already Pinned',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
|
||||
case 'not_authed':
|
||||
throw new SlackNoAuthException('No Auth Token',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
throw new SlackAlreadyPinnedException('Already Pinned',$request->status());
|
||||
|
||||
case 'channel_not_found':
|
||||
throw new SlackChannelNotFoundException('Channel Not Found',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
throw new SlackChannelNotFoundException('Channel Not Found',$request->status());
|
||||
|
||||
case 'hash_conflict':
|
||||
if (App::environment() == 'dev')
|
||||
file_put_contents('/tmp/hash_conflict.'.$method,print_r(json_decode(json_decode($parameters)->view),TRUE));
|
||||
if (App::environment() == 'local')
|
||||
file_put_contents('/tmp/hash_conflict.'.$method,print_r($response,TRUE));
|
||||
|
||||
throw new SlackHashConflictException('Hash Conflict',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
throw new SlackHashConflictException('Hash Conflict',$request->status());
|
||||
|
||||
case 'invalid_auth':
|
||||
throw new SlackNoAuthException('Invalid Auth Token',$request->status());
|
||||
|
||||
case 'message_not_found':
|
||||
throw new SlackMessageNotFoundException('Message Not Found',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
throw new SlackMessageNotFoundException('Message Not Found',$request->status());
|
||||
|
||||
case 'no_pin':
|
||||
throw new SlackNoPinException('No Pin',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
throw new SlackNoPinException('No Pin',$request->status());
|
||||
|
||||
case 'not_in_channel':
|
||||
throw new SlackNotInChannelException('Not In Channel',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
case 'not_authed':
|
||||
throw new SlackNoAuthException('No Auth Token',$request->status());
|
||||
|
||||
case 'not_found':
|
||||
file_put_contents('/tmp/method.'.$method,print_r(['request'=>is_json($parameters) ? json_decode($parameters,TRUE) : $parameters,'response'=>$result],TRUE));
|
||||
throw new SlackNotFoundException('Not Found',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
file_put_contents('/tmp/method.'.$method,print_r(['request'=>is_json($parameters) ? json_decode($parameters,TRUE) : $parameters,'response'=>$response],TRUE));
|
||||
throw new SlackNotFoundException('Not Found',$request->status());
|
||||
|
||||
case 'not_in_channel':
|
||||
throw new SlackNotInChannelException('Not In Channel',$request->status());
|
||||
|
||||
case 'thread_not_found':
|
||||
throw new SlackThreadNotFoundException('Thread Not Found',curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
throw new SlackThreadNotFoundException('Thread Not Found',$request->status());
|
||||
|
||||
case 'account_inactive':
|
||||
// @todo Mark the token/team as inactive
|
||||
default:
|
||||
Log::error(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'t'=>$this->_token->team_id,'r'=>$result]);
|
||||
throw new SlackException($result->error,curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
Log::error(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'t'=>$this->_token->team_id,'r'=>$response]);
|
||||
throw new SlackException($response->error,$request->status());
|
||||
}
|
||||
}
|
||||
|
||||
curl_close($request);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the API call
|
||||
*
|
||||
* @param $url
|
||||
* @param string $parameters
|
||||
* @param array $headers
|
||||
* @return resource
|
||||
*/
|
||||
private function prepareRequest($url,$parameters='',$headers = [])
|
||||
{
|
||||
$request = curl_init();
|
||||
|
||||
curl_setopt($request,CURLOPT_URL,$url);
|
||||
curl_setopt($request,CURLOPT_RETURNTRANSFER,TRUE);
|
||||
curl_setopt($request,CURLOPT_HTTPHEADER,$headers);
|
||||
curl_setopt($request,CURLINFO_HEADER_OUT,TRUE);
|
||||
curl_setopt($request,CURLOPT_SSL_VERIFYPEER,FALSE);
|
||||
curl_setopt($request,CURLOPT_POSTFIELDS,$parameters);
|
||||
|
||||
return $request;
|
||||
}
|
||||
}
|
41
src/Base.php
41
src/Base.php
@ -44,6 +44,7 @@ abstract class Base
|
||||
*
|
||||
* @param bool $create
|
||||
* @return Channel|null
|
||||
* @todo Enable simulating an existing channel, using FALSE (dont create), 0 (create dont save), 1 (create and save)
|
||||
*/
|
||||
final public function channel(bool $create=FALSE): ?Channel
|
||||
{
|
||||
@ -63,6 +64,44 @@ abstract class Base
|
||||
return $o->exists ? $o : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate out a callback command to the id that the command relates to
|
||||
*
|
||||
* @param string $key
|
||||
* @param string|null $item
|
||||
* @return string|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
final protected function keyitem(string $key,string $item=NULL): ?string
|
||||
{
|
||||
if (! $item)
|
||||
return $item;
|
||||
|
||||
$regex = '/^([a-z_]+)\|([0-9]+)$/';
|
||||
$id = NULL;
|
||||
$value = NULL;
|
||||
|
||||
if (preg_match($regex,$item)) {
|
||||
$id = preg_replace($regex,'$1',$item);
|
||||
$value = preg_replace($regex,'$2',$item);
|
||||
}
|
||||
|
||||
switch ($key) {
|
||||
case 'id':
|
||||
return $id ?: $item;
|
||||
case 'value':
|
||||
return $value;
|
||||
|
||||
default:
|
||||
throw new \Exception('Unknown key: '.$key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Eneterprise object that a Response is related to
|
||||
*
|
||||
* @return Enterprise
|
||||
*/
|
||||
final public function enterprise(): Enterprise
|
||||
{
|
||||
$class = class_exists(AppEnterprise::class) ? AppEnterprise::class : Enterprise::class;
|
||||
@ -113,7 +152,7 @@ abstract class Base
|
||||
if (! $o->exists) {
|
||||
$o->team_id = $this->enterprise_id ? NULL : $this->team()->id;
|
||||
$o->enterprise_id = ($x=$this->enterprise())->exists ? $x->id : NULL;
|
||||
$o->active = TRUE;
|
||||
$o->active = FALSE;
|
||||
$o->save();
|
||||
|
||||
Log::debug(sprintf('%s: User Created in DB [%s] (%s)',self::LOGKEY,$this->user_id,$o->id));
|
||||
|
@ -5,12 +5,24 @@ namespace Slack;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
/**
|
||||
* Class BlockKit - Slack Blockit Objects
|
||||
*
|
||||
* @notes
|
||||
* + callback_id is used to identify the source of any action (modal). (Message blocks do not have a callback_id, accept in legacy attachments).
|
||||
* eg: hometab, add_product, ask_modal
|
||||
* + block_id is used to identify the sub action(s) of any action (modal). (Messages with blocks can have a block_id, we need to use this to determine what to do.)
|
||||
* eg: multiple blocks (list of something)
|
||||
* + action_id is used to identify the action that was initiated
|
||||
* eg: view, edit, review
|
||||
* + value is the value of the action_id
|
||||
* eg: 5 (question #), yes, no, skip, abort
|
||||
*
|
||||
* @package Slack
|
||||
*/
|
||||
abstract class BlockKit implements \JsonSerializable
|
||||
abstract class BlockKit implements \JsonSerializable,\Countable
|
||||
{
|
||||
protected Collection $_data;
|
||||
|
||||
@ -40,7 +52,7 @@ abstract class BlockKit implements \JsonSerializable
|
||||
protected function validate(string $key,$value)
|
||||
{
|
||||
if (Arr::get(static::LIMITS,$key) && (strlen($value) > static::LIMITS[$key]))
|
||||
throw new Exception(sprintf('%s must be %d chars or less for buttons %s',$key,self::LIMITS[$key],get_class($this)));
|
||||
throw new SlackSyntaxException(sprintf('%s must be %d chars or less for buttons %s',$key,static::LIMITS[$key],get_class($this)));
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
@ -2,26 +2,26 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Accessories;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\Blocks\Elements\{Confirm,Text};
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Overflow extends Element
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'action_id' => 255,
|
||||
];
|
||||
|
||||
private const MIN_OPTIONS = 1;
|
||||
private const MAX_OPTIONS = 5;
|
||||
public const MIN_OPTIONS = 1;
|
||||
public const MAX_OPTIONS = 5;
|
||||
|
||||
/**
|
||||
* @param string $action_id
|
||||
* @param Collection $options
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
* @todo We dont handle option_groups yet.
|
||||
*/
|
||||
public function __construct(string $action_id,Collection $options)
|
||||
@ -34,10 +34,10 @@ final class Overflow extends Element
|
||||
$this->action_id = $this->validate('action_id',$action_id);
|
||||
|
||||
if (count($options) < self::MIN_OPTIONS)
|
||||
throw new Exception(sprintf('Must have atleast %d options',self::MIN_OPTIONS));
|
||||
throw new SlackSyntaxException(sprintf('Must have atleast %d options',self::MIN_OPTIONS));
|
||||
|
||||
if (count($options) > self::MAX_OPTIONS)
|
||||
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
|
||||
$this->options = $options->transform(function($item) {
|
||||
return ['text'=>Text::item(Arr::get($item,'name'),'plain_text'),'value'=>(string)Arr::get($item,'id')];
|
||||
|
@ -2,21 +2,21 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\Blocks;
|
||||
use Slack\Blockkit\Blocks\Elements\{Button,MultiStaticSelect};
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Actions extends Blocks
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'block_id' => 255, // @todo Should be unique for each message
|
||||
];
|
||||
|
||||
private const MAX_ELEMENTS = 5;
|
||||
public const MAX_ELEMENTS = 5;
|
||||
|
||||
private const VALID_ELEMENTS = [
|
||||
public const VALID_ELEMENTS = [
|
||||
Button::class,
|
||||
MultiStaticSelect::class,
|
||||
Blocks\Accessories\Overflow::class,
|
||||
@ -38,7 +38,7 @@ final class Actions extends Blocks
|
||||
public function jsonSerialize()
|
||||
{
|
||||
if (! $this->elements)
|
||||
throw new Exception('Must define at least 1 element');
|
||||
throw new SlackSyntaxException('Must define at least 1 element');
|
||||
|
||||
return parent::jsonSerialize();
|
||||
}
|
||||
@ -55,7 +55,7 @@ final class Actions extends Blocks
|
||||
public function elements(Collection $collection): self
|
||||
{
|
||||
if (count($collection) > self::MAX_ELEMENTS)
|
||||
throw new Exception(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS));
|
||||
|
||||
// @todo Check that a valid element is added. https://api.slack.com/reference/block-kit/blocks#actions
|
||||
|
||||
|
@ -2,22 +2,22 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\Blocks;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Context extends Blocks
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'block_id' => 255, // @todo Should be unique for each message
|
||||
];
|
||||
|
||||
private const MAX_ELEMENTS = 10;
|
||||
public const MAX_ELEMENTS = 10;
|
||||
|
||||
/**
|
||||
* @param Collection $collection
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
* @todo Collection can only be image or text elements
|
||||
*/
|
||||
public function __construct(Collection $collection)
|
||||
@ -28,7 +28,7 @@ final class Context extends Blocks
|
||||
$this->type = 'context';
|
||||
|
||||
if (count($collection) > self::MAX_ELEMENTS)
|
||||
throw new Exception(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d elements',self::MAX_ELEMENTS));
|
||||
|
||||
$this->elements = $collection;
|
||||
}
|
||||
|
@ -2,9 +2,8 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Button extends Element
|
||||
{
|
||||
@ -16,26 +15,19 @@ final class Button extends Element
|
||||
'value' => 2000,
|
||||
];
|
||||
|
||||
public function __construct(Text $text,string $value,string $action_id)
|
||||
public function __construct(string $text,string $value,string $action_id)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Defaults
|
||||
$this->type = 'button';
|
||||
|
||||
if ($text->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$text->type));
|
||||
|
||||
if (strlen($text->text) > self::LIMITS['text'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
|
||||
$this->text = $text;
|
||||
|
||||
$this->text = Text::item($this->validate('text',$text),'plain_text');
|
||||
$this->value = $this->validate('value',$value);
|
||||
$this->action_id = $this->validate('action_id',$action_id);
|
||||
}
|
||||
|
||||
public static function item(Text $text,string $value,string $action_id): self
|
||||
public static function item(string $text,string $value,string $action_id): self
|
||||
{
|
||||
return new self($text,$value,$action_id);
|
||||
}
|
||||
@ -52,7 +44,7 @@ final class Button extends Element
|
||||
public function style(string $string): self
|
||||
{
|
||||
if (! in_array($string,['default','primary','danger']))
|
||||
throw new Exception(sprintf('Unknown style %s',$string));
|
||||
throw new SlackSyntaxException(sprintf('Unknown style %s',$string));
|
||||
|
||||
$this->style = $string;
|
||||
|
||||
|
@ -3,18 +3,39 @@
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Confirm extends Element
|
||||
{
|
||||
public function __construct()
|
||||
protected const LIMITS = [
|
||||
'title' => 100,
|
||||
'text' => 300,
|
||||
'confirm' => 30,
|
||||
'deny' => 30,
|
||||
];
|
||||
|
||||
public function __construct(string $title,Text $text,string $confirm,string $deny)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
abort(500,'Not Implememted');
|
||||
$this->title = Text::item($this->validate('title',$title),'plain_text');
|
||||
$this->text = $this->validate('text',$text->text) ? $text : NULL;
|
||||
$this->confirm = Text::item($this->validate('confirm',$confirm),'plain_text');
|
||||
$this->deny = Text::item($this->validate('deny',$deny),'plain_text');
|
||||
}
|
||||
|
||||
public static function item(): self
|
||||
public static function item(string $title,Text $text,string $confirm,string $deny): self
|
||||
{
|
||||
return new self();
|
||||
return new self($title,$text,$confirm,$deny);
|
||||
}
|
||||
|
||||
public function style(string $string): self
|
||||
{
|
||||
if (! in_array($string,['default','primary','danger']))
|
||||
throw new SlackSyntaxException(sprintf('Unknown style %s',$string));
|
||||
|
||||
$this->style = $string;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -2,9 +2,8 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class ExternalSelect extends Element
|
||||
{
|
||||
@ -14,29 +13,22 @@ final class ExternalSelect extends Element
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Text $placeholder
|
||||
* @param string $placeholder
|
||||
* @param string $action_id
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function __construct(Text $placeholder,string $action_id)
|
||||
public function __construct(string $placeholder,string $action_id)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Defaults
|
||||
$this->type = 'external_select';
|
||||
|
||||
if ($placeholder->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type));
|
||||
|
||||
if (strlen($placeholder->text) > self::LIMITS['placeholder'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
|
||||
|
||||
$this->placeholder = $placeholder;
|
||||
|
||||
$this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text');
|
||||
$this->action_id = $this->validate('action_id',$action_id);
|
||||
}
|
||||
|
||||
public static function item(Text $placeholder,string $action_id): self
|
||||
public static function item(string $placeholder,string $action_id): self
|
||||
{
|
||||
return new self($placeholder,$action_id);
|
||||
}
|
||||
@ -68,7 +60,7 @@ final class ExternalSelect extends Element
|
||||
public function min_query_length(int $int): self
|
||||
{
|
||||
if ($int < 1)
|
||||
throw new Exception('Minimum 1 options must be configured');
|
||||
throw new SlackSyntaxException('Minimum 1 options must be configured');
|
||||
|
||||
$this->min_query_length = $int;
|
||||
|
||||
|
@ -2,47 +2,39 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class MultiExternalSelect extends Element
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'action_id' => 255,
|
||||
'placeholder' => 150,
|
||||
];
|
||||
|
||||
private const MAX_OPTIONS = 100;
|
||||
public const MAX_OPTIONS = 100;
|
||||
|
||||
// @todo option_group? (double check it is applicable to this item)
|
||||
|
||||
/**
|
||||
* @param Text $placeholder
|
||||
* @param string $placeholder
|
||||
* @param string $action_id
|
||||
* @throws Exception
|
||||
* @todo We dont handle option_groups yet.
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function __construct(Text $placeholder,string $action_id)
|
||||
public function __construct(string $placeholder,string $action_id)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Defaults
|
||||
$this->type = 'multi_external_select';
|
||||
|
||||
if ($placeholder->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type));
|
||||
|
||||
if (strlen($placeholder->text) > self::LIMITS['placeholder'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
|
||||
|
||||
$this->placeholder = $placeholder;
|
||||
|
||||
$this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text');
|
||||
$this->action_id = $this->validate('action_id',$action_id);
|
||||
}
|
||||
|
||||
public static function item(Text $placeholder,string $action_id): self
|
||||
public static function item(string $placeholder,string $action_id): self
|
||||
{
|
||||
return new self($placeholder,$action_id);
|
||||
}
|
||||
@ -74,7 +66,7 @@ final class MultiExternalSelect extends Element
|
||||
public function min_query_length(int $int): self
|
||||
{
|
||||
if ($int < 1)
|
||||
throw new Exception('Minimum 1 options must be configured');
|
||||
throw new SlackSyntaxException('Minimum 1 options must be configured');
|
||||
|
||||
$this->min_query_length = $int;
|
||||
|
||||
@ -84,7 +76,7 @@ final class MultiExternalSelect extends Element
|
||||
public function max_selected_items(int $int): self
|
||||
{
|
||||
if ($int < 1)
|
||||
throw new Exception('Minimum 1 options must be configured');
|
||||
throw new SlackSyntaxException('Minimum 1 options must be configured');
|
||||
|
||||
$this->max_selected_items = $int;
|
||||
|
||||
|
@ -2,58 +2,51 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class MultiStaticSelect extends Element
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'action_id' => 255,
|
||||
'placeholder' => 150,
|
||||
];
|
||||
|
||||
private const MAX_OPTIONS = 100;
|
||||
public const MAX_OPTIONS = 100;
|
||||
|
||||
// @todo option_group? (double check it is applicable to this item)
|
||||
|
||||
/**
|
||||
* @param Text $placeholder
|
||||
* @param string $placeholder
|
||||
* @param string $action_id
|
||||
* @param Collection $options
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
* @todo We dont handle option_groups yet.
|
||||
*/
|
||||
public function __construct(Text $placeholder,string $action_id,Collection $options)
|
||||
public function __construct(string $placeholder,string $action_id,Collection $options)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Defaults
|
||||
$this->type = 'multi_static_select';
|
||||
|
||||
if ($placeholder->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type));
|
||||
|
||||
if (strlen($placeholder->text) > self::LIMITS['placeholder'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
|
||||
|
||||
$this->placeholder = $placeholder;
|
||||
|
||||
$this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text');
|
||||
$this->action_id = $this->validate('action_id',$action_id);
|
||||
|
||||
if (! $options->count())
|
||||
throw new Exception('There are no options?');
|
||||
throw new SlackSyntaxException('There are no options?');
|
||||
|
||||
if ($options->count() > self::MAX_OPTIONS)
|
||||
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
|
||||
$this->options = $options->transform(function($item) {
|
||||
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value];
|
||||
});
|
||||
}
|
||||
|
||||
public static function item(Text $placeholder,string $action_id,Collection $options): self
|
||||
public static function item(string $placeholder,string $action_id,Collection $options): self
|
||||
{
|
||||
return new self($placeholder,$action_id,$options);
|
||||
}
|
||||
@ -74,17 +67,17 @@ final class MultiStaticSelect extends Element
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function initial_options(array $initial=NULL): self
|
||||
public function initial_options(Collection $initial=NULL): self
|
||||
{
|
||||
// No initial options.
|
||||
if (count($initial)) {
|
||||
if (! $this->options)
|
||||
throw new Exception('Cannot set an initial value without options defined first');
|
||||
throw new SlackSyntaxException('Cannot set an initial value without options defined first');
|
||||
|
||||
if (count($initial) > self::MAX_OPTIONS)
|
||||
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
|
||||
$this->initial_options = $this->options->filter(function($item) use ($initial) { return in_array($item['value'],$initial); });
|
||||
$this->initial_options = $this->options->filter(function($item) use ($initial) { return $initial->contains($item['value']); });
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -93,7 +86,7 @@ final class MultiStaticSelect extends Element
|
||||
public function max_selected_items(int $int): self
|
||||
{
|
||||
if ($int < 1)
|
||||
throw new Exception('Minimum 1 options must be configured');
|
||||
throw new SlackSyntaxException('Minimum 1 options must be configured');
|
||||
|
||||
$this->max_selected_items = $int;
|
||||
|
||||
|
@ -2,9 +2,8 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
/**
|
||||
* @note Overflow, select, and multi-select menus can only use plain_text objects,
|
||||
@ -24,7 +23,7 @@ final class Options extends Element
|
||||
parent::__construct();
|
||||
|
||||
if (strlen($text->text) > self::LIMITS['text'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
|
||||
$this->text = $text;
|
||||
$this->value = $this->validate('value',$value);
|
||||
@ -37,15 +36,9 @@ final class Options extends Element
|
||||
|
||||
/* OPTIONAL ITEMS */
|
||||
|
||||
public function description(Text $text): self
|
||||
public function description(string $text): self
|
||||
{
|
||||
if ($text->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$text->type));
|
||||
|
||||
if (strlen($text->text) > self::LIMITS['description'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['description']));
|
||||
|
||||
$this->description = $text;
|
||||
$this->description = Text::item($this->validate('description',$text),'plain_text');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -3,18 +3,19 @@
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
/**
|
||||
* This is an element of an input dialog
|
||||
*/
|
||||
final class PlainTextInput extends Element
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'action_id' => 255, // @todo Should be unique for each message
|
||||
'placeholder' => 150,
|
||||
];
|
||||
|
||||
private const MAX_MIN_LENGTH = 3000;
|
||||
public const MAX_MIN_LENGTH = 3000;
|
||||
|
||||
// @todo dispatch_action_config
|
||||
// @todo focus_on_load
|
||||
@ -45,7 +46,7 @@ final class PlainTextInput extends Element
|
||||
public function min_length(int $int): self
|
||||
{
|
||||
if ($int > self::MAX_MIN_LENGTH)
|
||||
throw new Exception(sprintf('min_length must be less than %d',self::MAX_MIN_LENGTH));
|
||||
throw new SlackSyntaxException(sprintf('min_length must be less than %d',self::MAX_MIN_LENGTH));
|
||||
|
||||
$this->min_length = $int;
|
||||
|
||||
@ -55,7 +56,7 @@ final class PlainTextInput extends Element
|
||||
public function max_length(int $int): self
|
||||
{
|
||||
if ($this->min_length && ($int < $this->min_length))
|
||||
throw new Exception('max_length must be greater than min_length');
|
||||
throw new SlackSyntaxException('max_length must be greater than min_length');
|
||||
|
||||
$this->max_length = $int;
|
||||
|
||||
@ -72,7 +73,7 @@ final class PlainTextInput extends Element
|
||||
public function placeholder(Text $text): self
|
||||
{
|
||||
if (strlen($text->text) > self::LIMITS['placeholder'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
|
||||
throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
|
||||
|
||||
$this->placeholder = $text;
|
||||
|
||||
|
@ -2,58 +2,51 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class StaticSelect extends Element
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'action_id' => 255,
|
||||
'placeholder' => 150,
|
||||
];
|
||||
|
||||
private const MAX_OPTIONS = 100;
|
||||
public const MAX_OPTIONS = 100;
|
||||
|
||||
// @todo option_group
|
||||
|
||||
/**
|
||||
* @param Text $placeholder
|
||||
* @param string $placeholder
|
||||
* @param string $action_id
|
||||
* @param Collection $options
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
* @todo We dont handle option_groups yet.
|
||||
*/
|
||||
public function __construct(Text $placeholder,string $action_id,Collection $options)
|
||||
public function __construct(string $placeholder,string $action_id,Collection $options)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Defaults
|
||||
$this->type = 'static_select';
|
||||
|
||||
if ($placeholder->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$placeholder->type));
|
||||
|
||||
if (strlen($placeholder->text) > self::LIMITS['placeholder'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['placeholder']));
|
||||
|
||||
$this->placeholder = $placeholder;
|
||||
|
||||
$this->placeholder = Text::item($this->validate('placeholder',$placeholder),'plain_text');
|
||||
$this->action_id = $this->validate('action_id',$action_id);
|
||||
|
||||
if (! $options->count())
|
||||
throw new Exception('There are no options?');
|
||||
throw new SlackSyntaxException('There are no options?');
|
||||
|
||||
if ($options->count() > self::MAX_OPTIONS)
|
||||
throw new Exception(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d options',self::MAX_OPTIONS));
|
||||
|
||||
$this->options = $options->transform(function($item) {
|
||||
return ['text'=>Text::item($item->name,'plain_text'),'value'=>(string)$item->value];
|
||||
});
|
||||
}
|
||||
|
||||
public static function item(Text $placeholder,string $action_id,Collection $options): self
|
||||
public static function item(string $placeholder,string $action_id,Collection $options): self
|
||||
{
|
||||
return new self($placeholder,$action_id,$options);
|
||||
}
|
||||
@ -77,7 +70,7 @@ final class StaticSelect extends Element
|
||||
public function initial_option(string $string=NULL): self
|
||||
{
|
||||
if (! $this->options)
|
||||
throw new Exception('Cannot set an initial value without options defined first');
|
||||
throw new SlackSyntaxException('Cannot set an initial value without options defined first');
|
||||
|
||||
if ($string)
|
||||
$this->initial_option = $this->options->first(function($item) use ($string) { return $item['value'] == $string; });
|
||||
|
@ -2,23 +2,22 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks\Elements;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\Blockkit\Element;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Text extends Element
|
||||
{
|
||||
private const TYPES = ['mrkdwn','plain_text'];
|
||||
public const TYPES = ['mrkdwn','plain_text'];
|
||||
|
||||
public function __construct(string $text,string $type)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if (! in_array($type,self::TYPES))
|
||||
throw new Exception(sprintf('Type [%s] not valid',$type));
|
||||
throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type));
|
||||
|
||||
$this->type = $type;
|
||||
$this->text = $text;
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public static function item(string $text,string $type='mrkdwn'): self
|
||||
@ -30,8 +29,8 @@ final class Text extends Element
|
||||
|
||||
public function emoji(bool $bool): self
|
||||
{
|
||||
if ($x=$this->type != 'plain_text')
|
||||
throw new Exception(sprintf('Cannnot use emoji when type is [%s]',$x));
|
||||
if ($this->type != 'plain_text')
|
||||
throw new SlackSyntaxException(sprintf('Cannnot use emoji when type is [%s]',$this->type));
|
||||
|
||||
$this->emoji = $bool;
|
||||
|
||||
|
@ -2,10 +2,9 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\Blockkit\Blocks;
|
||||
use Slack\Blockkit\Blocks\Elements\Text;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Header extends Blocks
|
||||
{
|
||||
@ -15,26 +14,20 @@ final class Header extends Blocks
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Text $text
|
||||
* @throws Exception
|
||||
* @param string $text
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function __construct(Text $text)
|
||||
public function __construct(string $text)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Defaults
|
||||
$this->type = 'header';
|
||||
|
||||
if ($text->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$text->type));
|
||||
|
||||
if (strlen($text->text) > self::LIMITS['text'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
|
||||
$this->text = $text;
|
||||
$this->text = Text::item($this->validate('text',$text),'plain_text');
|
||||
}
|
||||
|
||||
public static function item(Text $text): self
|
||||
public static function item(string $text): self
|
||||
{
|
||||
return new self($text);
|
||||
}
|
||||
|
@ -2,19 +2,18 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\Blockkit\{Blocks,Element};
|
||||
use Slack\Blockkit\{Blocks,Blocks\Elements\Text,Element};
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Input extends Blocks
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'block_id' => 255, // @todo Should be unique for each message
|
||||
'hint' => 2000,
|
||||
'label' => 2000,
|
||||
];
|
||||
|
||||
private const VALID_ELEMENTS = [
|
||||
public const VALID_ELEMENTS = [
|
||||
Elements\PlainTextInput::class,
|
||||
Elements\MultiStaticSelect::class
|
||||
];
|
||||
@ -22,26 +21,20 @@ final class Input extends Blocks
|
||||
// @todo dispatch_action
|
||||
|
||||
/**
|
||||
* @param Elements\Text|null $label
|
||||
* @throws Exception
|
||||
* @param string|null $label
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function __construct(Elements\Text $label=NULL)
|
||||
public function __construct(string $label=NULL)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if ($label->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$label->type));
|
||||
|
||||
// Defaults
|
||||
$this->type = 'input';
|
||||
|
||||
if (strlen($label->text) > self::LIMITS['label'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['label']));
|
||||
|
||||
$this->label = $label;
|
||||
$this->label = Text::item($this->validate('label',$label),'plain_text');
|
||||
}
|
||||
|
||||
public static function item(Elements\Text $label=NULL): self
|
||||
public static function item(string $label=NULL): self
|
||||
{
|
||||
return new self($label);
|
||||
}
|
||||
@ -49,7 +42,7 @@ final class Input extends Blocks
|
||||
public function jsonSerialize()
|
||||
{
|
||||
if (! $this->element)
|
||||
throw new Exception('Must define an element');
|
||||
throw new SlackSyntaxException('Must define an element');
|
||||
|
||||
return parent::jsonSerialize();
|
||||
}
|
||||
@ -66,7 +59,7 @@ final class Input extends Blocks
|
||||
public function element(Element $object): self
|
||||
{
|
||||
if (! in_array(get_class($object),self::VALID_ELEMENTS))
|
||||
throw new Exception(sprintf('Invalid element [%s] added to input',get_class($object)));
|
||||
throw new SlackSyntaxException(sprintf('Invalid element [%s] added to input',get_class($object)));
|
||||
|
||||
$this->element = $object;
|
||||
|
||||
@ -76,7 +69,7 @@ final class Input extends Blocks
|
||||
public function hint(Elements\Text $text): self
|
||||
{
|
||||
if (strlen($text->text) > self::LIMITS['hint'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['hint']));
|
||||
throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['hint']));
|
||||
|
||||
$this->hint = $text;
|
||||
|
||||
|
@ -2,25 +2,25 @@
|
||||
|
||||
namespace Slack\Blockkit\Blocks;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Blockkit\{Blocks,Element};
|
||||
use Slack\BLockKit\Blocks\Elements\Text;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
final class Section extends Blocks
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'block_id' => 255, // @todo Should be unique for each message
|
||||
'text' => 3000,
|
||||
];
|
||||
|
||||
private const MAX_FIELDS = 10;
|
||||
private const MAX_FIELDS_TEXT = 2000;
|
||||
public const MAX_FIELDS = 10;
|
||||
public const MAX_FIELDS_TEXT = 2000;
|
||||
|
||||
/**
|
||||
* @param Text|NULL $text not required if fields is provided
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function __construct(Text $text=NULL)
|
||||
{
|
||||
@ -31,7 +31,7 @@ final class Section extends Blocks
|
||||
|
||||
if ($text) {
|
||||
if (strlen($text->text) > self::LIMITS['text'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
throw new SlackSyntaxException(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
|
||||
$this->text = $text;
|
||||
}
|
||||
@ -45,7 +45,7 @@ final class Section extends Blocks
|
||||
public function jsonSerialize()
|
||||
{
|
||||
if (! $this->text && ! $this->fields && ! $this->accessory)
|
||||
throw new Exception('Must define text, accessory or fields');
|
||||
throw new SlackSyntaxException('Must define text, accessory or fields');
|
||||
|
||||
return parent::jsonSerialize();
|
||||
}
|
||||
@ -69,10 +69,10 @@ final class Section extends Blocks
|
||||
public function fields(Collection $collection): self
|
||||
{
|
||||
if (count($collection) > self::MAX_FIELDS)
|
||||
throw new Exception(sprintf('Can only have maximum %d fields',self::MAX_FIELDS));
|
||||
throw new SlackSyntaxException(sprintf('Can only have maximum %d fields',self::MAX_FIELDS));
|
||||
|
||||
if (($x=$collection->map(function($item) { return strlen($item->text); })->max()) > self::MAX_FIELDS_TEXT)
|
||||
throw new Exception(sprintf('The maximum size of the text in a field is %d (%d)',self::MAX_FIELDS_TEXT,$x));
|
||||
throw new SlackSyntaxException(sprintf('The maximum size of the text in a field is %d (%d)',self::MAX_FIELDS_TEXT,$x));
|
||||
|
||||
$this->fields = $collection;
|
||||
|
||||
|
@ -2,52 +2,44 @@
|
||||
|
||||
namespace Slack\Blockkit;
|
||||
|
||||
use \Exception;
|
||||
|
||||
use Slack\BlockKit;
|
||||
use Slack\Blockkit\Blocks\Divider;
|
||||
use Slack\Blockkit\Blocks\Elements\Text;
|
||||
use Slack\Blockkit\Blocks\Section;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
/**
|
||||
* This class creates a slack Modal Response
|
||||
*/
|
||||
final class Modal extends BlockKit
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'callback_id' => 255,
|
||||
'close' => 24,
|
||||
'private_metadata' => 3000,
|
||||
'submit' => 24,
|
||||
'text' => 24,
|
||||
'title' => 24,
|
||||
];
|
||||
|
||||
private const MAX_BLOCKS = 100;
|
||||
public const MAX_BLOCKS = 100;
|
||||
|
||||
private $action = NULL;
|
||||
|
||||
public function __construct(Text $title=NULL,string $type='modal')
|
||||
public function __construct(string $title=NULL,string $type='modal')
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if (! in_array($type,['modal','home']))
|
||||
throw new Exception(sprintf('Unknown type %s',$type));
|
||||
|
||||
if ($type != 'modal' && $title)
|
||||
throw new Exception(sprintf('Titles are not required for %s',$type));
|
||||
throw new SlackSyntaxException(sprintf('Unknown type %s',$type));
|
||||
|
||||
if ($title) {
|
||||
if ($title->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$title->type));
|
||||
if ($type != 'modal')
|
||||
throw new SlackSyntaxException(sprintf('Titles are not required for %s',$type));
|
||||
|
||||
if (strlen($title->text) > self::LIMITS['text'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['text']));
|
||||
|
||||
$this->title = $title;
|
||||
$this->title = Text::item($this->validate('title',$title),'plain_text');
|
||||
}
|
||||
|
||||
$this->type = $type;
|
||||
|
||||
$this->blocks = collect();
|
||||
}
|
||||
|
||||
@ -56,14 +48,14 @@ final class Modal extends BlockKit
|
||||
*
|
||||
* @param Blocks $block
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function addBlock(Blocks $block): self
|
||||
{
|
||||
$this->blocks->push($block);
|
||||
|
||||
if ($x=$this->blocks->count() > self::MAX_BLOCKS)
|
||||
throw new Exception(sprintf('Modal can only have %d blocks',self::MAX_BLOCKS));
|
||||
if ($this->blocks->count() > self::MAX_BLOCKS)
|
||||
throw new SlackSyntaxException(sprintf('Modal can only have %d blocks',self::MAX_BLOCKS));
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -90,17 +82,17 @@ final class Modal extends BlockKit
|
||||
public function action(string $string)
|
||||
{
|
||||
if (! in_array($string,['clear','update']))
|
||||
throw new Exception(sprintf('Unknown action %s',$string));
|
||||
throw new SlackSyntaxException(sprintf('Unknown action %s',$string));
|
||||
|
||||
$this->action = $string;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function clear_on_close(bool $bool): self
|
||||
public function clear_on_close(bool $bool=TRUE): self
|
||||
{
|
||||
if ($this->type != 'modal')
|
||||
throw new Exception(sprintf('clear_on_close is not required for %s',$type));
|
||||
throw new SlackSyntaxException(sprintf('clear_on_close is not required for %s',$this->type));
|
||||
|
||||
$this->clear_on_close = $bool;
|
||||
|
||||
@ -114,18 +106,12 @@ final class Modal extends BlockKit
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function close(Text $text): self
|
||||
public function close(string $text): self
|
||||
{
|
||||
if ($this->type != 'modal')
|
||||
throw new Exception(sprintf('Close is not required for %s',$type));
|
||||
throw new SlackSyntaxException(sprintf('Close is not required for %s',$this->type));
|
||||
|
||||
if ($text->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$text->type));
|
||||
|
||||
if (strlen($text->text) > self::LIMITS['close'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['close']));
|
||||
|
||||
$this->close = $text;
|
||||
$this->close = Text::item($this->validate('close',$text),'plain_text');
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -150,10 +136,10 @@ final class Modal extends BlockKit
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function notify_on_close(bool $bool): self
|
||||
public function notify_on_close(bool $bool=TRUE): self
|
||||
{
|
||||
if ($this->type != 'modal')
|
||||
throw new Exception(sprintf('notify_on_close is not required for %s',$type));
|
||||
throw new SlackSyntaxException(sprintf('notify_on_close is not required for %s',$this->type));
|
||||
|
||||
$this->notify_on_close = $bool;
|
||||
|
||||
@ -175,18 +161,12 @@ final class Modal extends BlockKit
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function submit(Text $text): self
|
||||
public function submit(string $text): self
|
||||
{
|
||||
if ($this->type != 'modal')
|
||||
throw new Exception(sprintf('Submit is not required for %s',$type));
|
||||
throw new SlackSyntaxException(sprintf('Submit is not required for %s',$this->type));
|
||||
|
||||
if ($text->type != 'plain_text')
|
||||
throw new Exception(sprintf('Text must be plain_text not %s',$text->type));
|
||||
|
||||
if (strlen($text->text) > self::LIMITS['submit'])
|
||||
throw new Exception(sprintf('Text must be %d chars or less',self::LIMITS['submit']));
|
||||
|
||||
$this->submit = $text;
|
||||
$this->submit = Text::item($this->validate('submit',$text),'plain_text');
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -194,24 +174,10 @@ final class Modal extends BlockKit
|
||||
public function submit_disabled(bool $bool): self
|
||||
{
|
||||
if ($this->type != 'modal')
|
||||
throw new Exception(sprintf('submit_disabled is not required for %s',$type));
|
||||
throw new SlackSyntaxException(sprintf('submit_disabled is not required for %s',$this->type));
|
||||
|
||||
$this->submit_disabled = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a block to the modal
|
||||
*
|
||||
* @param Blocks $blocks
|
||||
* @return $this
|
||||
* @deprecated use addBlock() instead
|
||||
*/
|
||||
public function setBlocks(Blocks $blocks): self
|
||||
{
|
||||
$this->_data->put('blocks',$blocks);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ use React\EventLoop\Loop;
|
||||
use Slack\Client\SocketMode;
|
||||
use Slack\Command\Factory as SlackCommandFactory;
|
||||
use Slack\Event\Factory as SlackEventFactory;
|
||||
use Slack\Exceptions\SlackException;
|
||||
use Slack\Interactive\Factory as SlackInteractiveFactory;
|
||||
|
||||
class SlackSocketClient extends Command
|
||||
@ -33,13 +34,13 @@ class SlackSocketClient extends Command
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
// Make sure our socket_token is defined
|
||||
if (! config('slack.socket_token'))
|
||||
throw new \Exception('SocketMode Client Token not defined.');
|
||||
throw new SlackException('SocketMode Client Token not defined.');
|
||||
|
||||
$loop = Loop::get();
|
||||
|
||||
|
7
src/Exceptions/SlackSyntaxException.php
Normal file
7
src/Exceptions/SlackSyntaxException.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Slack\Exceptions;
|
||||
|
||||
class SlackSyntaxException extends SlackException
|
||||
{
|
||||
}
|
@ -18,7 +18,6 @@ final class InteractiveOptionsController extends Controller
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function fire(Request $request)
|
||||
{
|
||||
|
@ -73,7 +73,7 @@ final class CheckRequest
|
||||
return response('',200);
|
||||
|
||||
} else {
|
||||
Log::debug(sprintf('%s:Incoming Request Allowed',static::LOGKEY),['m'=>__METHOD__,'e'=>$event->enterprise_id,'t'=>$event->team_id,'eo'=>$event->enterprise()->id,'to'=>$event->team()]);
|
||||
Log::debug(sprintf('%s:Incoming Request Allowed [%s/%s]',static::LOGKEY,$event->enterprise_id,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ abstract class Base extends SlackBase
|
||||
* + user_id
|
||||
* @param string $key
|
||||
* @return mixed|object
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __get(string $key)
|
||||
{
|
||||
@ -46,6 +47,12 @@ abstract class Base extends SlackBase
|
||||
case 'user_id':
|
||||
return object_get($this->_data,'user.id');
|
||||
|
||||
case 'callback_key':
|
||||
return $this->keyitem('id',$this->callback_id);
|
||||
|
||||
case 'callback_value':
|
||||
return $this->keyitem('value',$this->callback_id);
|
||||
|
||||
case 'callback_id':
|
||||
case 'trigger_id':
|
||||
case 'type':
|
||||
|
@ -5,7 +5,6 @@ namespace Slack\Interactive;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use Slack\Exceptions\SlackException;
|
||||
use Slack\Models\Channel;
|
||||
|
||||
/**
|
||||
@ -65,20 +64,25 @@ final class BlockActions extends Base
|
||||
case 'action':
|
||||
return Arr::get(object_get($this->_data,'actions'),$this->index);
|
||||
|
||||
case 'action_id':
|
||||
return object_get(Arr::get($this->actions,$this->index),$key);
|
||||
|
||||
// An event can have more than 1 action, each action can have 1 value.
|
||||
case 'action_key':
|
||||
return $this->action('action');
|
||||
return $this->keyitem('id',$this->action_id);
|
||||
|
||||
case 'action_value':
|
||||
return $this->action('value');
|
||||
return $this->keyitem('value',$this->action_id);
|
||||
|
||||
// Interactive Messages have a block_id.
|
||||
case 'block_key':
|
||||
return $this->keyitem('id',$this->block_id);
|
||||
|
||||
case 'block_value':
|
||||
return $this->keyitem('value',$this->block_id);
|
||||
|
||||
case 'actions':
|
||||
case 'response_url':
|
||||
return object_get($this->_data,$key);
|
||||
|
||||
case 'action_id':
|
||||
case 'block_id':
|
||||
return object_get($this->action,$key);
|
||||
|
||||
@ -93,14 +97,14 @@ final class BlockActions extends Base
|
||||
return NULL;
|
||||
|
||||
default:
|
||||
throw new SlackException('Unknown container type: '.$this->container_type);
|
||||
throw new \Exception('Unknown container type: '.$this->container_type);
|
||||
}
|
||||
|
||||
case 'container_type':
|
||||
return object_get($this->_data,'container.type');
|
||||
|
||||
case 'channel_id':
|
||||
return object_get($this->_data,'channel.id') ?: Channel::findOrFail($this->action('value'))->channel_id;
|
||||
return object_get($this->_data,'channel.id') ?: Channel::findOrFail($this->keyitem('value',object_get($this->action,'action_id')))->channel_id;
|
||||
|
||||
case 'keys':
|
||||
return collect(object_get($this->_data,'view.blocks'))->pluck('accessory.action_id');
|
||||
@ -143,7 +147,7 @@ final class BlockActions extends Base
|
||||
// @todo To Check
|
||||
case 'overflow':
|
||||
// @todo To Check
|
||||
throw new SlackException('To be implemented: ',$x);
|
||||
throw new \Exception('To be implemented: ',$x);
|
||||
case 'static_select':
|
||||
return count(object_get($this->action,'selected_option'));
|
||||
case 'multi_static_select':
|
||||
@ -157,38 +161,6 @@ final class BlockActions extends Base
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate out an action command to the id that the command relates to
|
||||
*
|
||||
* @param string $key
|
||||
* @return string|null
|
||||
* @throws SlackException
|
||||
*/
|
||||
private function action(string $key): ?string
|
||||
{
|
||||
$regex = '/^([a-z_]+)\|([0-9]+)$/';
|
||||
$action = NULL;
|
||||
$value = NULL;
|
||||
|
||||
// We only take the action up to the pipe symbol
|
||||
$action_key = object_get($this->action,'action_id');
|
||||
|
||||
if (preg_match($regex,$action_key)) {
|
||||
$action = preg_replace($regex,'$1',$action_key);
|
||||
$value = preg_replace($regex,'$2',$action_key);
|
||||
}
|
||||
|
||||
switch ($key) {
|
||||
case 'action':
|
||||
return $action ?: $action_key;
|
||||
case 'value':
|
||||
return $value;
|
||||
|
||||
default:
|
||||
throw new SlackException('Unknown key: '.$key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Some block actions are triggered by messages, and thus dont have a callback_id
|
||||
*
|
||||
|
@ -99,26 +99,8 @@ class InteractiveMessage extends Base
|
||||
public function respond(): Message
|
||||
{
|
||||
Log::info(sprintf('%s:Interactive Message - Callback [%s] Name [%s] Type [%s]',static::LOGKEY,$this->callback_id,$this->name,$this->type),['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:Unhandled action [%s]',static::LOGKEY,$this->callback_id),['m'=>__METHOD__]);
|
||||
|
||||
$action = NULL;
|
||||
$id = NULL;
|
||||
|
||||
if (preg_match('/^(.*)\|([0-9]+)/',$this->callback_id)) {
|
||||
[$action,$id] = explode('|',$this->callback_id,2);
|
||||
|
||||
} elseif (preg_match('/^[a-z_]+$/',$this->callback_id)) {
|
||||
$id = $this->name;
|
||||
$action = $this->callback_id;
|
||||
|
||||
} 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__]);
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
default:
|
||||
Log::notice(sprintf('%s:Unhandled ACTION [%s]',static::LOGKEY,$action),['m'=>__METHOD__]);
|
||||
return (new Message)->text('That didnt work, I didnt know what to do with your button - you might like to tell '.$this->team()->owner->slack_user);
|
||||
}
|
||||
return (new Message)->text('That didnt work, I didnt know what to do with your button - you might like to tell '.$this->team()->owner->slack_user);
|
||||
}
|
||||
}
|
@ -2,13 +2,9 @@
|
||||
|
||||
namespace Slack\Interactive;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Blockkit\Modal;
|
||||
use Slack\Exceptions\SlackException;
|
||||
use Slack\Models\Team;
|
||||
|
||||
/**
|
||||
* Incoming modal view submission event.
|
||||
@ -28,14 +24,14 @@ class ViewSubmission extends Base
|
||||
case 'blocks':
|
||||
return collect(object_get($this->_data,'view.'.$key));
|
||||
|
||||
case 'callback':
|
||||
case 'callback_id':
|
||||
return object_get($this->_data,'view.callback_id');
|
||||
|
||||
case 'callback_id':
|
||||
return $this->callback('id');
|
||||
case 'callback_key':
|
||||
return $this->keyitem('id',$this->callback_id);
|
||||
|
||||
case 'callback_value':
|
||||
return $this->callback('value');
|
||||
return $this->keyitem('value',$this->callback_id);
|
||||
|
||||
case 'meta':
|
||||
return object_get($this->_data,'view.private_metadata');
|
||||
@ -51,70 +47,29 @@ class ViewSubmission extends Base
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate out an callback command to the id that the command relates to
|
||||
*
|
||||
* @param string $key
|
||||
* @return string|null
|
||||
* @throws SlackException
|
||||
*/
|
||||
private function callback(string $key): ?string
|
||||
{
|
||||
$regex = '/^([a-z_]+)\|([0-9]+)$/';
|
||||
$id = NULL;
|
||||
$value = NULL;
|
||||
|
||||
if (preg_match($regex,$this->callback)) {
|
||||
$id = preg_replace($regex,'$1',$this->callback);
|
||||
$value = preg_replace($regex,'$2',$this->callback);
|
||||
}
|
||||
|
||||
switch ($key) {
|
||||
case 'id':
|
||||
return $id ?: $this->callback;
|
||||
case 'value':
|
||||
return $value;
|
||||
|
||||
default:
|
||||
throw new SlackException('Unknown key: '.$key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should be overridden by a local implementation
|
||||
*
|
||||
* @return Modal
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function respond(): Modal
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:View Submission for Callback [%s] User [%s] in [%s]',self::LOGKEY,$this->callback_id,$this->user_id,$this->team_id),['m'=>__METHOD__]);
|
||||
|
||||
$action = NULL;
|
||||
$id = NULL;
|
||||
|
||||
if (preg_match('/^(.*)\|([0-9]+)/',$this->callback_id)) {
|
||||
[$action,$cid] = explode('|',$this->callback_id,2);
|
||||
|
||||
} elseif (preg_match('/^[a-z_]+$/',$this->callback_id)) {
|
||||
$action = $this->callback_id;
|
||||
|
||||
} 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__]);
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
default:
|
||||
Log::notice(sprintf('%s:Unhandled ACTION [%s]',self::LOGKEY,$action),['m'=>__METHOD__]);
|
||||
}
|
||||
Log::notice(sprintf('%s:Unhandled action [%s]',self::LOGKEY,$this->callback_key),['m'=>__METHOD__]);
|
||||
|
||||
return new Modal;
|
||||
}
|
||||
|
||||
public function value(string $block_id,string $action_id): ?string
|
||||
public function value(string $block_id,string $action_id=NULL): ?string
|
||||
{
|
||||
return object_get($this->state->get($block_id),$action_id.'.value');
|
||||
// If there is no state we need to search out blocks for the block_id
|
||||
if (! $this->state->count()) {
|
||||
$block = $this->blocks->search(function($item) use ($block_id) { return $item->block_id == $block_id; });
|
||||
return $block !== FALSE ? object_get($this->blocks->get($block),$action_id) : NULL;
|
||||
|
||||
} else {
|
||||
return object_get($this->state->get($block_id),$action_id.'.value');
|
||||
}
|
||||
}
|
||||
}
|
@ -22,16 +22,16 @@ final class DeleteChat extends Job
|
||||
public function __construct(Model $o,string $ts)
|
||||
{
|
||||
if ($o instanceof Channel) {
|
||||
$this->_data['cid'] = $o->channel_id;
|
||||
$this->cid = $o->channel_id;
|
||||
|
||||
} elseif ($o instanceof User) {
|
||||
$this->_data['cid'] = $o->user_id;
|
||||
$this->cid = $o->user_id;
|
||||
|
||||
} else
|
||||
throw new \Exception('Invalid Model: '.get_class($o));
|
||||
|
||||
$this->_data['to'] = $o->team;
|
||||
$this->_data['ts'] = $ts;
|
||||
$this->to = $o->team;
|
||||
$this->ts = $ts;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,18 +15,16 @@ final class DeleteResponse extends Job
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param string $url
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(string $url)
|
||||
{
|
||||
$this->_data['url'] = $url;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
@ -7,6 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
abstract class Job implements ShouldQueue
|
||||
{
|
||||
@ -23,10 +24,20 @@ abstract class Job implements ShouldQueue
|
||||
|
||||
use InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $_data = [];
|
||||
protected Collection $_data;
|
||||
|
||||
public function __get($key)
|
||||
{
|
||||
if (! isset($this->_data))
|
||||
$this->_data = collect();
|
||||
|
||||
return Arr::get($this->_data,$key);
|
||||
}
|
||||
|
||||
public function __set(string $key,$value) {
|
||||
if (! isset($this->_data))
|
||||
$this->_data = collect();
|
||||
|
||||
return $this->_data->put($key,$value);
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@ class TeamUpdate extends Job
|
||||
*/
|
||||
public function __construct(Team $to)
|
||||
{
|
||||
$this->_data['to'] = $to;
|
||||
$this->to = $to;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
|
@ -20,11 +20,11 @@ class AppHomeOpenedListener implements ShouldQueue
|
||||
* @param AppHomeOpened $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(AppHomeOpened $event)
|
||||
public function handle(AppHomeOpened $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:App Home Page open for [%s] in team [%s]',self::LOGKEY,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
dispatch((new SlackHomeTabUpdate($event->user(),$event->team(TRUE),$event->tab,$event->view))->onQueue('high'));
|
||||
dispatch((new SlackHomeTabUpdate($event->user(),$event->team(TRUE),$event->tab,$event->view))->onQueue('slack'));
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Slack\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Jobs\DeleteChat;
|
||||
@ -10,13 +11,16 @@ use Slack\Interactive\BlockActions;
|
||||
/**
|
||||
* This class handles BlockActions events.
|
||||
* It's expected that the local application would implement this class completely, rather than using this
|
||||
* modules implementation.
|
||||
* module's implementation.
|
||||
*
|
||||
* @note Since block actions contain a trigger_id, we shouldnt queue this as the trigger_id may have expired by the time
|
||||
* the queue runs the job. (trigger_id's only last 3 seconds)
|
||||
*/
|
||||
class BlockActionListener
|
||||
class BlockActionListener //implements ShouldQueue
|
||||
{
|
||||
protected const LOGKEY = 'LBA';
|
||||
|
||||
public $queue = 'slack';
|
||||
// public $queue = 'slack';
|
||||
|
||||
/**
|
||||
* Handle the event.
|
||||
@ -60,7 +64,7 @@ class BlockActionListener
|
||||
switch ($event->action_id) {
|
||||
case 'self_destruct':
|
||||
// Queue the delete of the message
|
||||
dispatch((new DeleteChat($event->user(),$event->message_ts))->onQueue('low'));
|
||||
dispatch((new DeleteChat($event->user(),$event->message_ts))->onQueue('slack'));
|
||||
|
||||
// @todo If this message is on integrations messages channel, which is not the user_id() - need to use the user's integration direct channel ID
|
||||
break;
|
||||
|
@ -2,10 +2,12 @@
|
||||
|
||||
namespace Slack\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Options\BlockSuggestion;
|
||||
|
||||
class BlockSuggestionListener
|
||||
class BlockSuggestionListener implements ShouldQueue
|
||||
{
|
||||
private const LOGKEY = 'LBS';
|
||||
|
||||
@ -22,6 +24,7 @@ class BlockSuggestionListener
|
||||
public function handle(BlockSuggestion $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Block Suggestion for Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['e'=>$event]);
|
||||
Log::info(sprintf('%s:Block Suggestion Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['e'=>$event]);
|
||||
Log::notice(sprintf('%s:Ignoring Block Suggestion [%s]',static::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ class ChannelJoinListener implements ShouldQueue
|
||||
* @param MemberJoinedChannel $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(MemberJoinedChannel $event)
|
||||
public function handle(MemberJoinedChannel $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:User [%s] joined Channel [%s]',self::LOGKEY,$event->invited,$event->channel_id),['m'=>__METHOD__]);
|
||||
|
@ -19,7 +19,7 @@ class ChannelLeftListener implements ShouldQueue
|
||||
* @param Base $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Base $event)
|
||||
public function handle(Base $event): void
|
||||
{
|
||||
if (! $event instanceof ChannelLeft AND ! $event instanceof GroupLeft)
|
||||
abort(500,'Wrong class calling this listener? '.get_class($event));
|
||||
|
@ -19,14 +19,10 @@ class InteractiveMessageListener implements ShouldQueue
|
||||
* @param InteractiveMessage $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(InteractiveMessage $event)
|
||||
public function handle(InteractiveMessage $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Interactive Message for Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
switch ($event->callback_id) {
|
||||
default:
|
||||
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',self::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
Log::info(sprintf('%s:Interactive Message Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:Ignoring Interactive Message [%s]',static::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
}
|
@ -19,7 +19,7 @@ class MessageListener implements ShouldQueue
|
||||
* @param Message $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Message $event)
|
||||
public function handle(Message $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Message event [%s] - subtype [%s]',self::LOGKEY,$event->ts,$event->type),['m'=>__METHOD__]);
|
||||
|
@ -19,7 +19,7 @@ class PinAddedListener implements ShouldQueue
|
||||
* @param PinAdded $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(PinAdded $event)
|
||||
public function handle(PinAdded $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Pin Added to message [%s] in [%s]',self::LOGKEY,$event->ts,$event->channel_id),['m'=>__METHOD__]);
|
||||
|
@ -19,7 +19,7 @@ class PinRemovedListener implements ShouldQueue
|
||||
* @param PinRemoved $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(PinRemoved $event)
|
||||
public function handle(PinRemoved $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Pin Removed from message [%s] in [%s]',self::LOGKEY,$event->ts,$event->channel_id),['m'=>__METHOD__]);
|
||||
|
@ -19,11 +19,10 @@ class ReactionAddedListener implements ShouldQueue
|
||||
* @param ReactionAdded $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ReactionAdded $event)
|
||||
public function handle(ReactionAdded $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Reaction [%s] added to message in [%s]',self::LOGKEY,$event->reaction,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
Log::debug(sprintf('%s:Ignoring Reaction Add [%s] on [%s]',static::LOGKEY,$event->reaction,$event->ts),['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:Ignoring Reaction Add [%s] on [%s]',static::LOGKEY,$event->reaction,$event->ts),['m'=>__METHOD__]);
|
||||
}
|
||||
}
|
@ -2,13 +2,13 @@
|
||||
|
||||
namespace Slack\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Blockkit\Blocks\Elements\Text;
|
||||
use Slack\Blockkit\Blocks\{Header,Section};
|
||||
use Slack\Blockkit\Modal;
|
||||
use Slack\Interactive\Shortcut;
|
||||
use Slack\Message;
|
||||
|
||||
class ShortcutListener //implements ShouldQueue
|
||||
{
|
||||
@ -20,38 +20,28 @@ class ShortcutListener //implements ShouldQueue
|
||||
* Handle the event.
|
||||
*
|
||||
* @param Shortcut $event
|
||||
* @return Message|null
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
* @todo To Test
|
||||
*/
|
||||
public function handle(Shortcut $event): ?Message
|
||||
public function handle(Shortcut $event): void
|
||||
{
|
||||
if (! $event->channel() || ! $event->channel()->active) {
|
||||
$modal = new Modal(Text::item(config('app.name'),'plain_text'));
|
||||
$modal = new Modal(config('app.name'));
|
||||
|
||||
$modal->addBlock(
|
||||
Header::item(Text::item(':robot_face: Bot not in this channel','plain_text'))
|
||||
)
|
||||
->addBlock(
|
||||
Section::item(Text::item('Please add the BOT to this channel and try this again.'))
|
||||
);
|
||||
$modal->addBlock(Header::item(':robot_face: Bot not in this channel'))
|
||||
->addBlock(Section::item(Text::item('Please add the BOT to this channel and try this again.')));
|
||||
|
||||
try {
|
||||
$event->team()->slackAPI()->viewOpen($event->trigger_id,json_encode($modal));
|
||||
$event->team()->slackAPI()->viewOpen($event->trigger_id,$modal);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error(sprintf('%s:Got an error posting view to slack: %s',static::LOGKEY,$e->getMessage()),['m'=>__METHOD__]);
|
||||
}
|
||||
|
||||
return (new Message)->blank();
|
||||
}
|
||||
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Shortcut [%s] triggered for: [%s]',self::LOGKEY,$event->callback_id,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
switch ($event->callback_id) {
|
||||
default:
|
||||
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',self::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
Log::notice(sprintf('%s:Ignoring Shortcut [%s] in team [%s]',static::LOGKEY,$event->callback_id,$event->team_id),['m'=>__METHOD__]);
|
||||
}
|
||||
}
|
@ -19,14 +19,10 @@ class ViewClosedListener implements ShouldQueue
|
||||
* @param ViewClosed $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ViewClosed $event)
|
||||
public function handle(ViewClosed $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:Block Action for Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
switch ($event->callback_id) {
|
||||
default:
|
||||
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',self::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
Log::info(sprintf('%s:View Closed Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:Ignoring View Closed [%s]',static::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
}
|
@ -19,14 +19,10 @@ class ViewSubmissionListener implements ShouldQueue
|
||||
* @param ViewSubmission $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(ViewSubmission $event)
|
||||
public function handle(ViewSubmission $event): void
|
||||
{
|
||||
// Do some magic with event data
|
||||
Log::info(sprintf('%s:View Submission for Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
|
||||
switch ($event->callback_id) {
|
||||
default:
|
||||
Log::notice(sprintf('%s:Unhandled CALLBACK [%s]',self::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
Log::info(sprintf('%s:View Submission Callback [%s] User [%s] in [%s]',self::LOGKEY,$event->callback_id,$event->user_id,$event->team_id),['m'=>__METHOD__]);
|
||||
Log::notice(sprintf('%s:Ignoring View Closed [%s]',static::LOGKEY,$event->callback_id),['m'=>__METHOD__]);
|
||||
}
|
||||
}
|
214
src/Message.php
214
src/Message.php
@ -7,13 +7,14 @@ use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Blockkit\Blocks;
|
||||
use Slack\Blockkit\Blocks\Context;
|
||||
use Slack\Blockkit\Blocks\{Context,Divider,Section};
|
||||
use Slack\Blockkit\Blocks\Elements\Text;
|
||||
use Slack\Exceptions\SlackException;
|
||||
use Slack\Jobs\DeleteChat;
|
||||
use Slack\Exceptions\{SlackException,SlackSyntaxException};
|
||||
use Slack\Jobs\{DeleteChat,DeleteResponse};
|
||||
use Slack\Message\Attachment;
|
||||
use Slack\Models\{Channel,User};
|
||||
use Slack\Response\Generic;
|
||||
@ -25,7 +26,8 @@ final class Message extends BlockKit
|
||||
{
|
||||
private const LOGKEY = 'SM-';
|
||||
|
||||
private const MAX_ATTACHMENTS = 20;
|
||||
public const MAX_ATTACHMENTS = 20;
|
||||
public const MAX_BLOCKS = 50;
|
||||
|
||||
private Model $o;
|
||||
private ?Carbon $selfdestruct = NULL;
|
||||
@ -34,7 +36,7 @@ final class Message extends BlockKit
|
||||
* Message constructor.
|
||||
*
|
||||
* @param Model|null $o Who the message will be to - Channel or User
|
||||
* @throws SlackException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(Model $o=NULL)
|
||||
{
|
||||
@ -50,7 +52,7 @@ final class Message extends BlockKit
|
||||
$this->setUser($o);
|
||||
|
||||
} else {
|
||||
throw new SlackException('Model not handled: '.get_class($o));
|
||||
throw new \Exception('Model not handled: '.get_class($o));
|
||||
}
|
||||
|
||||
$this->o = $o;
|
||||
@ -72,9 +74,9 @@ final class Message extends BlockKit
|
||||
/**
|
||||
* Add a block to the message
|
||||
*
|
||||
* @param Blocks $blocks
|
||||
* @param Attachment $attachment
|
||||
* @return Message
|
||||
* @todo to test
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function addAttachment(Attachment $attachment): self
|
||||
{
|
||||
@ -84,7 +86,7 @@ final class Message extends BlockKit
|
||||
$this->attachments->push($attachment);
|
||||
|
||||
if (count($this->attachments) > self::MAX_ATTACHMENTS)
|
||||
throw new Exception(sprintf('Messages should not have more than %d attachments',self::MAX_ATTACHMENTS));
|
||||
throw new SlackSyntaxException(sprintf('Messages should not have more than %d attachments',self::MAX_ATTACHMENTS));
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -96,6 +98,9 @@ final class Message extends BlockKit
|
||||
|
||||
$this->blocks->push($block);
|
||||
|
||||
if (count($this->blocks) > self::MAX_BLOCKS)
|
||||
throw new SlackSyntaxException(sprintf('Messages should not have more than %d blocks',self::MAX_BLOCKS));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -119,6 +124,14 @@ final class Message extends BlockKit
|
||||
return $this;
|
||||
}
|
||||
|
||||
// This is a helper method
|
||||
public function divider(): self
|
||||
{
|
||||
$this->blocks->push(Divider::item());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function forgetTS(): self
|
||||
{
|
||||
$this->_data->forget('ts');
|
||||
@ -133,7 +146,7 @@ final class Message extends BlockKit
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
return $this->jsonSerialize() ? FALSE : TRUE;
|
||||
return ! $this->jsonSerialize();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -142,7 +155,7 @@ final class Message extends BlockKit
|
||||
public function jsonSerialize()
|
||||
{
|
||||
// For interactive messages that generate a dialog, we need to return NULL
|
||||
return $this->_data->count() ? parent::jsonSerialize() : NULL;
|
||||
return count($this) ? parent::jsonSerialize() : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -157,20 +170,31 @@ final class Message extends BlockKit
|
||||
if (! $delete && $this->selfdestruct)
|
||||
$delete = $this->selfdestruct;
|
||||
|
||||
if ($this->_data->has('ephemeral'))
|
||||
abort('500','Cannot post ephemeral messages.');
|
||||
if ((! isset($this->o)) || ((! $this->o->team) && (! $this->o->user_team)))
|
||||
throw new SlackSyntaxException('Message needs to have a user or a channel to work out the team.');
|
||||
|
||||
if ($this->_data->get('blocks') && $this->_data->get('attachments'))
|
||||
throw new SlackException('Message cannot have blocks and attachments.');
|
||||
$api = $this->o->team ? $this->o->team->slackAPI() : $this->o->user_team->slackAPI();
|
||||
|
||||
$api = $this->o->team->slackAPI();
|
||||
$response = $this->_data->has('ts') ? $api->updateMessage($this) : $api->postMessage($this);
|
||||
if ($this->ephemeral) {
|
||||
$response = $api->postEphemeral($this);
|
||||
|
||||
} else {
|
||||
$response = $this->_data->has('ts') ? $api->updateMessage($this) : $api->postMessage($this);
|
||||
}
|
||||
|
||||
if ($delete) {
|
||||
Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$delete->format('Y-m-d')),['m'=>__METHOD__]);
|
||||
Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$delete->format('Y-m-d H:i')),['m'=>__METHOD__,'r'=>$response,'message_ts'=>$response->ts]);
|
||||
|
||||
// Queue the delete of the message if requested
|
||||
dispatch((new DeleteChat($this->o,$response->ts))->onQueue('low')->delay($delete));
|
||||
if ($this->o instanceof User) {
|
||||
Log::error(sprintf('%s:Cannot schedule delete of [%s:%s] on user channels [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts,$this->o->user_id),['m'=>__METHOD__]);
|
||||
|
||||
} elseif ($this->ephemeral) {
|
||||
Log::error(sprintf('%s:Ignoring delete of [%s:%s] on ephemeral messages',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$response->ts),['m'=>__METHOD__]);
|
||||
|
||||
} else {
|
||||
// Queue the delete of the message if requested
|
||||
dispatch((new DeleteChat($this->o,$response->ts))->onQueue('slack')->delay($delete));
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
@ -181,26 +205,21 @@ final class Message extends BlockKit
|
||||
*
|
||||
* @note This URL can only be used 5 times in 30 minutes
|
||||
* @param string $url
|
||||
* @return string
|
||||
* @param Carbon|null $delete
|
||||
* @return object
|
||||
* @throws SlackException
|
||||
*/
|
||||
public function respond(string $url)
|
||||
public function respond(string $url,Carbon $delete=NULL): object
|
||||
{
|
||||
$request = curl_init();
|
||||
if (! $delete && $this->selfdestruct)
|
||||
$delete = $this->selfdestruct;
|
||||
|
||||
curl_setopt($request,CURLOPT_URL,$url);
|
||||
curl_setopt($request,CURLOPT_RETURNTRANSFER,TRUE);
|
||||
curl_setopt($request,CURLINFO_HEADER_OUT,TRUE);
|
||||
curl_setopt($request,CURLOPT_HTTPHEADER,['Content-Type: application/json; charset=utf-8']);
|
||||
curl_setopt($request,CURLOPT_SSL_VERIFYPEER,FALSE);
|
||||
curl_setopt($request,CURLOPT_POSTFIELDS,json_encode($this));
|
||||
$http = Http::acceptJson();
|
||||
$http->withBody(json_encode($this),'application/json');
|
||||
|
||||
try {
|
||||
$result = curl_exec($request);
|
||||
if (! $result)
|
||||
throw new \Exception('CURL exec returned an empty response: '.serialize(curl_getinfo($request)));
|
||||
|
||||
$result = json_decode($result);
|
||||
$request = $http->post($url)->throw();
|
||||
$response = $request->object();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error(sprintf('%s:Got an error while posting to [%s] (%s)',static::LOGKEY,$url,$e->getMessage()),['m'=>__METHOD__]);
|
||||
@ -208,16 +227,19 @@ final class Message extends BlockKit
|
||||
throw new \Exception($e->getMessage());
|
||||
}
|
||||
|
||||
if (! $result->ok) {
|
||||
switch ($result) {
|
||||
default:
|
||||
Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$result]);
|
||||
throw new SlackException($result,curl_getinfo($request,CURLINFO_HTTP_CODE));
|
||||
}
|
||||
if (! $response->ok) {
|
||||
Log::critical(sprintf('%s:Generic Error',static::LOGKEY),['m'=>__METHOD__,'r'=>$response]);
|
||||
throw new SlackException(serialize($response),$request->status());
|
||||
}
|
||||
|
||||
curl_close($request);
|
||||
return $result;
|
||||
if ($delete) {
|
||||
Log::debug(sprintf('%s:Scheduling Delete of [%s:%s] on [%s]',static::LOGKEY,object_get($this->o,'channel_id',$this->o->id),$url,$delete->format('Y-m-d')),['m'=>__METHOD__]);
|
||||
|
||||
// Queue the delete of the response if requested
|
||||
dispatch((new DeleteResponse($url))->onQueue('slack')->delay($delete));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -241,6 +263,7 @@ final class Message extends BlockKit
|
||||
*
|
||||
* @param Carbon $time
|
||||
* @return Message
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function selfdestruct(Carbon $time): self
|
||||
{
|
||||
@ -251,6 +274,7 @@ final class Message extends BlockKit
|
||||
);
|
||||
|
||||
$this->selfdestruct = $time;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -282,11 +306,18 @@ final class Message extends BlockKit
|
||||
return $this;
|
||||
}
|
||||
|
||||
// This is a helper method
|
||||
public function spacer(): self
|
||||
{
|
||||
$this->blocks->push(Section::item(Text::item(' ')));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/* CONFIGURATION METHODS */
|
||||
|
||||
/**
|
||||
* @return Message
|
||||
* @todo - Check this is a valid option
|
||||
*/
|
||||
public function ephemeral(): self
|
||||
{
|
||||
@ -323,8 +354,10 @@ final class Message extends BlockKit
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the original message
|
||||
*
|
||||
* @param bool $bool
|
||||
* @return Message
|
||||
* @todo - Check this is a valid option
|
||||
*/
|
||||
public function replace_original(bool $bool=TRUE): self
|
||||
{
|
||||
@ -385,84 +418,29 @@ final class Message extends BlockKit
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Post the message to user
|
||||
*
|
||||
* @param string $user
|
||||
* @return $this
|
||||
*/
|
||||
public function user(string $user): self
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set bot username (used with as_user)
|
||||
*
|
||||
* @param string $user
|
||||
* @return $this
|
||||
*/
|
||||
public function username(string $user): self
|
||||
{
|
||||
$this->username = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an attachment to a message
|
||||
*
|
||||
* @param Attachment $attachments
|
||||
* @return Message
|
||||
* @deprecated use addAttachment()
|
||||
*/
|
||||
public function setAttachments(Attachment $attachments): self
|
||||
{
|
||||
$this->_data->put('attachments',[$attachments]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add blocks to the message
|
||||
*
|
||||
* @param Blocks $blocks
|
||||
* @return Message
|
||||
* @throws \Exception
|
||||
* @deprecated use addBlocks()
|
||||
*/
|
||||
public function setBlocks(Blocks $blocks): self
|
||||
{
|
||||
if ($this->blocks->count())
|
||||
throw new \Exception('Blocks already defined');
|
||||
|
||||
$this->blocks = $blocks;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Message text
|
||||
*
|
||||
* @param string $string
|
||||
* @return Message
|
||||
* @deprecated use text()
|
||||
*/
|
||||
public function setText(string $string): self
|
||||
{
|
||||
$this->text = $string;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the timestamp, used when replacing messages
|
||||
*
|
||||
* @param string $string
|
||||
* @return Message
|
||||
* @deprecated use ts()
|
||||
*/
|
||||
public function setTS(string $string): self
|
||||
{
|
||||
$this->_data->put('ts',$string);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the thread timestamp, used when adding a threaded response
|
||||
*
|
||||
* @param string $string
|
||||
* @return Message
|
||||
* @deprecated use thead_ts()
|
||||
*/
|
||||
public function setThreadTS(string $string): self
|
||||
{
|
||||
$this->_data->put('thread_ts',$string);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace Slack\Message;
|
||||
|
||||
use \Exception;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
use Slack\BlockKit;
|
||||
@ -10,7 +9,7 @@ use Slack\Blockkit\Blocks;
|
||||
|
||||
final class Attachment extends BlockKit
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'footer' => 300,
|
||||
];
|
||||
|
||||
|
@ -4,10 +4,11 @@ namespace Slack\Message;
|
||||
|
||||
use Slack\BlockKit;
|
||||
use Slack\Blockkit\Blocks\Elements\Text;
|
||||
use Slack\Exceptions\SlackSyntaxException;
|
||||
|
||||
/**
|
||||
* Class MessageAttachmentAction - Slack Message Attachments Actions
|
||||
* Represents an Single Action for a Slack Message Attachment
|
||||
* Represents a Single Action for a Slack Message Attachment
|
||||
*
|
||||
* @package App\Slack\Message
|
||||
* @note These are now legacy - use blocks instead
|
||||
@ -15,14 +16,14 @@ use Slack\Blockkit\Blocks\Elements\Text;
|
||||
*/
|
||||
final class AttachmentAction extends BlockKit
|
||||
{
|
||||
protected const LIMITS = [
|
||||
public const LIMITS = [
|
||||
'text' => 30,
|
||||
'value' => 2000,
|
||||
];
|
||||
|
||||
private const DATA_SOURCES = ['static','users','channels','conversastions','external'];
|
||||
private const STYLES = ['default','danger','primary'];
|
||||
private const TYPES = ['button','select'];
|
||||
public const DATA_SOURCES = ['static','users','channels','conversastions','external'];
|
||||
public const STYLES = ['default','danger','primary'];
|
||||
public const TYPES = ['button','select'];
|
||||
|
||||
// @todo options
|
||||
// @todo option_groups
|
||||
@ -33,7 +34,7 @@ final class AttachmentAction extends BlockKit
|
||||
parent::__construct();
|
||||
|
||||
if (! in_array($type,self::TYPES))
|
||||
throw new Exception(sprintf('Type [%s] not valid',$type));
|
||||
throw new SlackSyntaxException(sprintf('Type [%s] not valid',$type));
|
||||
|
||||
$this->type = $type;
|
||||
switch ($type) {
|
||||
@ -80,7 +81,7 @@ final class AttachmentAction extends BlockKit
|
||||
public function data_source(string $string): self
|
||||
{
|
||||
if (! in_array($string,self::DATA_SOURCES))
|
||||
throw new Exception(sprintf('Type [%s] not valid',$string));
|
||||
throw new SlackSyntaxException(sprintf('Type [%s] not valid',$string));
|
||||
|
||||
$this->data_source = $string;
|
||||
|
||||
@ -97,13 +98,14 @@ final class AttachmentAction extends BlockKit
|
||||
/**
|
||||
* Set the text displayed in the action
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $string
|
||||
* @return $this
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function style(string $string): self
|
||||
{
|
||||
if (! in_array($string,self::STYLES))
|
||||
throw new Exception(sprintf('Type [%s] not valid',$string));
|
||||
throw new SlackSyntaxException(sprintf('Type [%s] not valid',$string));
|
||||
|
||||
$this->style = $string;
|
||||
|
||||
@ -115,6 +117,7 @@ final class AttachmentAction extends BlockKit
|
||||
*
|
||||
* @param string $string
|
||||
* @return $this
|
||||
* @throws SlackSyntaxException
|
||||
*/
|
||||
public function value(string $string): self
|
||||
{
|
||||
|
@ -4,9 +4,9 @@ namespace Slack\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Slack\Traits\ScopeActive;
|
||||
|
||||
use App\Models\Slack\Team as AppTeam;
|
||||
use Slack\Traits\ScopeActive;
|
||||
|
||||
class Channel extends Model
|
||||
{
|
||||
|
@ -14,7 +14,7 @@ class Team extends Model
|
||||
{
|
||||
use ScopeActive;
|
||||
|
||||
protected $fillable = ['team_id'];
|
||||
protected $fillable = ['team_id','active'];
|
||||
protected $table = 'slack_teams';
|
||||
|
||||
/* RELATIONS */
|
||||
|
@ -61,8 +61,6 @@ class User extends Model
|
||||
*/
|
||||
public function getUserTeamAttribute(): ?Team
|
||||
{
|
||||
Log::debug(sprintf('%s:User [%s], Team [%s], Enterprise [%s]',self::LOGKEY,$this->id,$this->team_id,$this->enterprise_id),['m'=>__METHOD__]);
|
||||
|
||||
return $this->team_id ? $this->team : (($x=$this->enterprise->teams) ? $x->first() : NULL);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace Slack\Options;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Base as SlackBase;
|
||||
use Slack\Exceptions\SlackException;
|
||||
use Slack\Message;
|
||||
|
||||
abstract class Base extends SlackBase
|
||||
@ -35,47 +36,42 @@ abstract class Base extends SlackBase
|
||||
* + user_id
|
||||
* @param string $key
|
||||
* @return mixed|object
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __get(string $key)
|
||||
{
|
||||
switch ($key) {
|
||||
case 'team_id':
|
||||
return object_get($this->_data,'team.id');
|
||||
case 'channel_id':
|
||||
return object_get($this->_data,'channel.id');
|
||||
|
||||
case 'enterprise_id':
|
||||
return object_get($this->_data,'team.'.$key);
|
||||
|
||||
case 'team_id':
|
||||
return object_get($this->_data,'team.id');
|
||||
case 'user_id':
|
||||
return object_get($this->_data,'user.id');
|
||||
|
||||
case 'callback_key':
|
||||
return $this->keyitem('id',$this->callback_id);
|
||||
|
||||
case 'callback_value':
|
||||
return $this->keyitem('value',$this->callback_id);
|
||||
|
||||
case 'callback_id':
|
||||
//case 'action_ts':
|
||||
//case 'message_ts':
|
||||
case 'type':
|
||||
return object_get($this->_data,$key);
|
||||
|
||||
default:
|
||||
throw new SlackException('Unknown key: '.$key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @throws \Exception
|
||||
*/
|
||||
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();
|
||||
}
|
||||
abstract public function respond(): Message;
|
||||
}
|
@ -126,7 +126,7 @@ use Slack\Message;
|
||||
)
|
||||
)
|
||||
*/
|
||||
class BlockSuggestion extends Base
|
||||
abstract class BlockSuggestion extends Base
|
||||
{
|
||||
protected const LOGKEY = 'OIM';
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace Slack\Options;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@ -26,14 +25,16 @@ class Factory {
|
||||
* @param string $type
|
||||
* @param array $request
|
||||
* @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)
|
||||
{
|
||||
$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__]);
|
||||
|
||||
if (App::environment() == 'dev')
|
||||
file_put_contents('/tmp/option.'.$type,print_r(json_decode($request->input('payload')),TRUE));
|
||||
if (App::environment() == 'local')
|
||||
file_put_contents('/tmp/option.'.$type,print_r($request,TRUE));
|
||||
|
||||
return new $class($request);
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ use Slack\Message;
|
||||
* [attachment_id] => 3
|
||||
* [token] => Oow8S2EFvrZoS9z8N4nwf9Jo
|
||||
*/
|
||||
class InteractiveMessage extends Base
|
||||
abstract class InteractiveMessage extends Base
|
||||
{
|
||||
protected const LOGKEY = 'OIM';
|
||||
|
||||
@ -49,25 +49,4 @@ class InteractiveMessage extends Base
|
||||
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();
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@ namespace Slack\Options;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
use Slack\Message;
|
||||
|
||||
/**
|
||||
* Catch all unknown slack event that we havent specifically programmed for.
|
||||
*
|
||||
@ -11,10 +13,26 @@ use Illuminate\Support\Facades\Log;
|
||||
*/
|
||||
final class Unknown extends Base
|
||||
{
|
||||
protected const LOGKEY = 'OU-';
|
||||
|
||||
public function __construct(array $request)
|
||||
{
|
||||
Log::notice(sprintf('SOU:UNKNOWN Slack Interaction Option received [%s]',get_class($this)),['m'=>__METHOD__]);
|
||||
|
||||
parent::__construct($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interactive messages can return their output in the incoming HTTP post
|
||||
*
|
||||
* @return Message
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ abstract class Base extends SlackBase implements \JsonSerializable
|
||||
if (get_class($this) == Base::class) {
|
||||
Log::debug(sprintf('%s:Slack RESPONSE Initialised [%s]',static::LOGKEY,get_class($this)),['m'=>__METHOD__]);
|
||||
|
||||
if (App::environment() == 'dev')
|
||||
if (App::environment() == 'local')
|
||||
file_put_contents('/tmp/response',print_r($this,TRUE),FILE_APPEND);
|
||||
}
|
||||
}
|
||||
@ -55,8 +55,9 @@ abstract class Base extends SlackBase implements \JsonSerializable
|
||||
case 'scheduled_messages': // Used by scheduledMessagesList()
|
||||
return collect(object_get($this->_data,$key));
|
||||
|
||||
case 'team_id':
|
||||
case 'ts':
|
||||
return object_get($this->_data,$key) ?: object_get($this->_data,'message_ts');
|
||||
case 'team_id':
|
||||
case 'user_id':
|
||||
case 'type': // Needed by URL verification
|
||||
return object_get($this->_data,$key);
|
||||
|
Loading…
x
Reference in New Issue
Block a user