From e3144c30935b3783158602d02e59476c32112fb2 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 22 Feb 2018 16:35:34 -0800 Subject: [PATCH] Added Slack test button --- .../Controllers/Api/SettingsController.php | 25 +++++ app/Notifications/SlackTest.php | 77 +++++++++++++ resources/lang/en/admin/settings/general.php | 3 +- resources/views/settings/slack.blade.php | 106 +++++++++++++++++- routes/api.php | 5 + 5 files changed, 210 insertions(+), 6 deletions(-) create mode 100644 app/Notifications/SlackTest.php diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php index 3172243894..942c4a5fe4 100644 --- a/app/Http/Controllers/Api/SettingsController.php +++ b/app/Http/Controllers/Api/SettingsController.php @@ -8,6 +8,8 @@ use App\Models\Ldap; use Validator; use App\Models\Setting; use Mail; +use App\Notifications\SlackTest; +use Notification; class SettingsController extends Controller { @@ -96,6 +98,29 @@ class SettingsController extends Controller } + + public function slacktest() + { + + 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); + } + + \Log::debug('Preparing to test slack connection'); + + try { + Notification::send($settings = Setting::getSettings(), new SlackTest()); + return response()->json(['message' => 'Success'], 200); + } catch (\Exception $e) { + \Log::debug('Slack connection failed'); + return response()->json(['message' => $e->getMessage()], 400); + } + + + } + + /** * Test the email configuration * diff --git a/app/Notifications/SlackTest.php b/app/Notifications/SlackTest.php new file mode 100644 index 0000000000..213b21315e --- /dev/null +++ b/app/Notifications/SlackTest.php @@ -0,0 +1,77 @@ +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!'); + } + + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/resources/lang/en/admin/settings/general.php b/resources/lang/en/admin/settings/general.php index c717910dd1..d544657dff 100644 --- a/resources/lang/en/admin/settings/general.php +++ b/resources/lang/en/admin/settings/general.php @@ -105,7 +105,8 @@ 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. 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', 'support_footer' => 'Support Footer Links ', 'support_footer_help' => 'Specify who sees the links to the Snipe-IT Support info and Users Manual', diff --git a/resources/views/settings/slack.blade.php b/resources/views/settings/slack.blade.php index 7aded72be1..3647934aa3 100644 --- a/resources/views/settings/slack.blade.php +++ b/resources/views/settings/slack.blade.php @@ -38,9 +38,14 @@
-
+

+ {!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!} +

+ + + +
-

{!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!}

@@ -87,10 +92,26 @@
+ @if (($setting->slack_channel!='') && ($setting->slack_endpoint)) +
+
+ {{ Form::label('est_slack', 'Test Slack') }} +
+ +
+ + + +
+
+

{{ trans('admin/settings/general.slack_test_help') }}

+
- -
- +
+ @endif +