-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedtest-alert.js
43 lines (38 loc) · 1.03 KB
/
speedtest-alert.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
#!/usr/bin/env node
const nodemailer = require('nodemailer');
const speedTest = require('speedtest-net');
const settings = require('./settings.json');
const transporter = nodemailer.createTransport({
service: settings.service,
auth: {
user: settings.email,
pass: settings.password,
}
});
const main = () => {
speedTest({ acceptLicense: true, acceptGdpr: true }).then((result) => {
const downloadMegabits = (result.download.bytes / 1000000).toFixed(0);
if (downloadMegabits < settings.threshold) {
sendSMS({
subject: 'Internet Service Alert',
body: `Download speed is ${downloadMegabits}, below threshold of ${settings.threshold}`,
});
}
});
};
const sendSMS = (sms) => {
const mailOptions = {
from: settings.email,
to: settings.receiver,
subject: sms.subject,
text: sms.body,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
};
main();