-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextem.js
99 lines (93 loc) · 3.32 KB
/
textem.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
var models = require('./models');
var nodemailer = require('nodemailer');
// var RSVP = require('rsvp');
// Twilio Credentials
var accountSid = 'yourtwilioinfo';
var authToken = 'yourothertwilioinfo';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken);
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
XOAuth2: {
user: "[email protected]", // Your gmail address.
// Not @developer.gserviceaccount.com
clientId: "yourgobbledygook.apps.googleusercontent.com",
clientSecret: "yourothergobbledygook",
refreshToken: "lastgobbledygook"
}
}
});
function textParticipant(participant) {
console.log('got participant', participant);
if (participant.withdrawn !== true && (!participant.hasOwnProperty('started') || models.days_between(participant.started, new Date()) < models.MAX_DAYS)) {
console.log('going to text', participant);
messageParticipant(participant);
} else {
console.log("don't text them");
}
}
function messageParticipant (participant) {
// note: the pid argument here required me to modify the qualtrics
// questionnaire to notice the parameter and autofill for the participant
// see the other js file
var link = "youqualtricslink#pid=" + participant.pid;
if (participant.link) {
console.log('participant has custom link');
link = participant.link;
}
// if (participant.withdrawn !== true) {
var greeting = '';
if (participant.hasOwnProperty('fName')) {
greeting = 'Hi '+ participant.fName + ', ';
}
greeting = greeting + 'You are participant number: ' + participant.pid + '. ';
if (participant.text) {
console.log('using sms for', participant.phone);
client.messages.create({
to: participant.phone,
from: "+YOURTWILIONUMBER",
body: greeting+"WHATEVER MESSAGE "+link,
}, function (err, message) {
console.log(message.sid);
if (err) {
console.error(err);
}
});
} else {
console.log('using email for', participant.email);
var mailOptions = {
from: "[email protected]",
to: participant.email,
subject: "WHATEVER SUBJECT",
generateTextFromHTML: true,
html: greeting+'<a href="' + link + '">Please click here to complete today\'s survey: ' + link + '</a>'
};
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
console.log("error sending a message, try once more", mailOptions, error, response);
smtpTransport.sendMail(mailOptions, function(error2, response2) {
if (error2) {
console.log('erred a 2nd time', error2, mailOptions);
} else {
console.log(response2);
}
});
} else {
console.log(response);
}
// smtpTransport.close();
});
}
// }
}
function textEmAll() {
console.log(new Date(), 'going to text all ', models.participants.length, '+ participants');
models.participants.forEach(textParticipant);
}
console.log('started textem', new Date());
var CronJob = require('cron').CronJob;
new CronJob('00 00 18 * * *', function () {
console.log('starting cronned job');
textEmAll();
}, null, true, 'America/New_York');