Skip to content

Instantly share code, notes, and snippets.

@ferm10n
Last active March 11, 2020 04:06
Show Gist options
  • Save ferm10n/a2dd6a5289a24efe621a2a2fe9dde4b1 to your computer and use it in GitHub Desktop.
Save ferm10n/a2dd6a5289a24efe621a2a2fe9dde4b1 to your computer and use it in GitHub Desktop.
pb-notify sends a push from command line. `pb-notify title message`
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"axios": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
"requires": {
"follow-redirects": "1.5.10"
}
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
},
"follow-redirects": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "=3.1.0"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}
}
}
{
"name": "pb-notify",
"version": "1.0.0",
"type": "module",
"bin": {
"pb-notify": "./pb-notify.js"
},
"dependencies": {
"axios": "^0.19.2"
}
}
#!/usr/bin/env -S node --experimental-modules
import os from 'os';
import { promises as fs } from 'fs';
import path from 'path';
import http from 'http';
import axios from 'axios';
import assert from 'assert';
async function main () {
const [ , , title, msg ] = process.argv;
if (!title || !msg) {
console.error('title and message arguments are required!');
return;
}
let pb;
const pbPath = path.join(os.homedir(), '.pushbullet.json'); // create the path to the config file
try {
pb = JSON.parse(await fs.readFile(pbPath, 'utf8')); // read and parse the config file
assert.ok(pb.token, 'token is set');
} catch (err) {
console.error('Failed to read pushbullet config file', pbPath, err);
return;
}
try {
const response = await axios({
method: 'post',
url: 'https://api.pushbullet.com/v2/pushes',
data: { body: msg, title, type: 'note' },
headers: { 'Access-Token': pb.token },
});
console.log('sent push');
} catch (err) {
if (!err.response) {
console.error(err);
return;
}
console.error('pushbullet request failed', { status: err.response.status, data: err.response.data });
return;
}
return;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment