-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
119 lines (91 loc) · 3.91 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
// To-do: Add chatbot functionality
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const rp = require('request-promise');
const config = require('./config');
const sgMail = require('@sendgrid/mail');
const path = require('path');
//access Sendgrid API key
sgMail.setApiKey(config.SENDGRID_API_KEY);
// Generate a JWT token to authenticate and make Zoom API calls
const payload = {
iss: config.ZOOM_API_KEY,
exp: ((new Date()).getTime() + 5000)
};
const token = jwt.sign(payload, config.ZOOM_API_SECRET);
app.use(express.static('public'));
//Landing Page -- todo: Redesign the landing page based on what we want to add.
app.get('/', (request, response) => {
response.sendFile(path.join(__dirname+'/public/index.html'));
});
// Set up a webhook listener for your Webhook Event - in this case we are listening to Webinar Ended event but you can add any events of your choice.
app.post('/', bodyParser.raw({ type: 'application/json' }), (req, res) => {
let event;
try {
event = JSON.parse(req.body);
} catch (err) {
res.status(400).send(`Webhook Error: ${err.message}`);
}
// Check to see if you received the event or not.
console.log(event)
if (req.headers.authorization === config.VERIFICATION_TOKEN) {
res.status(200);
console.log("Webinar Ended Webhook Recieved.")
res.send();
var uuid = event.payload.object.uuid;
//Double encode the uuid for validation incase it contains slashes
var euuid = encodeURIComponent(encodeURIComponent(uuid));
var options = {
uri: "https://api.zoom.us/v2/past_webinars/" + euuid + "/absentees",
auth: {
'bearer': token
},
headers: {
'User-Agent': 'Zoom-api-Jwt-Request',
'content-type': 'application/json'
},
json: true
};
rp(options)
.then(function (response) {
var myregistrantobj= response.registrants;
//console.log("Registrants:", myregistrantobj)
//fetch only the email addresses from the response and store the addresses in an array
//Either use for loop or use the map() function to store email addresses only.
var emailList = []
for (var i = 0; i < myregistrantobj.length; i++) {
//Store emails as an array of strings to match the request body for SendGrid API
emailobjs = myregistrantobj[i].email
emailList.push(emailobjs);
}
// check if the emails have been fetched or not by printing to the console
console.log(emailList);
// Call SendGrid Email API to send the email to participants. You can customize the email content as you like.
const msg = {
to: emailList,
from: '[email protected]',
subject: 'We are sorry that we missed you.',
text: 'Please, let us know if the timing of these webinars do not work for you. We hope you can join us next time.'
};
return msg;
})
.then(function(msg) {
sgMail.sendMultiple(msg);
})
.then(function(){
console.log("Email sent.")
})
.catch(function (err) {
// API call failed...
console.log('API call failed, reason ', err);
});
} else {
res.status(403).end('Access forbidden');
console.log("Invalid Post Request.")
}
});
app.listen(3000, () => {
console.log('Server is up and running on port 3000.')
})