Created
October 23, 2015 18:09
-
-
Save chocolatkey/8a9696517edcee0cfd4b to your computer and use it in GitHub Desktop.
Slack team invite sign-up form handler
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 | |
/* | |
Slack team invite sign-up form handler | |
Henry Stark 2015 | |
Original Idea: https://levels.io/slack-typeform-auto-invite-sign-ups/ | |
*/ | |
/* VARIABLES TO EDIT */ | |
$slackTeam = 'scanlation';// Slack team name | |
$slackToken = 'xoxp-1234';// Slack API token (Generate at https://api.slack.com/web) | |
$slackJoinChannels = 'C0D2ET69W';// Separate IDs with a comma | |
$usedEmailsFile = $_SERVER["DOCUMENT_ROOT"].'/sho/prevEmails.json';// File for used emails | |
$formName = 'name';// Form name field name var | |
$formEmail = 'email';// Form email field name var | |
/* END VARIABLES */ | |
mb_internal_encoding("UTF-8"); | |
if(isset($_POST[$formName]) && isset($_POST[$formEmail])){ | |
$name = htmlspecialchars($_POST[$formName]); | |
$email = htmlspecialchars($_POST[$formEmail]); | |
// Load used emails file | |
if(@!file_get_contents($usedEmailsFile)) { | |
$usedEmails = array(); | |
} else { | |
$usedEmails = json_decode(file_get_contents($usedEmailsFile),true); | |
} | |
$offset = count($usedEmails); | |
if(!in_array($email, $usedEmails)) {// If account has not been registered via form | |
$slackInviteUrl = 'https://'.$slackTeam.'.slack.com/api/users.admin.invite?t='.time(); | |
// URL fields | |
$fields = array( | |
'email' => urlencode($email), | |
'channels' => urlencode($slackJoinChannels), | |
'first_name' => urlencode($name), | |
'token' => $slackToken, | |
'set_active' => urlencode('true'), | |
'_attempts' => '1' | |
); | |
// Convert to url | |
$fields_string=''; | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string, '&'); | |
// POST connection to Slack API | |
$ch = curl_init(); | |
curl_setopt($ch,CURLOPT_URL, $slackInviteUrl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch,CURLOPT_POST, count($fields)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); | |
$replyRaw = curl_exec($ch); | |
$reply = json_decode($replyRaw,true); | |
if($reply['ok'] == false) {// Success | |
echo('Error regitstering: '.$reply['error']); | |
} | |
else {// Error posting | |
echo('Thank you for your signing up, '.$name.'!. You have been added to the <a href="//'.$slackTeam.'.slack.com">'.$slackTeam.'</a> Slack team.<br/>Check your email ('.$email.') for an invite link.'); | |
} | |
curl_close($ch); | |
// Save email in used emails file | |
array_push($usedEmails, $email); | |
file_put_contents($usedEmailsFile, json_encode($usedEmails)); | |
} else {// Error email in use | |
echo('Error: The email '.$email.' has already been used to sign up in the team <a href="//'.$slackTeam.'">'.$slackTeam.'</a>.'); | |
} | |
} else {// Error name or email values not set | |
echo('Error: Missing data. Please (re)submit the form.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you gael! I'll change this soon