-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
135 lines (113 loc) · 3.5 KB
/
server.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
const http = require('http');
const https = require('https');
const express = require('express');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const bodyParser = require("body-parser");
const accountSid = 'AC656119bfe88b16fbd15c1a28d4bc92bc';
const authToken = 'c7db194a5706e18b5eee3ddafb25287d';
const client = require('twilio')(accountSid, authToken);
const app = express();
var isSMSReceived = false;
var numSMSReceived = 0;
var currSite = '';
var originPhoneNum;
var statusCode2;
var headers2;
/** bodyParser.urlencoded(options)
* Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
* and exposes the resulting object (containing the keys and values) on req.body
*/
app.use(bodyParser.urlencoded({
extended: true
}));
/**bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
app.post('/sms', (req, res) => {
if(!isSMSReceived) {
isSMSReceived = true;
}
numSMSReceived++;
currSite = req.body.Body;
originPhoneNum = req.body.From;
var body2;
var body3 = '';
var str = "empty";
if(currSite.charAt(4) == ('s'))
{
// Send a GET request to the site
https.get(currSite, (res2) => {
//console.log('statusCode:', res2.statusCode);
//console.log('headers:', res2.headers);
statusCode2 = res2.statusCode;
headers2 = res2.headers;
res2.on('data', (d) => {
//body2 += d;
//body3 = d;
body3 = d.toString();
});
}).on('error', (e) => {
console.error(e);
}).on('end', () => {
// body2 = Buffer.concat(body2).toString();
// At this point, we have the headers, method, url and body, and can now
// do whatever we need to in order to respond to this request.
});
}
else {
// Send a GET request to the site
http.get(currSite, (res2) => {
//console.log('statusCode:', res2.statusCode);
//console.log('headers:', res2.headers);
statusCode2 = res2.statusCode;
headers2 = res2.headers;
res2.on('data', (d) => {
//body2 += d;
str = d.toString();
//console.log(str);
for(var i = 0; i < 10; i++)
{
client.messages
.create({
body: 'body: ' + str.substring(i*str.length/11, (i + 1)*str.length/11),
from: '+16476916089',
to: '+16479634081'
})
.then(message => console.log(message.sid))
.done();
}
});
}).on('error', (e) => {
console.error(e);
}).on('end', () => {
// body2 = Buffer.concat(body2).toString();
// At this point, we have the headers, method, url and body, and can now
// do whatever we need to in order to respond to this request.
});
}
res.writeHead(200, {'Content-Type': 'text/xml'});
//res.end(body3.toString());
res.end(twiml.toString());
});
// a page for when this link is viewed
app.get('/sms', (req, res) => {
if(isSMSReceived)
{
res.json({
"message": "SMS Messages Received: " + currSite,
"lastSite": originPhoneNum
});
}
else {
res.json({"message": "Access Denied"});
}
});
/** production
http.createServer(app).listen(8080, () => {
console.log('Express server listening on port ' + 8080);
}); */
/** deployment */
http.createServer(app).listen(process.env.PORT || 8080, () => {
console.log('Express server listening on port ' + (process.env.PORT || 8080));
});