Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Forked from ardeay/slack-auto-invite.php
Last active March 8, 2018 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joubertredrat/b85554802ddb88687f402ba9827a9f70 to your computer and use it in GitHub Desktop.
Save joubertredrat/b85554802ddb88687f402ba9827a9f70 to your computer and use it in GitHub Desktop.
<?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