-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (97 loc) · 3.09 KB
/
Copy pathindex.js
File metadata and controls
112 lines (97 loc) · 3.09 KB
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
const express = require('express');
const cors = require('cors')
const nodemailer = require('nodemailer');
const Ical = require('ical-generator');
const bodyParser = require('body-parser');
const app = express();
const port = 1524; // change
const Config = require('./config.js');
if (! Object.values(Config.organizer).every(x => x.length > 1)) throw 'incomplete config';
if (! Object.values(Config.attendee).every(x => x.length > 1)) throw 'incomplete config';
app.use(cors())
app.use(bodyParser.json())
app.use(express.urlencoded({ extended: true }));
const router = express.Router();
app.use(router);
const ROOT = '/spurwing-hooks'; // change
router.use((req, res, next)=>{
next()
});
router.get(ROOT + '/appointment', (req, res) => {
const To = [{
name: Config.attendee.name,
email: Config.attendee.email,
},{
name: req.query.name,
email: req.query.email,
}]
if (!req.query.name || !req.query.email) {
res.send('error: nope')
return;
}
// console.log(req.query)
let StartDate = new Date(req.query.start);
let EndDate = new Date(req.query.end);
let Summary = 'Spurwing Appointment with ' + req.query.name; // change
let Subject = Summary; // change
let Html = `Appointment with ${req.query.name} on ${req.query.start}.<br>` + Config.email.html; // change
sendMail(To, StartDate, EndDate, Subject, Html, Summary, null)
res.send({success:1})
});
app.listen(port, () => console.log(`Spurwing-hooks listening on port ${port}!`))
function getIcal(starttime, endtime, summary, description, location, url, Attendees) {
const cal = Ical({
domain: "spurwing.io", // change
name: 'Spurwing Appointment Call', // change
});
const attendees = [];
for (const at of Attendees) {
attendees.push({
mailto : at.email,
email : at.email,
name : at.name,
status : 'needs-action',
rsvp : true,
type : 'individual',
})
}
cal.createEvent({
start: starttime, end: endtime,
summary: summary,
description: description,
location: location,
url: url,
organizer: {
name: Config.organizer.name,
email: Config.organizer.email,
},
status: 'confirmed',
attendees: attendees,
});
cal.method('REQUEST');
return cal;
}
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: Config.organizer.email,
pass: Config.organizer.smtp_pass,
}
});
function sendMail(Attendees, StartDate, EndDate, Subject, Html, Summary, Description) {
let ical = getIcal(StartDate, EndDate, Summary, Description, null, null, Attendees);
const mailOptions = {
from: Config.organizer.email,
to: Attendees.map(x => x.email),
subject: Subject,
text: Html.replace(/<[^>]*>?/gm, ''),
html: Html,
icalEvent: {
content: ical.toString()
}
};
transporter.sendMail(mailOptions, function(error, info){
if (error) console.log(error);
else console.log('Email sent: ' + info.response);
});
}