forked from tvst/coffeebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffeebot.js
155 lines (114 loc) · 4.06 KB
/
coffeebot.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
// Get the SHEET_ID from your Google Sheet's URL:
// https://docs.google.com/spreadsheets/d/[ THIS IS THE SHEET_ID ]/edit#gid=0
// ^^^^^^^^^^^^^^^^^^^^^^^^
const SHEET_ID = 'Fill this out! See above.'
// Put your email here:
const authorEmail = '[email protected]'
// ----------------------------------------------------------------------
// Don't change anything else below.
// ----------------------------------------------------------------------
// Columns A-F in the spreadsheet should be the following:
//
// Email (required), Name, Website, LinkedIn, Twitter, Other info
//
// ...and ROW 1 is a header row, which we ignore.
const RANGE = 'Main!A2:F'
function run() {
const subscribers = getSubscribers()
Logger.log('Subscribers: %s', subscribers)
const assignments = getAssignments(subscribers)
Logger.log('Assignments: %s', assignments)
for (let assignment of assignments) {
Logger.log('Sending email for %s', assignment)
sendEmail(assignment)
}
Logger.log('Done!')
}
const BODY_TEMPLATE = ({assignment}) => (
`👋 Hello there, humans!
You ${assignment.length} have been matched to have coffee this week.
ALLOW ME TO INTRODUCE YOU:
${assignment
.map(x => x.toString())
.join('\n\n')}
NEXT STEPS:
📆 Set up a 30min coffee meeting. This could be in-person or virtual. Up to you!
🤘 Be excellent to each other and have a great time. I suggest shadow puppets. Humans enjoy that.
Cheerios! Beep bop beep.
- Coffeebot out
---
You are receiving this because you signed up for Coffeebot.
To unsubscribe, just remove your name from this Google sheet:
https://docs.google.com/spreadsheets/d/${SHEET_ID}/edit#gid=0
If you have any questions, ping my human at ${authorEmail}
`)
function sendEmail(assignment) {
const recipients = assignment.map(x => x.email).join(',')
const subject = "☕ Rejoice! You have been matched for coffee"
const body = BODY_TEMPLATE({assignment})
const options = {noReply: true}
MailApp.sendEmail(recipients, subject, body, options)
}
function getAssignments(subscribers) {
const shuffled = shuffleArray(subscribers)
const assignments = []
// Pair up every 2 consecutive people.
for (let i = 0; i < shuffled.length - 1; i += 2) {
assignments.push([shuffled[i], shuffled[i+1]])
}
// If odd number of people, add the last person (who hasn't been matched) to the last assignment.
// So sometimes there's an assignment with 3 people rather than 2.
if (shuffled.length % 2 == 1) {
if (assignments.length == 0) assignments.push([])
const lastAssignment = assignments[assignments.length - 1]
lastAssignment.push(shuffled[shuffled.length - 1])
}
return assignments
}
// Copied from: https://stackoverflow.com/a/2450976
function shuffleArray(array) {
let currentIndex = array.length
let randomIndex
// While there remain elements to shuffle.
while (currentIndex > 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]]
}
return array
}
function getSubscribers() {
try {
const values = Sheets.Spreadsheets.Values.get(SHEET_ID, RANGE).values
if (!values) return []
return values.map(x => new Subscriber(x))
} catch (err) {
return []
}
}
class Subscriber {
constructor(row) {
this.email = row[0]
this.name = row[1]
this.website = row[2]
this.linkedIn = row[3]
this.twitter = row[4]
this.other = row[5]
}
toString() {
let out = []
if (this.name) out.push(`Name: ${this.name}`)
if (this.email) out.push(`Email: ${this.email}`)
if (this.website) out.push(`Website: ${this.website}`)
if (this.linkedIn) out.push(`LinkedIn: ${this.linkedIn}`)
if (this.twitter) out.push(`Twitter: ${this.twitter}`)
if (this.other) out.push(`Other stuff: ${this.other}`)
if (out.length === 0) {
Logger.log('ERROR: Malformed Subscriber object!', this)
}
return out.join('\n')
}
}