-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·148 lines (133 loc) · 4.79 KB
/
index.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
require("dotenv").config();
const { v4: uuidv4 } = require("uuid");
const AccessToken = require("twilio").jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
const ChatGrant = AccessToken.ChatGrant;
const express = require("express");
const cors = require('cors')
const app = express();
const port = 5000;
app.use(cors())
// use the Express JSON middleware
app.use(express.json());
// create the twilioClient
const twilioClient = require("twilio")(
process.env.TWILIO_API_KEY_SID,
process.env.TWILIO_API_KEY_SECRET,
{ accountSid: process.env.TWILIO_ACCOUNT_SID }
);
const addParticipant = async (username, conversationSid) => {
try {
const participant = await twilioClient.conversations.conversations(conversationSid)
.participants.create({ identity: username });
console.log(`add participant ${JSON.stringify(participant)}`);
} catch (error) {
console.log(`error ${JSON.stringify(error)}`);
}
};
const createConversation = async (username, roomName) => {
if (roomName && username) {
const conversation = await twilioClient.conversations.conversations
.create({ friendlyName: roomName });
console.log(`create conversation ${JSON.stringify(conversation)}`);
const participant = await twilioClient.conversations.conversations(conversation.sid)
.participants.create({ identity: username })
console.log(`create participant ${JSON.stringify(participant)}`);
return conversation.sid;
} else {
console.log(`kosong ${JSON.stringify(roomName + ' ' +username)}`);
throw error;
}
};
const findOrCreateRoom = async (username, roomName, conversationSid) => {
try {
// see if the room exists already. If it doesn't, this will throw
// error 20404.
const room = await twilioClient.video.rooms(roomName).fetch();
console.log(`room ${JSON.stringify(room)}`);
const participant = await addParticipant(username, conversationSid);
console.log(`participant ${JSON.stringify(participant)}`);
} catch (error) {
// the room was not found, so create it
if (error.code == 20404) {
await twilioClient.video.rooms.create({
uniqueName: roomName,
type: "go",
});
return await createConversation(username, roomName);
} else {
// let other errors bubble up
throw error;
}
}
};
const getAccessToken = (roomName, username) => {
// create an access token
const token = new AccessToken(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_API_KEY_SID,
process.env.TWILIO_API_KEY_SECRET,
// generate a random unique identity for this participant
{ identity: username }
);
// create a video grant for this specific room
const videoGrant = new VideoGrant({
room: roomName,
});
const chatGrant = new ChatGrant({
serviceSid: process.env.DEFAULT_CONVERSATIONS_SERVICE_SID,
});
// add the video grant
token.addGrant(videoGrant);
token.addGrant(chatGrant);
// serialize the token and return it
return token.toJwt();
};
app.post("/join-room", async (req, res) => {
// return 400 if the request has an empty body or no roomName
if (!req.body || !req.body.roomName) {
return res.status(400).send("Must include roomName argument.");
}
const roomName = req.body.roomName;
const username = uuidv4();
const conversationSid = req.body.conversationSid;
// find or create a room with the given roomName
const conversationSidCreated = await findOrCreateRoom(username, roomName, conversationSid);
console.log('conversation', conversationSidCreated)
// generate an Access Token for a participant in this room
const token = getAccessToken(roomName, username);
res.send({
token: token,
conversationSidCreated: conversationSidCreated,
});
});
app.post("/room-complete", async (req, res) => {
// return 400 if the request has an empty body or no roomName
if (!req.body || !req.body.roomName) {
return res.status(400).send("Must include roomName argument.");
}
const roomName = req.body.roomName;
const conversationSid = req.body.conversationSid;
const room = await getRoomByUniqueName(roomName);
const status = (await completeRoom(room.sid)).status;
await deleteConversations(conversationSid);
res.send({
status: status,
});
});
const getRoomByUniqueName = async (roomName) => {
return await twilioClient.video.rooms(roomName).fetch();
}
const completeRoom = async (roomSid) => {
return await twilioClient.video.rooms(roomSid)
.update({status: 'completed'});
}
const deleteConversations = async (conversationSid) => {
console.log('conversationSid', conversationSid);
const isDeleted = await twilioClient.conversations.conversations(conversationSid).remove();
console.log('conversationSid', isDeleted);
}
// Start the Express server
app.listen(port, () => {
console.log(`Express server running on port ${port}`);
});