-
-
Save joubertredrat/b85554802ddb88687f402ba9827a9f70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Define your slack group | |
*/ | |
$slackGroup = ''; | |
/** | |
* Define your slack token | |
* @see https://api.slack.com/docs/oauth-test-tokens | |
*/ | |
$slackToken = ''; | |
/** | |
* Slack url data | |
*/ | |
$slackUrlPrefix = 'https://'; | |
$slackUrlSufix = '.slack.com'; | |
$slackUrlPath = '/api/users.admin.invite?t=1m'; | |
try { | |
if (empty($slackGroup) || empty($slackToken)) { | |
throw new \ErrorException('Script not configured yet'); | |
} | |
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL); | |
if (!$email) { | |
throw new \InvalidArgumentException('Invalid e-mail'); | |
} | |
$url = array_reduce( | |
[$slackUrlPrefix, $slackGroup, $slackUrlSufix, $slackUrlPath], | |
function ($carry, $item) { | |
$carry .= $item; | |
return $carry; | |
} | |
); | |
$formParams = [ | |
'email' => $email, | |
'channels' => '', | |
'first_name' => '', | |
'token' => $slackToken, | |
'set_active' => 'true', | |
'_attempts' => '1', | |
]; | |
array_walk($formParams, function (&$item, $key) { | |
$item = $key . '=' . $item; | |
}); | |
$formData = implode("&", $formParams); | |
$curlHandler = curl_init(); | |
curl_setopt($curlHandler, CURLOPT_URL, $url); | |
curl_setopt($curlHandler, CURLOPT_POST, count($formParams)); | |
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $formData); | |
curl_exec($curlHandler); | |
curl_close($curlHandler); | |
$response = 'Convite gerado com sucesso, confira seu e-mail'; | |
} catch (\ErrorException $e) { | |
$response = 'O registro para este grupo não está ativo no momento'; | |
} catch (\InvalidArgumentException $i) { | |
$response = 'E-mail inválido'; | |
} catch (\Throwable $z) { | |
$response = 'Algo de muito errado aconteceu'; | |
} finally { | |
echo $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment