-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.js
executable file
·48 lines (40 loc) · 1.33 KB
/
client.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
#! /usr/bin/env node
const packageJson = require('./package.json');
const program = require('commander');
const https = require('https');
const fs = require('fs');
const url = require('url');
const http = require('http');
program.version(packageJson.version);
// Example usage:
// ./client.js notify http://localhost:4000 a81123751fa9219ce873aa403c9d458dd32eb4cb991350324e004a06b9c87461 clientID '{"title":"A nice title.","body":"A nice body."}'
program.command('notify [mercuriusURL] [token] [payload]')
.description('send a notification')
.action(function(mercuriusURL, token, client, payload) {
var payload_string = JSON.stringify({
token: token,
client: client,
payload: JSON.parse(payload),
ttl: 200,
});
var urlParts = url.parse(url.resolve(mercuriusURL, 'notify'));
var options = {
hostname: urlParts.hostname,
port: urlParts.port,
path: urlParts.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': payload_string.length,
}
};
var request = http.request(options, function(response) {
console.log('Response statusCode: ' + response.statusCode);
});
request.write(payload_string);
request.end();
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}