forked from Reneehsu/Community-Text-at-Spectra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
237 lines (220 loc) · 7.63 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//require necessary modules
//test////
var express = require('express')
var mongoose = require('mongoose')
var bodyParser = require('body-parser')
var exphbs = require('express-handlebars');
var client = require('twilio')(process.env.TWILIO_SID, process.env.TWILIO_AUTH_TOKEN);
var cronJob = require('cron').CronJob;
//setup mongoose connection
mongoose.connection.on('error', function() {
console.log('error connecting to database')
})
mongoose.connection.on('connected', function() {
console.log('succesfully connected to database')
})
mongoose.connect(process.env.MONGODB_URI)
var models = require('./models/model');
var User = models.User;
var Community = models.Community;
//setup application configurations
var app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// var communityArr = ["Gratitude","Health/Fitness","Education","Empowerment"];
// var questionArr = [ "What is one thing you are grateful for that happened today?",
// "What is one healthy choice you made today","What is one thing you learned today?",
// "What is one thing you are proud of today?"]
//
// var responses = ["My family", "I made my 10,000 step count!", "All about iOS", "Completing my first hackathon"]
//
// for (var i=0; i<communityArr.length; i++){
// var newCommunity = new Community({
// name: communityArr[i],
// number: i+1,
// question: questionArr[i]
// }); //
// newCommunity.save(function(err){
// if (err) console.log('Error saving community');
// })
// }
// var textJob = new cronJob('* * * * *',function*(){
// var date = new Date();
// console.log('cronJob ' + date);
// client.messages.create( { to:'+13109233881', from:'+14245238634', body:'Hello! Hope you’re having a good day.'}, function( err, data ) {
// console.log( data.body );
// });
// })
/*Community.findOne({question: ---}, function(err, theCommunity) {
theCommunity.responses.push({
user:
})
})*/
//ROUTES GO HERE
app.get('/', function(req, res) {
res.send("GOT /");
})
app.post('/handletext',function(req,res){
res.writeHead(200,{'Content-Type': 'text/xml'});
// console.log(req.body);
var content;
User.findOne({phoneNumber:req.body.From}, function(err, theUser){
if (theUser){
if (req.body.Body.substr(0, 4) === 'JOIN'){
console.log("body " + req.body.Body);
var comm = req.body.Body.substr(5).split(' ');
console.log(comm);
theUser.community = theUser.community.concat(comm);
content = "You just joined communities " + req.body.Body.substr(5);
var communityString = '';
for (var i = 0; i < comm.length; i++) {
var num = parseInt(comm[i]);
Community.findOne({number: num}, function(err, theCommunity) {
if (err){
console.log(err);
}
// console.log("the community is " + theCommunity);
theCommunity.users.push(theUser.phoneNumber);
// communityString += theCommunity.name;
// console.log("the community name is " + theCommunity.name);
theCommunity.save(function(err) {
if (err) {
console.log(err);
}
})
});
content += " ";
}
theUser.save(function(err) {
if (err) {
console.log(err);
}
});
client.messages.create({
to: req.body.From,
from: '+14245238634',
body: content,
});
} else {
if (req.body.Body.substr(0,1) === "1"){
Community.findOne({number:1},function(err,theCommunity){
theCommunity.responses.push({
user: req.body.From,
response: req.body.Body.substr(2)
})
theCommunity.save();
content = theCommunity.responses[0].response;
client.messages.create({
to: req.body.From,
from: '+14245238634',
body: "Someone else is grateful for " + content + ' View more: https://communitytext.herokuapp.com/',
});
})
} else if (req.body.Body.substr(0,1) === "2"){
Community.findOne({number:2},function(err,theCommunity){
theCommunity.responses.push({
user: req.body.From,
response: req.body.Body.substr(2)
})
theCommunity.save();
content = theCommunity.responses[0].response;
client.messages.create({
to: req.body.From,
from: '+14245238634',
body: "Someone else said : " + content + ' View more: https://communitytext.herokuapp.com/',
});
})
} else if (req.body.Body.substr(0,1) === "3"){
Community.findOne({number:3},function(err,theCommunity){
theCommunity.responses.push({
user: req.body.From,
response: req.body.Body.substr(2)
})
theCommunity.save();
content = theCommunity.responses[0].response;
client.messages.create({
to: req.body.From,
from: '+14245238634',
body: "Someone else said : " + content + ' View more: https://communitytext.herokuapp.com/',
});
})
} else if (req.body.Body.substr(0,1) === "4"){
Community.findOne({number:4},function(err,theCommunity){
theCommunity.responses.push({
user: req.body.From,
response: req.body.Body.substr(2)
})
theCommunity.save();
content = theCommunity.responses[0].response;
client.messages.create({
to: req.body.From,
from: '+14245238634',
body: "Someone else said : " + content + ' View more: https://communitytext.herokuapp.com/',
});
})
}
}
} else {
content = "Welcome to community text!! These are the communities you can join: 1. Gratitude 2. Health/Fitness 3. Education 4. Empowerment -- Reply with 'JOIN' plus the number(s) of the communities you want to join.";
client.messages.create({
to: req.body.From,
from: '+14245238634',
body: content,
});
var newUser = new User({
phoneNumber: req.body.From,
zipCode: req.body.FromZip
});
newUser.save(function(err) {
if (err) {
console.log("Error saving user");
}
});
}
});
//initial text to our app
// if (req.body.Body === "Hello"){
// content = "Welcome to community text!! What's one thing that you grateful today?";
// } else {
// content = "Awesome! Here's another person gratitude: ...";
// }
//
// //create a User with their phone number in the database
// var newUser = new User({
// phoneNumber: req.body.From
// });
//
// newUser.save(function(err) {
// if (err) {
// console.log("Error saving user");
// }
// });
//our response to the user
// client.messages.create({
// to: req.body.From,
// from: '+14245238634',
// body: content,
// });
res.end();
});
app.get('/community',function(req, res){
Community.find(function(err,communities){
if (err){
res.send(err);
} else {
res.json(communities);
}
})
})
//add a route that will respond to post requests sent by Twilio via
//webhooks
//start up our server
var port = process.env.PORT || 3000
app.listen(port)