-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.js
80 lines (69 loc) · 2.17 KB
/
sms.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
require('dotenv').config();
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const twilio = require('twilio');
// const twilioClient = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.AUTH_TOKEN);
const MessagingResponse = twilio.twiml.MessagingResponse;
import { statusCode, headers, optinStatuses } from '../constants';
// Adapted from https://stackoverflow.com/a/13419367
const parseQuery = queryString => {
const query = {};
const pairs = (queryString[0] === '?'
? queryString.substr(1)
: queryString
).split('&');
pairs.forEach(pair => {
const _pair = pair.split('=');
query[decodeURIComponent(_pair[0])] = decodeURIComponent(_pair[1] || '');
});
return query;
};
exports.handler = function(event, context, callback) {
// We only care to do anything if this is our POST request
if (event.httpMethod !== 'POST' || !event.body) {
callback(null, {
statusCode,
headers,
body: 'No GET request handler',
});
return;
}
const { Body, From } = parseQuery(event.body);
const twiml = new MessagingResponse();
const theMessage = Body.toLowerCase().trim();
// Should we update the db?
let optinStatus = null;
if (theMessage === 'yes') {
twiml.message("Amazing! 🎉 You're awsome and we love you ❤ - TextJoy");
optinStatus = optinStatuses.accepted;
} else if (theMessage === 'no') {
twiml.message(
"Awwh 😥 We're sad to see you go. It's okay though, we still love you ❤ - TextJoy",
);
optinStatus = optinStatuses.declined;
} else {
twiml.message(
"Ooops 🤯 We don't know what that means! Please reply YES to opt-in or NO to opt-out - TextJoy",
);
}
if (optinStatus) {
stripe.charges.list().autoPagingEach(charge => {
// If the charge has the same phone number set all the optinStatuses
if (charge.metadata.recipientPhoneNumber === From) {
stripe.charges.update(charge.id, {
metadata: {
...charge.metadata,
optinStatus,
},
});
}
});
}
callback(null, {
statusCode,
headers: {
...headers,
'Content-Type': 'text/xml',
},
body: twiml.toString(),
});
};