diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php index 5f5eecbb4..b3c91cafc 100644 --- a/app/Http/Controllers/Api/SettingsController.php +++ b/app/Http/Controllers/Api/SettingsController.php @@ -6,13 +6,13 @@ use App\Http\Controllers\Controller; use App\Http\Transformers\LoginAttemptsTransformer; use App\Models\Setting; use App\Notifications\MailTest; -use App\Notifications\SlackTest; use App\Services\LdapAd; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; +use GuzzleHttp\Client; class SettingsController extends Controller { @@ -91,24 +91,33 @@ class SettingsController extends Controller return response()->json($message, 200); } - public function slacktest() + public function slacktest(Request $request) { - if ($settings = Setting::getSettings()->slack_channel=='') { - \Log::debug('Slack is not enabled. Cannot test.'); - return response()->json(['message' => 'Slack is not enabled, cannot test.'], 400); - } + $slack = new Client([ + 'base_url' => e($request->input('slack_endpoint')), + 'defaults' => [ + 'exceptions' => false + ] + ]); - \Log::debug('Preparing to test slack connection'); + + $payload = json_encode( + [ + 'channel' => e($request->input('slack_channel')), + 'text' => trans('general.slack_test_msg'), + 'username' => e($request->input('slack_botname')), + 'icon_emoji' => ':heart:' + ]); try { - Notification::send($settings = Setting::getSettings(), new SlackTest()); + $slack->post($request->input('slack_endpoint'),['body' => $payload]); return response()->json(['message' => 'Success'], 200); } catch (\Exception $e) { - \Log::debug('Slack connection failed'); - return response()->json(['message' => $e->getMessage()], 400); + return response()->json(['message' => 'Oops! Please check the channel name and webhook endpoint URL. Slack responded with: '.$e->getMessage()], 400); } + return response()->json(['message' => 'Something went wrong :( '], 400); } diff --git a/app/Notifications/SlackTest.php b/app/Notifications/SlackTest.php deleted file mode 100644 index 99d37761b..000000000 --- a/app/Notifications/SlackTest.php +++ /dev/null @@ -1,51 +0,0 @@ -from($settings->slack_botname, ':heart:') - ->to($settings->slack_channel) - ->image('https://snipeitapp.com/favicon.ico') - ->content('Oh hai! Looks like your Slack integration with Snipe-IT is working!'); - } - -} diff --git a/resources/lang/en/admin/settings/general.php b/resources/lang/en/admin/settings/general.php index 6f97789cd..6de059ba7 100644 --- a/resources/lang/en/admin/settings/general.php +++ b/resources/lang/en/admin/settings/general.php @@ -150,7 +150,7 @@ return array( 'slack_channel' => 'Slack Channel', 'slack_endpoint' => 'Slack Endpoint', 'slack_integration' => 'Slack Settings', - 'slack_integration_help' => 'Slack integration is optional, however the endpoint and channel are required if you wish to use it. To configure Slack integration, you must first create an incoming webhook on your Slack account.', + 'slack_integration_help' => 'Slack integration is optional, however the endpoint and channel are required if you wish to use it. To configure Slack integration, you must first create an incoming webhook on your Slack account. Click on the Test Slack Integration button to confirm your settings are correct before saving. ', 'slack_integration_help_button' => 'Once you have saved your Slack information, a test button will appear.', 'slack_test_help' => 'Test whether your Slack integration is configured correctly. YOU MUST SAVE YOUR UPDATED SLACK SETTINGS FIRST.', 'snipe_version' => 'Snipe-IT version', diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index ee7287515..8b60aa7d7 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -202,6 +202,7 @@ 'sign_in' => 'Sign in', 'signature' => 'Signature', 'skin' => 'Skin', + 'slack_test_msg' => 'Oh hai! Looks like your Slack integration with Snipe-IT is working!', 'some_features_disabled' => 'DEMO MODE: Some features are disabled for this installation.', 'site_name' => 'Site Name', 'state' => 'State', diff --git a/resources/views/settings/slack.blade.php b/resources/views/settings/slack.blade.php index 6ad51eef5..7afbd46ac 100644 --- a/resources/views/settings/slack.blade.php +++ b/resources/views/settings/slack.blade.php @@ -47,30 +47,30 @@
-{{ trans('admin/settings/general.slack_test_help') }}
-