-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
113 lines (83 loc) · 2.84 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
105
106
107
108
109
110
111
112
require('dotenv').config()
const { connect, createLocalTracks, createLocalVideoTrack } = require("twilio-video");
const express = require("express");
const pino = require('pino');
const expressPino = require('express-pino-logger');
const sgMail = require('@sendgrid/mail');
const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
const expressLogger = expressPino({ logger });
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressLogger);
const port = process.env.PORT || 4000;
app.use(express.static(path.join(__dirname, 'build')));
const AccessToken = require('twilio').jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
app.listen(port, () => {
logger.info('Server running on port %d', port);
})
app.get('/api/greeting', (req, res) => {
const name = req.query.name || 'World';
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ greeting: `Hello ${name}!` }));
});
let roomObject;
app.post("/api/generate-token", (req, res) => {
let userIdentity = req.body.body.identity;
let room = req.body.body.room;
const accessToken = new AccessToken(
process.env.ACCOUNT_SID,
process.env.TWILIO_API_SID,
process.env.TWILIO_API_SECRET
);
// Set the Identity of this token
accessToken.identity = userIdentity
// Grant access to Video
const grant = new VideoGrant();
console.log("BEFOIRE GRANT =>", grant)
grant.room = room;
console.log("AFTER GRANT =>", grant)
accessToken.addGrant(grant);
// Serialize the token as a JWT
const jwt = accessToken.toJwt();
res.send({
identity: userIdentity,
room: room,
token: jwt
});
})
app.post("/api/email-invite", async (req, res) => {
console.log("")
console.log("")
console.log("HIT THAT API")
// room: {
// state: 'connected',
// sid: 'RM021a83946e3e1fe7925c53b288347956',
// participants: {},
// name: 'a',
// localParticipant: [Object],
// isRecording: false,
// dominantSpeaker: null
// }
let emailObj = req.body.body.emails
let emails = emailObj[emailObj.length - 1].split(",")
let roomInfo = req.body.body.room
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
for (let i = 0; i < emails.length; i++) {
const msg = {
to: `${emails[i]}`,
from: 'the_devil@hell.org',
subject: "We're waiting for you",
text: `Yo! We are in room ${roomInfo.name}`,
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
try {
await sgMail.send(msg);
} catch (e) {
console.log("Sendgrid Email error => ", e)
}
}
})