Switch to Guzzle for the Slack test

This commit is contained in:
snipe 2020-09-15 23:05:27 -07:00
parent b1dda88c9d
commit 7736f12eb4

View file

@ -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,36 @@ class SettingsController extends Controller
return response()->json($message, 200);
}
public function slacktest()
public function slacktest(Request $request)
{
\Log::debug($request->input('slack_channel'));
\Log::debug($request->input('slack_endpoint'));
\Log::debug($request->input('slack_botname'));
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' => $request->input('slack_endpoint'),
'defaults' => [
'exceptions' => false
]
]);
\Log::debug('Preparing to test slack connection');
$payload = json_encode(
[
'channel' => $request->input('slack_channel'),
'text' => trans('general.slack_test_msg'),
'username' => $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);
}