adds integration button, working on its functionality

This commit is contained in:
Godfrey M 2023-02-01 12:08:13 -08:00
parent 1e0c58c3cf
commit f95a904d90
6 changed files with 47 additions and 45 deletions

View file

@ -143,21 +143,11 @@ class SettingsController extends Controller
}
public function slacktest(SlackSettingsRequest $request)
public function slacktest($slack_endpoint,$slack_channel,$slack_botname)
{
$validator = Validator::make($request->all(), [
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable',
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
]);
if ($validator->fails()) {
return response()->json(['message' => 'Validation failed', 'errors' => $validator->errors()], 422);
}
// If validation passes, continue to the curl request
$slack = new Client([
'base_url' => e($request->input('slack_endpoint')),
'base_url' => e($slack_endpoint),
'defaults' => [
'exceptions' => false,
],
@ -165,18 +155,18 @@ class SettingsController extends Controller
$payload = json_encode(
[
'channel' => e($request->input('slack_channel')),
'channel' => e($slack_channel),
'text' => trans('general.slack_test_msg'),
'username' => e($request->input('slack_botname')),
'username' => e($slack_botname),
'icon_emoji' => ':heart:',
]);
try {
$slack->post($request->input('slack_endpoint'), ['body' => $payload]);
$slack->post($slack_endpoint, ['body' => $payload]);
return response()->json(['message' => 'Success'], 200);
} catch (\Exception $e) {
return response()->json(['message' => 'Please check the channel name and webhook endpoint URL ('.e($request->input('slack_endpoint')).'). Slack responded with: '.$e->getMessage()], 400);
return response()->json(['message' => 'Please check the channel name and webhook endpoint URL ('.e($slack_endpoint).'). Slack responded with: '.$e->getMessage()], 400);
}
//}

View file

@ -688,15 +688,15 @@ class SettingsController extends Controller
*
* @return View
*/
public function postSlack(SlackSettingsRequest $request)
public function postSlack($slack_endpoint,$slack_channel,$slack_botname)
{
if (is_null($setting = Setting::getSettings())) {
return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error'));
}
$setting->slack_endpoint = $request->input('slack_endpoint');
$setting->slack_channel = $request->input('slack_channel');
$setting->slack_botname = $request->input('slack_botname');
$setting->slack_endpoint = $slack_endpoint;
$setting->slack_channel = $slack_channel;
$setting->slack_botname = $slack_botname;
if ($setting->save()) {
return redirect()->route('settings.index')

View file

@ -4,28 +4,16 @@ namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Setting;
use App\Http\Controllers\Api\SettingsController;
class SlackSettingsForm extends Component
{
public $slack_endpoint;
public $slack_channel;
public $slack_botname;
public $successMessage;
public Setting $setting;
protected $rules = [
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable',
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
'slack_botname' => 'string|nullable',
];
protected $messages = [
'slack_endpoint.required_with' => 'Slack endpoint is required',
'slack_endpoint.starts_with' => 'Slack endpoint must start with https://hooks.slack.com',
];
public function mount(){
$this->setting = Setting::getSettings();
@ -40,6 +28,11 @@ class SlackSettingsForm extends Component
return view('livewire.slack-settings-form');
}
public function testSlack($slack_endpoint, $slack_channel, $slack_botname){
SettingsController::testSlack($slack_endpoint,$slack_channel,$slack_botname);
}
public function submit()
{
$this->validate([

View file

@ -25,7 +25,6 @@ class SlackSettingsRequest extends Request
'slack_endpoint' => 'url|required_with:slack_channel|starts_with:"https://hooks.slack.com"|nullable',
'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
'slack_botname' => 'string|nullable',
];
}

View file

@ -8,6 +8,7 @@
@endif
<form wire:submit.prevent ="submit">
{{csrf_field()}}
<!--slack endpoint-->
<div class="form-group required {{ $errors->has('slack_endpoint') ? 'error' : '' }}">
<div class="col-md-2">
@ -17,9 +18,9 @@
@if (config('app.lock_passwords')===true)
{{-- {{ Form::text('slack_endpoint', old('slack_endpoint', $setting->slack_endpoint), array('class' => 'form-control','disabled'=>'disabled','placeholder' => 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX', 'id' => 'slack_endpoint')) }}--}}
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
<input type="text" wire:model.defer="slack_endpoint" id="slack_endpoint" class= 'form-control' placeholder="'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX'" {{old('slack_endpoint', $slack_endpoint)}} ><br>
<input type="text" wire:model.lazy="slack_endpoint" id="slack_endpoint" class= 'form-control' placeholder="'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX'" {{old('slack_endpoint', $slack_endpoint)}} ><br>
@else
<input type="text" wire:model.defer="slack_endpoint" id="slack_endpoint" class= 'form-control' placeholder="'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX'" {{old('slack_endpoint', $slack_endpoint)}} ><br>
<input type="text" wire:model.lazy="slack_endpoint" id="slack_endpoint" class= 'form-control' placeholder="'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX'" {{old('slack_endpoint', $slack_endpoint)}} ><br>
{{-- {{ Form::text('slack_endpoint', old('slack_endpoint', $setting->slack_endpoint), array('class' => 'form-control','placeholder' => 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX', 'id' => 'slack_endpoint')) }}--}}
@endif
{!! $errors->first('slack_endpoint', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
@ -33,11 +34,11 @@
</div>
<div class="col-md-10">
@if (config('app.lock_passwords')===true)
<input type="text" wire:model.defer="slack_channel" id="slack_channel" class= 'form-control' placeholder="'#IT-Ops'" {{old('slack_channel', $slack_channel)}} ><br>
<input type="text" wire:model.lazy="slack_channel" id="slack_channel" class= 'form-control' placeholder="'#IT-Ops'" {{old('slack_channel', $slack_channel)}} ><br>
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
@else
<input type="text" wire:model.defer="slack_channel" id="slack_channel" class= 'form-control' placeholder="'#IT-Ops'" {{old('slack_channel', $slack_channel)}} ><br>
<input type="text" wire:model.lazy="slack_channel" id="slack_channel" class= 'form-control' placeholder="'#IT-Ops'" {{old('slack_channel', $slack_channel)}} ><br>
@endif
{!! $errors->first('slack_channel', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</div>
@ -51,16 +52,40 @@
<div class="col-md-10">
@if (config('app.lock_passwords')===true)
{{-- {{ Form::text('slack_botname', old('slack_botname', $setting->slack_botname), array('class' => 'form-control','disabled'=>'disabled','placeholder' => 'Snipe-Bot')) }}--}}
<input type="text" wire:model.defer="slack_botname" id="slack_botname" class= 'form-control' placeholder="'Snipe-Bot'" {{old('slack_botname', $slack_botname)}} ><br>
<input type="text" wire:model.lazy="slack_botname" id="slack_botname" class= 'form-control' placeholder="'Snipe-Bot'" {{old('slack_botname', $slack_botname)}} ><br>
<p class="text-warning"><i class="fas fa-lock"></i> {{ trans('general.feature_disabled') }}</p>
@else
<input type="text" wire:model.defer="slack_botname" id="slack_botname" class= 'form-control' placeholder="'Snipe-Bot'" {{old('slack_botname', $slack_botname)}} ><br>
<input type="text" wire:model.lazy="slack_botname" id="slack_botname" class= 'form-control' placeholder="'Snipe-Bot'" {{old('slack_botname', $slack_botname)}} ><br>
@endif
{!! $errors->first('slack_botname', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</div><!--col-md-10-->
</div>
<!--Slack Integration Test-->
@if($slack_endpoint != null && $slack_channel != null && $slack_botname != null)
<div class="form-group">
<div class="col-md-2">
{{ Form::label('test_slack', 'Test Slack') }}
</div>
<div class="col-md-10">
<button wire:submit.prevent="testSlack({{$slack_channel,$slack_endpoint,$slack_botname}})" id="test_slack" class="btn btn-default btn-sm pull-left"><span>{!! trans('admin/settings/general.slack_test') !!}</span></button>
<div wire:loading><i class="fas fa-spinner spin"></i></div>
@if($response == 'success')
message here
@endif
@if($response == 'error')
message here
@endif
</div>
</div>
@endif
{{-- <div class="col-md-10 col-md-offset-2">--}}
{{-- <span id="slacktesticon"></span>--}}
{{-- <span id="slacktestresult"></span>--}}
{{-- <span id="slackteststatus"></span>--}}
{{-- </div>--}}
<div class="box-footer">
<div class="text-left col-md-6">
<a class="btn btn-link text-left" href="{{ route('settings.index') }}">{{ trans('button.cancel') }}</a>

View file

@ -19,12 +19,10 @@
padding-right: 40px;
}
</style>
{{csrf_field()}}
<div class="row">
<div class="col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
<div class="panel box box-default">
<div class="box-header with-border">
<h2 class="box-title">
@ -33,15 +31,12 @@
</div>
<div class="box-body">
<p style="padding: 20px;">
{!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!}
</p>
@livewire('slack-settings-form')
</div> <!-- /.col-md-8-->
</div> <!-- /.row-->