-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
104 lines (82 loc) · 3.35 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
99
100
101
102
103
104
var http = require('http');
var express = require('express');
var config = require('./web/chat/config');
var path = require('path');
var express = require('express');
var twilio = require('twilio');
var http = require('http');
var AccessToken = require('twilio').AccessToken;
var IpMessagingGrant = AccessToken.IpMessagingGrant;
var twiliAccntInfoFromFile=config.getTwiliAccountSettingsfromFile ;
if (twiliAccntInfoFromFile !="Y" )
{
console.log("Loading Configuration from environment");
// Load configuration information from system environment variables
var TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID;
var TWILIO_IPM_SERVICE_SID = process.env.TWILIO_IPM_SERVICE_SID ;
var TWILIO_IPM_API_KEY = process.env.TWILIO_IPM_API_KEY;
var TWILIO_IPM_API_SECRET = process.env.TWILIO_IPM_API_SECRET;
}
else
{
console.log("Loading Configuration from config.js");
// Load configuration information config file
var TWILIO_ACCOUNT_SID = config.accountSid;
var TWILIO_IPM_SERVICE_SID = config.serviceSid ;
var TWILIO_IPM_API_KEY = config.apiKey;
var TWILIO_IPM_API_SECRET = config.apiSecret;
}
// Create an Express web app
var app = express();
// Mount Express middleware for serving static content from the "public"
// directory
app.use(express.static(path.join(process.cwd(), 'web', 'chat', 'public')));
app.use(express.static(path.join(process.cwd(), 'web', 'chat', 'assets')));
// In production, validate that inbound requests have in fact originated
// from Twilio. In our node.js helper library, we provide Express middleware
// for this purpose. This validation will only be performed in production
if (config.nodeEnv === 'production') {
// For all webhook routes prefixed by "/twilio", apply validation
// middleware
app.use('/twilio/*', twilio.webhook(config.authToken, {
host: config.host,
protocol: 'https' // Assumes you're being safe and using SSL
}));
}
/*
Generate an Access Token for a chat application user - it generates a random
username for the client requesting a token, and takes a device ID as a query
parameter.
*/
app.get('/token', function(request, response) {
var identity = request.query.identity;
var endpointId = request.query.endpointId;
// Create a "grant" which enables a client to use IPM as a given user,
// on a given device
var ipmGrant = new IpMessagingGrant({
serviceSid: TWILIO_IPM_SERVICE_SID,
endpointId: endpointId
});
// Create an access token which we will sign and return to the client,
// containing the grant we just created
console.log(TWILIO_ACCOUNT_SID);
console.log(TWILIO_IPM_API_KEY);
console.log(TWILIO_IPM_API_SECRET);
console.log(TWILIO_IPM_SERVICE_SID);
var token = new AccessToken(TWILIO_ACCOUNT_SID, TWILIO_IPM_API_KEY, TWILIO_IPM_API_SECRET);
token.addGrant(ipmGrant);
token.identity = identity;
// Serialize the token to a JWT string and include it in a JSON response
response.send({
identity: identity,
token: token.toJwt()
});
});
app.use(express.static(__dirname + '/web'))
//routes
app.get('/', function (req, res) {
res.sendFile(path.resolve(__dirname + '/web', 'index.html'));
});
app.listen(8000, () => {
console.log('Server running on port 8000!');
});