97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Slack\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Slack\Client\Payload;
|
|
use Slack\Command\Factory;
|
|
use Slack\Message;
|
|
use Slack\Message\Attachment;
|
|
|
|
class SlashCommandController extends Controller
|
|
{
|
|
private const LOGKEY = 'CSC';
|
|
|
|
public function fire(Request $request): ?Message
|
|
{
|
|
$command = Factory::make(new Payload($request->all(),TRUE));
|
|
$o = new Message;
|
|
|
|
// If the team is not active.
|
|
if (! $command->team()->active) {
|
|
Log::notice(sprintf('%s:IGNORING command, Team INACTIVE [%s]',static::LOGKEY,$command->team()->id));
|
|
$o->text(sprintf("Sorry, it would appear you are not allowed to use me.\nYour instance hasnt been enabled.\nAsk an admin to install me [%s].",$command->team()->team_id));
|
|
|
|
return $o;
|
|
}
|
|
|
|
if ($command->channel()) {
|
|
// If the channel is a direct message
|
|
if (str_starts_with($command->channel()->channel_id,'D')) {
|
|
Log::notice(sprintf('%s:IGNORING command, Channel is for direct messages [%s]',static::LOGKEY,$command->channel()->id));
|
|
$o->text('Sorry, I dont work with direct messages yet. Please use me in a channel.');
|
|
|
|
return $o;
|
|
}
|
|
|
|
// If the channel is not enabled
|
|
if (! $command->channel()->is_allowed) {
|
|
Log::notice(sprintf('%s:IGNORING command, Channel INACTIVE [%s]',static::LOGKEY,$request->channel_id));
|
|
$o->text(sprintf('Sorry, it would appear this channel is not allowed to use me. %s may be able to help.',$command->team()->owner->slack_user));
|
|
|
|
return $o;
|
|
}
|
|
|
|
// If the user hasnt been checked yet
|
|
if (is_null($command->user()->active)) {
|
|
Log::notice(sprintf('%s:IGNORING command, User UNKNOWN [%s]',static::LOGKEY,$command->user()->id));
|
|
$o->text('Sorry, unfortunately I cannot work with you - as I dont know who you are (yet)...');
|
|
|
|
$a = new Attachment;
|
|
|
|
if ($command->user()->created_at < Carbon::now()->subMinutes(5))
|
|
$a->color('#CC0000')
|
|
->text(sprintf('I probably should have figured out who you are by now, so something might be wrong. You might like to check with the %s.',$command->team()->owner->slack_user));
|
|
else
|
|
$a->color('#008800')
|
|
->text(sprintf('If I havent worked out who you are with the next 5 mins, you might like to check with the %s.',$command->team()->owner->slack_user));
|
|
|
|
$o->addAttachment($a);
|
|
|
|
return $o;
|
|
}
|
|
|
|
// If the user is not enabled
|
|
if (! $command->user()->isAllowed($command->channel())) {
|
|
Log::notice(sprintf('%s:IGNORING command, User INACTIVE [%s]',static::LOGKEY,$command->user()->id));
|
|
$o->text(sprintf('Sorry, it would appear that you are not allowed to use me. %s may be able to help.',$command->team()->owner->slack_user));
|
|
|
|
return $o;
|
|
}
|
|
}
|
|
|
|
// Check that we are active in the channel
|
|
if (! $command->channel() OR ! $command->channel()->active) {
|
|
$o->text('Hi, I am the *Ask the Experts* Bot');
|
|
|
|
$a = new Attachment;
|
|
$a->title('Greetings!')
|
|
->text('Im not active in this channel, so I cant do anything here unless you invite me. Please invite me with `/invite`')
|
|
->color('#FF8080');
|
|
$o->addAttachment($a);
|
|
|
|
return $o;
|
|
}
|
|
|
|
if (! method_exists($command,'respond')) {
|
|
Log::alert(sprintf('%s:Cant respond to Command [%s], no respond method',static::LOGKEY,get_class($command)),['m'=>__METHOD__]);
|
|
abort(500,'No respond method() for '.get_class($command));
|
|
}
|
|
|
|
return ($x=$command->respond())->isEmpty() ? NULL : $x;
|
|
}
|
|
} |