Skip to content

Instantly share code, notes, and snippets.

@captncraig
Created April 21, 2015 22:51
Show Gist options
  • Save captncraig/2be123bff0168dc30713 to your computer and use it in GitHub Desktop.
Save captncraig/2be123bff0168dc30713 to your computer and use it in GitHub Desktop.
Slack auto inviter
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
)
var slack_team, slack_api_key, slack_channels string
func init() {
if slack_team = os.Getenv("SLACK_TEAM"); slack_team == "" {
log.Fatal("Need to set SLACK_TEAM env var with slack subdomain: (***.slack.com)")
}
if slack_api_key = os.Getenv("SLACK_KEY"); slack_api_key == "" {
log.Fatal("Need to set SLACK_KEY env var with slack api key generated via web.")
}
if slack_channels = os.Getenv("SLACK_CHANS"); slack_channels == "" {
log.Fatal("Need to set SLACK_CHANS env var. ")
}
}
func main() {
http.HandleFunc("/", sendInvite)
http.ListenAndServe(":8888", nil)
}
func sendInvite(w http.ResponseWriter, r *http.Request) {
var fname, lname, email string
if fname = r.FormValue("first_name"); fname == "" {
w.WriteHeader(400)
io.WriteString(w, "first_name required")
return
}
if lname = r.FormValue("last_name"); lname == "" {
w.WriteHeader(400)
io.WriteString(w, "last_name required")
return
}
if email = r.FormValue("email"); email == "" {
w.WriteHeader(400)
io.WriteString(w, "email required")
return
}
u := fmt.Sprintf("https://%s.slack.com/api/users.admin.invite?t=%d", slack_team, time.Now().Unix())
resp, err := http.PostForm(u,
url.Values{
"email": {email},
"channels": {slack_channels},
"first_name": {fname},
"last_name": {lname},
"token": {slack_api_key},
"set_active": {"true"},
"_attempts": {"1"},
})
if err != nil {
fmt.Println(err)
return
}
w.WriteHeader(resp.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment