Created
December 31, 2021 02:45
-
-
Save gnanet/c790c23c5bbbdefa53a4cf7691e87f4f to your computer and use it in GitHub Desktop.
PushBullet API basic client for BASH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# PushBullet API basic client for BASH | |
# | |
# needs some packages to run (available Debian Jessie and up) | |
# apt -y install jq jshon curl | |
# | |
pbapi="" # PushBullet API token | |
device_to_default="" # device iden to send directly as default (optional) | |
if [[ "x${pbapi}" == "x" ]]; then echo "REQUIRED: PushBullet API token"; exit 1; fi | |
if [ $1 ]; then | |
case "$1" in | |
"list-push") | |
curl -s -u $pbapi: "https://api.pushbullet.com/v2/pushes?active=true" | jq '[.pushes[] | ({ type: .type, date: .modified, subject: .title, message: .body, target_iden: .target_device_iden}) ]' | |
;; | |
"list-directpush") | |
curl -s -u $pbapi: "https://api.pushbullet.com/v2/pushes?active=true" | jq '[.pushes[] | select(.target_device_iden != null) | ({ type: .type, date: .modified, subject: .title, message: .body, target_iden: .target_device_iden}) ]' | |
;; | |
"list-device") | |
curl -s -u $pbapi: "https://api.pushbullet.com/v2/devices" | jq '.devices[]' | |
;; | |
"push") | |
if [ "$2" ] && [ "$3" ]; then | |
title="$2" | |
msgbody="$3" | |
if [ "$4" ]; then | |
device_to="$4" | |
json_data=$(echo "{}" | jshon -s "${device_to}" -i device_iden | jshon -s note -i type | jshon -s "${title}" -i title | jshon -s "${msgbody}" -i body) | |
elif [ -n "${device_to_default}" ]; then | |
json_data=$(echo "{}" | jshon -s "${device_to_default}" -i device_iden | jshon -s note -i type | jshon -s "${title}" -i title | jshon -s "${msgbody}" -i body) | |
else | |
json_data=$(echo "{}" | jshon -s note -i type | jshon -s "${title}" -i title | jshon -s "${msgbody}" -i body) | |
fi | |
push_response=$(curl -s -u $pbapi: -X POST https://api.pushbullet.com/v2/pushes \ | |
--header 'Content-Type: application/json' \ | |
--data-binary "${json_data}") | |
if [[ "$(echo "${push_response}" | jshon -e type | tr -d '"')" == "note" ]]; then | |
exit 0 | |
elif [[ "x$(echo "${push_response}" | jshon -e error -e type -Q | tr -d '"')" != "x" ]]; then | |
echo "${push_response}" | jshon >&2 | |
exit 1 | |
fi | |
fi | |
;; | |
"monit") | |
if [ "$2" ] && [ "$3" ]; then | |
title="Monit $(hostname --fqdn) $2" | |
msgbody="$3" | |
if [ "$4" ]; then | |
device_to="$4" | |
json_data=$(echo "{}" | jshon -s "${device_to}" -i device_iden | jshon -s note -i type | jshon -s "${title}" -i title | jshon -s "${msgbody}" -i body) | |
elif [ -n "${device_to_default}" ]; then | |
json_data=$(echo "{}" | jshon -s "${device_to_default}" -i device_iden | jshon -s note -i type | jshon -s "${title}" -i title | jshon -s "${msgbody}" -i body) | |
else | |
json_data=$(echo "{}" | jshon -s note -i type | jshon -s "${title}" -i title | jshon -s "${msgbody}" -i body) | |
fi | |
push_response=$(curl -s -u $pbapi: -X POST https://api.pushbullet.com/v2/pushes \ | |
--header 'Content-Type: application/json' \ | |
--data-binary "${json_data}") | |
if [[ "$(echo "${push_response}" | jshon -e type | tr -d '"')" == "note" ]]; then | |
exit 0 | |
elif [[ "x$(echo "${push_response}" | jshon -e error -e type -Q | tr -d '"')" != "x" ]]; then | |
echo "${push_response}" | jshon >&2 | |
exit 1 | |
fi | |
fi | |
;; | |
*) | |
echo "Usage: $(basename $0) {list-push|list-directpush|list-device|push <subject> <message> [device_iden]|monit <subject> <message> [device_iden]}" | |
esac | |
exit 0 | |
else | |
echo "Usage: $(basename $0) {list-push|list-directpush|list-device|push <subject> <message> [device_iden]|monit <subject> <message> [device_iden]}" | |
exit 0 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment