-
Notifications
You must be signed in to change notification settings - Fork 3
/
invite.go
120 lines (99 loc) · 2.93 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Invite
//
// Provides a HTTP handler for signing up for a Slack invite and/or
// a MailChimp mailing list signup via a JSON request.
package main
import (
"encoding/json"
"log"
"net/http"
"sync"
"github.com/codegangsta/cli"
"github.com/mnbbrown/mailchimp"
"github.com/nlopes/slack"
)
type invitationRequest struct {
Email string `json:"email"`
RequestSlack bool `json:"requestSlack"`
RequestMailingList bool `json:"requestMailingList"`
}
type invitationResponse struct {
Message string `json:"message"`
ErrorCode int `json:"errorCode,omitempty"`
}
func inviteRequestHandler(c *cli.Context) handleFunc {
return func(w http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(req.Body)
var invitation invitationRequest
err := decoder.Decode(&invitation)
if err != nil {
log.Printf("Error parsing JSON request: %s", err.Error())
jsonResponse(w, invitationResponse{
Message: "There was an error processing your invitation. Please try again later.",
ErrorCode: 1,
}, http.StatusInternalServerError)
return
}
var (
slackErr, chimpErr error
wg sync.WaitGroup
)
if invitation.RequestSlack {
wg.Add(1)
go func() {
defer wg.Done()
slackErr = inviteToSlack(c.String("slack-token"), c.String("slack-team"), "", "", invitation.Email)
}()
}
if invitation.RequestMailingList {
wg.Add(1)
go func() {
defer wg.Done()
chimpErr = subscribeToList(c.String("mailchimp-token"), invitation.Email, c.String("mailchimp-list"))
}()
}
wg.Wait()
if slackErr != nil || chimpErr != nil {
var (
message string
code int
)
if slackErr != nil {
log.Printf("Error inviting %s to slack team: %s", invitation.Email, slackErr.Error())
}
if chimpErr != nil {
log.Printf("Error adding %s to mailing list: %s", invitation.Email, chimpErr.Error())
}
if slackErr != nil && chimpErr != nil {
message = "There was an error processing your invitation. Please try again later."
code = 2
} else if slackErr != nil {
message = "There was an error processing your invitation to the Slack group. Please try again later."
code = 3
} else {
message = "There was an adding you to the mailing list. Please try again later."
code = 4
}
jsonResponse(w, invitationResponse{
Message: message,
ErrorCode: code,
}, http.StatusInternalServerError)
return
}
jsonResponse(w, invitationResponse{
Message: "Your invitation has been sent. Please check your email.",
}, http.StatusOK)
}
}
func inviteToSlack(slackToken, teamName, firstName, lastName, email string) error {
client := slack.New(slackToken)
return client.InviteToTeam(teamName, firstName, lastName, email)
}
func subscribeToList(mailchimpAPIKey, email, listID string) error {
client, err := mailchimp.NewClient(mailchimpAPIKey, nil)
if err != nil {
return err
}
_, err = client.Subscribe(email, listID)
return err
}