-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (84 loc) · 2.99 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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').load();
}
var express = require('express');
var app = express();
var twilio = require('twilio');
var bodyParser = require('body-parser');
var path = require('path');
var cors = require('cors');
var firebase = require('firebase');
firebase.initializeApp({
databaseURL: "https://chat-dashboard-5dbff.firebaseio.com",
//serviceAccount: 'myapp-13ad200fc320.json', //this is file that I downloaded from Firebase Console
});
const accountSid = process.env.FILLIPI;
const authToken = process.env.AUTH_KEY;
var client = new twilio(accountSid, authToken);
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());
app.use(cors());
app.get('/', function (req, res) {
res.send('wassup Woooooorld');
});
app.post('/status', function (req, res) {
res.send('Status URL');
});
app.get('/records', function (req, res) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080");
res.header("Access-Control-Allow-Headers");
var response = {person : 'meek'};
res.json(response)
});
app.post('/sendsms', function (req, res) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080");
res.header("Access-Control-Allow-Headers");
console.log(req.body);
client.messages.create({
body: req.body.text,
to: req.body.phone,
from: '+15103437234',
})
.then((message) => console.log(message.status))
.done();
});
app.post('/sms', function (req, res) {
console.log('suckaaaa')
let incomingNum = req.body.From;
var newMessageData = {
phone: incomingNum,
timestamp: Date.now(),
message: req.body.Body
};
var ref = firebase.database().ref('messages/all');
ref.child(incomingNum).once("value", snapshot => {
//if the number isnt in the database then add it
// if(!(snapshot.exists())){
// //set the incmoming number key first
// ref.child(incomingNum).set(0);
// }
//add the message
var newMessageKey = ref.child(incomingNum).push().key;
var updates = {};
updates['messages/' + incomingNum + '/all/' + newMessageKey] = newMessageData;
firebase.database().ref().update(updates);
});
// let textRef = firebase.database().ref('customers').on("value", function(snapshot) {
// userArr = Object.keys(snapshot.val()).map(x => snapshot.val()[x]);
// console.log(userArr)
// const chatId = (userArr[0].chatId)
// const inText = firebase.database().ref(`messages(trial)/${chatId}`);
// const item = {
// phone:req.body.From,
// Text: req.body.Body,
// }
// inText.push(item);
// res.end();
// });
//console.log(req.body);
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Server listening at http://%s:%s", host, port)
});