-
Notifications
You must be signed in to change notification settings - Fork 1
/
announcement.js
executable file
·71 lines (63 loc) · 2 KB
/
announcement.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
#!/usr/bin/env node -
const https = require('https');
const { exec } = require('child_process');
require('dotenv').config();
const URI = process.env.ANNOUNCEMENT_WEBHOOK;
const version = "1.1";
function printhelp() {
console.log("Usage: announcement.js <flags> <message, must be in \"quotes\">");
console.log("Options:");
console.log(" -h Show all flags, aka this menu");
console.log(" -v Print version to console");
console.log(" -n Do not send to wall");
console.log(" -c Do not send to wall or webhook, print to console");
}
function postToWebhook(message) {
const data = JSON.stringify({ content: message });
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(URI, options, (res) => {
console.log(`Webhook response: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`Response body: ${chunk}`);
});
});
req.on('error', (e) => {
console.error(`Error posting to webhook: ${e}`);
});
req.write(data);
req.end();
}
function postToWall(message) {
exec(`wall "${message}"`, (error, stdout, stderr) => {
if (error) {
console.error("Error posting to wall:", error);
return;
}
if (stderr) {
console.error("Error posting to wall:", stderr);
return;
}
console.log(stdout);
});
}
if (process.argv.length === 2 || process.argv[2] === "-h") {
printhelp();
} else if (process.argv[2] === "-v") {
console.log(version);
} else if (process.argv[2] === "-c") {
console.log(process.argv.slice(3).join(' '));
} else if (process.argv[2] === "-n") {
const message = process.argv.slice(3).join(' ');
postToWebhook(message);
} else {
const message = process.argv.slice(2).join(' ');
postToWebhook(message);
postToWall(message);
}