-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverV1.js
152 lines (106 loc) · 3.98 KB
/
serverV1.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
var express = require('express');
var twilio = require('twilio');
var bodyParser = require('body-parser');
var fs = require('fs');
var cors = require('cors');
var opPostCount = 0;
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database("sense.db");
var app = express();
var msgArr = [];
var reqB;
var aOne = '';
var aTwo = '';
console.log(process.env.TWILIO_AUTH_TOKEN);
console.log(process.env.TWILIO_ACCOUNT_SID);
var client = new twilio.RestClient(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
app.set('port', (process.env.PORT || 5000));
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({ extended: false }));
app.use(express.static(__dirname + '/public'));
app.get('/messages', function(request, response) {
db.all("SELECT * FROM users", function(err, uRow) {
uRow.forEach(function(user){
console.log('user info is ' + user.phone);
db.all("SELECT * FROM messages WHERE phone = ?", user.phone, function(err, mRow){
if(err){ throw err;}
console.log('mRow is > ' + mRow);
user['messages'] = mRow;
})
})
// }
// var string = "";
// msgArr.forEach(function(msg){
// string += '<br>' + msg;
// })
console.log(uRow);
setTimeout(function(){response.json(uRow)},100);
});
});
app.post('/sms', twilio.webhook({
validate:false
}), function(request, response) {
var rPhone = request['body']['From'];
var rBody = request['body']['Body'];
db.get("SELECT * FROM users WHERE phone = ?", rPhone, function(err, row) {
if(row){
db.run("INSERT INTO messages (body,phone,received) VALUES(?,?,?)" , rBody, rPhone,true, function(err){});
}else{
db.run("INSERT INTO users (phone) VALUES (?)", rPhone, function(err) {
if(err) { throw err; }
// var id = this.lastID; //weird way of getting id of what you just inserted
db.run("INSERT INTO messages (body,phone,received) VALUES(?,?,?)" , rBody, rPhone,true, function(err) {
if(err) { throw err; }
});
});
}
});
msgArr.push(request['body']['Body']);
console.log(request['body']);
// Create a TwiML response
var twiml = new twilio.TwimlResponse();
twiml.message('Hello from express node.js!');
// Render the TwiML response as XML
//response.send(twiml);
});
app.post('/operator', function(request, response) {
opPostCount ++;
console.log('Operator Post Count is ' + opPostCount);
var sent = request;
var textBody = sent['body']['body'];
var n = textBody.search('\n');
if(n > 3){console.log('LINE BREAK!!!!!@@#')};
client.sms.messages.create({
to:'+' + sent['body']['phone'].toString(),
from:'2132973673',
body:sent['body']['body']
}, function(error, message) {
// The HTTP request to Twilio will run asynchronously. This callback
// function will be called when a response is received from Twilio
// The "error" variable will contain error information, if any.
// If the request was successful, this value will be "falsy"
if (!error) {
// The second argument to the callback will contain the information
// sent back by Twilio for the request. In this case, it is the
// information about the text messsage you just sent:
db.run("INSERT INTO messages (body,phone,received) VALUES(?,?,?)" , sent['body']['body'], sent['body']['phone'],false, function(err) {
if(err) { throw err; }
});
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
} else {
console.log('Oops! There was an error.');
console.log(error);
}
});
// Create a TwiML response
// Render the TwiML response as XML
// response.send(twiml);
});
// Have express create an HTTP server that will listen on port 3000
// or "process.env.PORT", if defined
app.listen(process.env.PORT || 80);
// app.listen(3000);