forked from DataMcFly/group-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (121 loc) · 3.93 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
var express = require('express');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var path = require('path');
var config = require('./config');
var app = express();
app.set('views', path.join(process.cwd(), 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
var port = process.env.PORT || 8080; // set our port
var twilio = require('twilio');
var client = twilio(config.twilio.sid, config.twilio.token );
var flybase = require('flybase');
var messagesRef = flybase.init(config.flybase.app_name, "messages", config.flybase.api_key);
var groupRef = flybase.init(config.flybase.app_name, "groups", config.flybase.api_key);
// listen for updates from Data McFly routes =========================================================
// when a new message is added to the Data McFly app, send it via Twilio...
messagesRef.on("added", function (data ){
var snapshot = data.value();
sendMessage(
snapshot.groupNumber,
snapshot.fromName,
snapshot.fromNumber,
snapshot.message
);
});
groupRef.on("added", function ( data ){
var snapshot = data.value();
var msg = snapshot.memberName + ' has joined the group';
messagesRef.push({
sid: "",
type:'',
tstamp: new Date().toLocaleString(),
fromName:"Admin",
fromNumber:"",
message:msg,
fromCity:"",
fromState:"",
fromCountry:"",
groupNumber:snapshot.groupNumber
});
});
groupRef.on("removed", function ( data ){
var snapshot = data.value();
var msg = snapshot.memberName + ' has left the group';
// send broadcast that a group member has been removed
messagesRef.push({
sid: "",
type:'',
tstamp: new Date().toLocaleString(),
fromName:"Admin",
fromNumber:"",
message:msg,
fromCity:"",
fromState:"",
fromCountry:"",
groupNumber:snapshot.groupNumber
});
});
// broadcast a message to the group
function sendMessage( group_number, from_name, from_number, message ){
var msg = from_name + ": " + message;
groupRef.where( {"memberNumber":{"$not":from_number}} ).limit(100).on( "value", function ( data ){
if( data.count() ){
data.forEach( function( snapshot ){
var member = snapshot.value();
client.sendMessage( {
to:member.memberNumber,
from:group_number,
body:msg
}, function( err, data ) {
});
});
}
});
}
// backend routes =========================================================
// listen for incoming sms messages
app.post('/message', function (request, response) {
groupRef.where( {"memberNumber":request.param('From')} ).limit(1).on( "value", function ( data ){
if( data.count() ){
data.forEach( function( snapshot ){
var member = snapshot.value();
messagesRef.push({
sid: request.param('MessageSid'),
type:'text',
tstamp: new Date().toLocaleString(),
fromName:member.memberName,
fromNumber:request.param('From'),
message:request.param('Body'),
fromCity:request.param('FromCity'),
fromState:request.param('FromState'),
fromCountry:request.param('FromCountry'),
groupNumber:request.param('To')
});
});
}
});
var resp = new twilio.TwimlResponse();
resp.message('Message received.');
response.writeHead(200, {
'Content-Type':'text/xml'
});
response.end(resp.toString());
});
// frontend routes =========================================================
// Create basic auth middleware used to authenticate all admin requests
var auth = express.basicAuth(config.un, config.pw);
// route to handle all frontend requests, with a password to protect unauthorized access....
app.get('*', auth, function(req, res) {
res.render('index', {
api_key:config.flybase.api_key,
app_name:config.flybase.app_name,
group_number:config.twilio.from_number
});
});
var server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port);
});