-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
167 lines (144 loc) · 3.74 KB
/
app.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
var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var _ = require('underscore');
var sanitizeHtml = require('sanitize-html');
var bodyParser = require('body-parser');
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
var redis = require('redis');
var redisClient = redis.createClient(process.env.REDIS_URL);
var twilioClient = require('twilio')(process.env.TWILIO_SID, process.env.TWILIO_AUTH_TOKEN);
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/*
Constants for minifying the HTML
*/
const shortenedExpressions = {
'the': 't',
'and': '&',
'that': 'h',
'with': 'w',
};
const shortenedHTML = {
'<input': 'Ξ',
'<a href="': 'Φ',
'</a>': 'Ω',
'<form>': 'Ψ',
'</form>': 'Θ',
'">': 'Σ',
'>': 'Π',
'="submit" ': 'Γ',
'="hidden" ': 'ß',
'name="': 'æ',
'type="': 'Δ',
'value="': '_'
};
const urlsWeDontWant = [
'policies', 'signin', 'preferences',
'login', 'signout'
];
/*
Helper functions
*/
const shortenText = (text) => {
if (text) {
_.mapObject(shortenedExpressions, (word, short) => {
text = text.replace(`/${word}/ig`, short);
});
}
return text;
};
const urlToSymbol = (msgId, url) => {
if (url && _.every(urlsWeDontWant, (notNecessary) => url.indexOf(notNecessary) < 0)) {
var urlPlaceholder = Math.random().toString(36).substr(2, 3);
redisClient.set(`${msgId}_${urlPlaceholder}`, url);
return urlPlaceholder;
}
return '';
};
const transformTags = (msgId) => {
return {
'a': (tagName, attribs) => {
return {
tagName: tagName,
text: shortenText(attribs.text),
attribs: {
href: urlToSymbol(msgId, attribs.href)
}
}
},
'input': (tagName, attribs) => {
var newAttribs = {
type: attribs.type || 'text'
};
if (attribs.type == 'submit') {
newAttribs.value = shortenText(attribs.value);
} else {
newAttribs.name = attribs.name;
}
return {
tagName: tagName,
attribs: newAttribs
}
}
}
};
/*
App time!!
*/
app.get('', (req, res) => {
res.send('Hello!');
});
app.all('/get', (req, res) => {
const url = `https://${req.body.Body}`;
const msgId = req.body.MessageSid;
request(url, (error, response, html) => {
if (error) return;
try {
var $ = cheerio.load(html); // Load HTML to extract the body like we would in jQuery
// Get rid of all the tags and attributes we don't want
var clean = sanitizeHtml($('body').html(), {
allowedTags: ['a', 'input', 'form'],
transformTags: transformTags(msgId),
textFilter: shortenText,
allowedAttributes: {
input: ['value', 'type', 'name'],
a: ['href']
},
exclusiveFilter: (frame) => {
// Ignore hidden inputs and anchors that go nowhere
return (frame.tag === 'input' && frame.attribs.type === 'hidden') ||
(frame.tag === 'a' && frame.attribs && !frame.attribs.href);
},
});
// Eliminate all spaces between elements
clean = clean.replace(new RegExp('\>[ ]+\<', 'g'), '><');
// Minify all the remaining tags and attributes
_.mapObject(shortenedHTML, (short, tag) => {
clean = clean.replace(new RegExp(tag, 'g'), short);
});
const chunks = clean.trim().match(/.{1,1595}/g).slice(0, 2); // Divide HTML into the max sized SMS - 5
chunks.map((chunk, index) => {
twilioClient.messages.create({
body: `${index+1}/${chunks.length} ${chunk}`,
from: process.env.TWILIO_NUMBER,
to: req.body.From,
});
});
} catch(error) {
twilioClient.messages.create({
body: `Sorry, something went wrong! Here's the error: ${error}`,
from: process.env.TWILIO_NUMBER,
to: req.body.From,
});
}
res.send(clean);
});
});
app.listen(process.env.PORT || '8081');
exports = module.exports = app;