-
Notifications
You must be signed in to change notification settings - Fork 11
/
code.js
184 lines (154 loc) · 4.45 KB
/
code.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function getMyHost() {
// TODO: put your slack domain here
return "https://phillydev.slack.com";
}
function getToken() {
// TODO: PUT YOUR TOKEN HERE
var token = 'fill_in_your_api_token';
return token;
}
function getSignupChannel() {
// TODO: PUT THE CHANNEL YOU WANT TO SEND UPDATES INTO HERE
// Look up the channel id at https://api.slack.com/methods/channels.info/test
return '#C025D8NV4';
}
function getInviteToChannels() {
// TODO: Put the channel id you want to invite someone into here
// You can look up the channel id at https://api.slack.com/methods/channels.info/test
return "C025D8NV4";
}
/**
* Retrieves all the rows in the active spreadsheet that contain data
* When going through the rows if there no value in 'invited by' then
* sends the email field to the slack invite service.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
// Sheet is a forms response sheet with 3 columns
// 'Timestamp' 'What is your email address?' 'Invited by'
Logger.log('Looking for rows with email but no invited by...');
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
var email = row[1];
var invited = row[2];
if (!invited && email) {
Logger.log('Inviting email=' + email);
var result = invite(email);
Logger.log(result);
sheet.getRange(i + 1, 3).setValue(result || 'scriptbot');
SpreadsheetApp.flush();
}
}
return;
}
/**
* obscure the full email
*/
function hideEmail(email) {
return email.replace(/(.+)@.+$/, '$1@redacted');
}
/**
* Tell the signupform channel you invited someone. This is a provides a backup
* if signup via API dies.
*/
function sayInvited(email, inviteResponse) {
var message;
var options;
var payload = getPayload();
var result;
var time = Math.ceil(new Date().getTime() / 1000);
var url = getMyHost() + '/api/chat.postMessage?t=' + time;
if (!payload) {
return;
}
if (inviteResponse.ok === true) {
message = hideEmail(email) + ', Invited Successfully';
} else {
message = 'Error Inviting: ' +
hideEmail(email) + ' Error:' + inviteResponse.error;
}
payload.channel = getSignupChannel();
payload.text = message;
payload.username = 'scriptbot';
options = {
'method' : 'POST',
'payload' : payload,
'followRedirects' : true,
'muteHttpExceptions': true
};
result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
Logger.log(result);
} else {
Logger.log('exception');
Logger.log(result);
}
return;
}
/**
* Return a payload object with the basic required information.
*/
function getPayload() {
var payload;
var token = getToken();
if (token == 'fill_in_your_api_token') {
Logger.log('You have to fill in your api token');
return;
}
payload = {
'token' : token,
'type' : 'post'
};
return payload;
}
/**
* Sends the email to the slack invite endpoint. You need to fill in your api token
* and the channels you want the user to be added to.
*
*/
function invite(email) {
var options;
var payload = getPayload();
var result;
var time = Math.ceil(new Date().getTime() / 1000);
var url = getMyHost() + '/api/users.admin.invite?t=' + time;
if (payload === undefined || payload === null) {
return;
}
payload.email = email;
payload.channels = getInviteToChannels();
payload.set_active = 'true';
payload._attempts = '1';
options = {
'method' : 'POST',
'payload' : payload,
'followRedirects' : true,
'muteHttpExceptions': true
};
result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
sayInvited(email, JSON.parse(result));
}
return result;
}
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : 'Invite to Slack',
functionName : 'readRows'
}];
spreadsheet.addMenu('Script Center Menu', entries);
}