-
Notifications
You must be signed in to change notification settings - Fork 3
/
invite.go
93 lines (76 loc) · 2.08 KB
/
invite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// package main
package slack_auto_invites
import (
"appengine"
"appengine/urlfetch"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type Configuration struct {
BaseUrl string
Token string
}
func importConfiguration() (string, string) {
file, _ := os.Open("conf.json")
decoder := json.NewDecoder(file)
configuration := Configuration{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
return configuration.BaseUrl, configuration.Token
}
func SetSlackToken(req *http.Request, token string) {
q := req.URL.Query()
q.Set("token", token)
req.URL.RawQuery = q.Encode()
}
func SetFormValues(req *http.Request, fname string, lname string, email string) {
q := req.URL.Query()
q.Set("first_name", fname)
q.Set("last_name", lname)
q.Set("email", email)
q.Set("set_active", "true")
q.Set("_attempts", "1")
req.URL.RawQuery = q.Encode()
}
func SendInvite(r *http.Request, fname string, lname string, email string) string {
// client := &http.Client{}
c := appengine.NewContext(r)
client := urlfetch.Client(c)
baseUrl, token := importConfiguration()
req, _ := http.NewRequest("POST", baseUrl, nil)
SetSlackToken(req, token)
SetFormValues(req, fname, lname, email)
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request error is: ", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Reading of response error is: ", err)
}
return string(body)
// TODO - add error checking for body of response
// success: {"ok":true}
// failure: {"ok":false,"error":"already_in_team"}
}
func inviteHandler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("fname") != "" && r.FormValue("lname") != "" && r.FormValue("email") != "" {
fname := r.FormValue("fname")
lname := r.FormValue("lname")
email := r.FormValue("email")
slack_resp := SendInvite(r, fname, lname, email)
fmt.Fprintf(w, slack_resp)
} else {
fmt.Fprintf(w, "No form values given. Please supply first name, last name, and email.")
}
}
func init() {
// func main() {
http.HandleFunc("/", inviteHandler)
}