-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
161 lines (134 loc) · 4.46 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
// import dependencies
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const path = require('path');
const request = require('request');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
//Imported internal files.
const db = require('./db.js');
//Twillio
const twill = require('./twiliocalls.js');
// import external files
var secrets = require('./secrets.js'); // import API keys etc
var appID = secrets.appID;
var secret = secrets.secret;
//Share the public folder.
app.use(express.static('public'));
//Standard entry point.
app.get('/', function(req, res){
res.sendFile(path.join(__dirname+'/index.html'));
});
//Standard entry point.
app.get('/myaccount', function(req, res){
res.sendFile(path.join(__dirname+'/qrcode.html'));
});
//Twillio integration
app.post('/text/:id', function(req, res){
const id = req.params.id;
const user = Number(id);
const index = req.body.hidden;
db.getUser(user, (data) => {
//Do something with this data...
const numbers = [];
function getNumbers(data){
data.attendees.forEach(process);
function process(data, i, array) {
numbers.push(array[i].phone_number);
}
return numbers;
}
if(req.body.messageOp == 'broadcast'){
//Send JSON with name and number over.
console.log('broadcast');
const twillDataBroadcast = getNumbers(data);
twill.multiMessage(twillDataBroadcast, req.body.message);
res.sendFile(path.join(__dirname, 'sent.html'));
}
if(req.body.messageOp == 'increment'){
//Send JSON with name and number over.
console.log('increment');
const twillDataIncrement = getNumbers(data);
twill.autoMessageIncrementer(twillDataIncrement, req.body.message, 2, 1);
res.sendFile(path.join(__dirname, 'sent.html'));
}
if(req.body.messageOp == 'individual'){
console.log('individual');
console.log(data);
const number = data.attendees[index]["phone_number"];
const twillData = {
mobileNumber: number
};
twill.directMessage(twillData, req.body.message);
res.sendFile(path.join(__dirname, 'sent.html'));
}
});
});
//Follows the home page when a valid APP ID and SECRET code have been entered.
app.get('/event', function(req, res){
res.sendFile(path.join(__dirname + '/create-event.html'), (err) =>{
if(err){
console.log(err)
}
})
});
//PROFILE METHODS
app.get('/organiser', (req, res) => {
res.sendFile(__dirname + '/organiser.html', (err) => {
if(err) console.log(err)
});
});
//User Profile Information
app.get('/profile/user/:id', (req, res) => {
db.getUser(req.params.id, (data) => {
res.send(data);
});
});
//Get event attendees
app.post('/event/attendees', (req, res) => {
db.getEvent(req.body.eventID, (event) => {
res.send(event);
})
});
// "create_event" route - Bring in the app ID/secret from the Hackathon owner.
app.post('/create_event', (req, res) => {
if(req.body.appID && req.body.secret){
//Obtain the AppID and Secret from page
const appID = req.body.appID;
const secret = req.body.secret;
const options = {
method: 'GET',
url: `https://my.mlh.io/api/v2/users?client_id=${appID}&per_page=1000&secret=${secret}`
};
request(options, function(error, response, body){
if(error){
console.log(error)
}
//Convert to JSON
// const data = JSON.parse(body).data[0];
const data = JSON.parse(body).data;
//Call addUsers mongo
db.addEvent(data, appID, function(event){
const id = String(event._id);
res.redirect('organiser/?event_id=' + id);
});
});
} else if(req.body.appIDLogin) {
db.getEventByAppId(req.body.appIDLogin, function(event) {
if(event) {
res.redirect('organiser/?event_id=' + event._id);
} else {
res.status(404).send('No such event!');
}
});
}
else{
res.status(400).send('Bad Request');
console.log('bad request');
}
});
app.listen(9001, function(){
console.log('App running on 9001');
console.log('User Connected');
});