-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwilioFuncs.js
74 lines (58 loc) · 1.93 KB
/
twilioFuncs.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
var env = require('./env.json');
module.exports.sendMessages = function(toArray, message, callback) {
var twilioNumber = env.TWILIO_PHONE_NUMBER;
var client = require('twilio')(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN);
var async = require('async');
var result = {
count: 0,
errorCount: 0,
errors: []
};
var sendMessage = function (phone, done) {
//console.log("This is where I would send \n"+req.body.message+"\nto: "+task.name+"\nIn "+task.county+" county");
result.count++;
var msgObj = {
to: phone,
from: twilioNumber,
body: message
};
console.log(msgObj);
client.sendSms(msgObj, function(err, message) {
if(err) {
console.error('Error sending:');
console.error(err);
result.errorCount++;
result.errors.push(err);
return done();
}
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
done();
});
};
async.eachLimit(toArray, 25, sendMessage, function (err) {
if (err) {
return callback(err);
}
callback(null, result);
});
};
module.exports.smsToEmail = function(req, res) {
if (req.body.From === undefined || req.body.Body === undefined) {
return console.error("POST payload malformed");
}
// var phone = req.body.From.substring(1,12);
console.log(req.body);
var sendgrid = require('sendgrid')(env.SENDGRID_API_KEY);
var email = new sendgrid.Email();
email.addTo(env.SEND_REPLIES_TO_EMAIL);
email.setFrom('[email protected]');
email.setSubject('SMS from ' + req.body.From);
email.setHtml('<b>SMS from ' + req.body.From + ':</b><br>\n' + req.body.Body);
sendgrid.send(email);
console.log(`email sent to ${env.SEND_REPLIES_TO_EMAIL}`);
res.type('text/xml');
res.end('<?xml version="1.0" encoding="UTF-8"?><Response></Response>');
};