API code tidyup

This commit is contained in:
Deon George 2022-09-04 10:21:01 +10:00
parent a68c7936a6
commit 89c13bcb73

View File

@ -19,7 +19,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 +55,7 @@ final class API
];
// Our slack token to use
private $_token;
private Token $_token;
public function __construct(Team $o)
{
@ -79,13 +79,28 @@ final class API
* @return Generic
* @throws \Exception
*/
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]));
}
/**
* Open a dialogue with the user
*
* @param string $trigger
* @param string $dialog
* @return Generic
* @throws \Exception
*/
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])));
}
/**
* Get Messages on a channel from a specific timestamp
*
@ -95,7 +110,7 @@ final class API
* @return Generic
* @throws \Exception
*/
public function getChannelHistory($channel,$timestamp,$limit=20): Generic
public function getChannelHistory(string $channel,string $timestamp,int $limit=20): Generic
{
Log::debug(sprintf('%s:Message History for Channel [%s] from Timestamp [%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
@ -109,7 +124,7 @@ final class API
* @return Generic
* @throws \Exception
*/
public function getChannelInfo($channel): Generic
public function getChannelInfo(string $channel): Generic
{
Log::debug(sprintf('%s:Channel Information [%s]',static::LOGKEY,$channel),['m'=>__METHOD__]);
@ -166,7 +181,7 @@ final class API
* @return ResponseUser
* @throws \Exception
*/
public function getUser($user_id): ResponseUser
public function getUser(string $user_id): ResponseUser
{
Log::debug(sprintf('%s:User Info [%s]',static::LOGKEY,$user_id),['m'=>__METHOD__]);
@ -174,18 +189,29 @@ final class API
}
/**
* Open a dialogue with the user
* Get the list of channels for a user (the bot normally)
*
* @param string $trigger
* @param string $dialog
* @return Generic
* @param User $uo
* @param int $limit
* @param string|null $cursor
* @return ChannelList
* @throws \Exception
*/
public function dialogOpen(string $trigger,string $dialog): Generic
public function getUserChannels(User $uo,int $limit=100,string $cursor=NULL): ChannelList
{
Log::debug(sprintf('%s:Open a Dialog',static::LOGKEY),['m'=>__METHOD__,'d'=>$dialog,'t'=>$trigger]);
Log::debug(sprintf('%s:Channel List for [%s] (%s:%s)',static::LOGKEY,$uo->user_id,$limit,$cursor),['m'=>__METHOD__]);
return new Generic($this->execute('dialog.open',json_encode(['dialog'=>$dialog,'trigger_id'=>$trigger])));
$args = collect([
'limit'=>$limit,
'exclude_archived'=>false,
'types'=>'public_channel,private_channel',
'user'=>$uo->user_id,
]);
if ($cursor)
$args->put('cursor',$cursor);
return new ChannelList($this->execute('users.conversations',$args->toArray()));
}
/**
@ -205,8 +231,8 @@ final class API
/**
* Pin a message in a channel
*
* @param $channel
* @param $timestamp
* @param string $channel
* @param string $timestamp
* @return Generic
* @throws \Exception
*/
@ -262,7 +288,7 @@ final class API
/**
* Get the scheduled messages
*
* @param Message $request
* @param string|null $request
* @return Generic
* @throws \Exception
*/
@ -281,7 +307,7 @@ final class API
* @return Generic
* @throws \Exception
*/
public function unpinMessage($channel,$timestamp): Generic
public function unpinMessage(string $channel,string $timestamp): Generic
{
Log::debug(sprintf('%s:Remove Pin from Message [%s|%s]',static::LOGKEY,$channel,$timestamp),['m'=>__METHOD__]);
@ -302,32 +328,6 @@ final class API
return new Generic($this->execute('chat.update',json_encode($request)));
}
/**
* Get the list of channels for a user (the bot normally)
*
* @param User $uo
* @param int $limit
* @param string|null $cursor
* @return ChannelList
* @throws \Exception
*/
public function getUserChannels(User $uo,int $limit=100,string $cursor=NULL): ChannelList
{
Log::debug(sprintf('%s:Channel List for [%s] (%s:%s)',static::LOGKEY,$uo->user_id,$limit,$cursor),['m'=>__METHOD__]);
$args = collect([
'limit'=>$limit,
'exclude_archived'=>false,
'types'=>'public_channel,private_channel',
'user'=>$uo->user_id,
]);
if ($cursor)
$args->put('cursor',$cursor);
return new ChannelList($this->execute('users.conversations',$args->toArray()));
}
public function viewOpen(string $trigger,Modal $view): Generic
{
Log::debug(sprintf('%s:Open a view',static::LOGKEY),['m'=>__METHOD__,'t'=>$trigger]);
@ -339,7 +339,7 @@ final class API
* Publish a view
*
* @param string $user
* @param string $view
* @param Modal $view
* @param string $hash
* @return Generic
* @throws \Exception
@ -374,7 +374,7 @@ final class API
* @return object
* @throws \Exception
*/
private function execute(string $method,$parameters = NULL): object
private function execute(string $method,$parameters=NULL): object
{
switch (config('app.env')) {
case 'dev': $url = 'http://steno:3000';
@ -482,12 +482,12 @@ final class API
/**
* Setup the API call
*
* @param $url
* @param string $url
* @param string $parameters
* @param array $headers
* @return resource
* @return \CurlHandle
*/
private function prepareRequest($url,$parameters='',$headers = [])
private function prepareRequest(string $url,string $parameters='',array $headers=[]): \CurlHandle
{
$request = curl_init();