Last active
April 11, 2019 06:31
-
-
Save mskian/66603782330d31b09c633ad0a376950a to your computer and use it in GitHub Desktop.
Bash Shell Script to Send Push Notes to your Devices Which Connected with Pushbullet
This file contains hidden or 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 | |
# ----------------------------------------------------------------------------- | |
# Info: | |
# author: Santhosh Veer | |
# file: pushbullet.sh | |
# created: 03.10.2018 | |
# revision: 03.10.2018 | |
# version: 0.1 | |
# ----------------------------------------------------------------------------- | |
# | |
# Bash Shell Script to Send Push Notes to your Devices Which Connected with Pushbullet | |
# DOCS - https://docs.pushbullet.com/#push | |
# | |
# ----------------------------------------------------------------------------- | |
# Version Info | |
VERSION=0.1 | |
## File name | |
SCRIPTNAME=$(basename "$0") | |
# pushbullet API key | |
PUSHBULLET="YOUR API KEY" | |
# List all Registered Devices | |
list_devices(){ | |
echo -e "\\n" | |
echo -e "\\033[1;32m Pushbullet Registered Devices \\033[0m" | |
echo -e "\\n" | |
curl -s --request GET \ | |
-H "Access-Token: $PUSHBULLET" \ | |
--url "https://api.pushbullet.com/v2/devices" \ | sed 's/\\[tnr""]//g' | jq '.devices' | jq -r '["Registered Devices"], ["----------------------"], (.[] | [.nickname, .iden], ["----------------------"]) | @tsv' | |
echo -e "\\n" | |
echo -e "This is your Device ID" | |
echo -e "\\n" | |
} | |
# Create a Notification | |
create_notification(){ | |
echo -e "\\n" | |
echo -e "\\033[1;32m == Pushbullet Notification == \\033[0m" | |
echo -e "\\n" | |
echo -n "Enter your Device ID: " | |
read -r deviceid | |
echo -n "Enter a Push Title: " | |
read -r title | |
echo -n "Enter a Push Message: " | |
read -r message | |
echo -e "\\n" | |
# Quoting the Input | |
body=$(printf '{"title":"%s","body":"%s","type":"note","device_iden":"%s"}' "$title" "$message" "$deviceid") | |
# If no Inputs you will see this Alert message | |
if [[ ! $deviceid ]]; then | |
echo -e "\\033[1;31m Error: Device ID Missing \\033[0m \\n" | |
exit 1 | |
fi | |
if [[ ! $title ]]; then | |
echo -e "\\033[1;31m Error: Push title is Missing \\033[0m \\n" | |
exit 1 | |
fi | |
if [[ ! $message ]]; then | |
echo -e "\\033[1;31m Error: Push Body Message is Missing \\033[0m \\n" | |
exit 1 | |
fi | |
# Curl request | |
curl -s --request POST \ | |
-H "Access-Token: $PUSHBULLET" \ | |
"https://api.pushbullet.com/v2/pushes" \ | |
-H "Content-Type: application/json" \ | |
--data "$body" | jq | |
} | |
# Help Message | |
help(){ | |
echo -e "\\n" | |
echo -e "$SCRIPTNAME [options] | |
Example: | |
pushbullet.sh -l | |
Options: | |
-l List devices | |
-s Send Notification to devices | |
-h Display Help Message | |
-v Check CLI Version | |
\\n" | |
} | |
# No input params triggers this error | |
check_for_empty_input(){ | |
if [ $# -eq 0 ]; | |
then | |
echo -e "\\n" | |
echo -e "\\033[1;31m Error: No input \\033[0m \\n" | |
help | |
exit 1 | |
fi | |
} | |
# Check for required packages | |
check_requirements(){ | |
local requirements=("$@") | |
for app in "${requirements[@]}"; do | |
type "$app" >/dev/null 2>&1 || \ | |
{ echo >&2 "$app is required but it's not installed. Aborting."; exit 1; } | |
done | |
} | |
# Main Functions | |
main(){ | |
check_for_empty_input "$@" | |
check_requirements jq curl | |
while getopts ':lsvh' flag; do | |
case "$flag" in | |
l) | |
list_devices | |
exit 0 | |
;; | |
s) | |
create_notification | |
;; | |
v) | |
echo -e "\\033[1;32m Version $VERSION \\033[0m" | |
exit 0 | |
;; | |
h) | |
help | |
exit 0 | |
;; | |
?) | |
echo "script usage: $SCRIPTNAME [-l] [-s] [-v] [-h]" >&2 | |
exit 1 | |
;; | |
*) | |
esac | |
done | |
shift $((OPTIND-1)) | |
} | |
main "$@" | |
exit 0 |
If you get this below error.
jq is required but it's not installed. Aborting.
Run the Below Command to Install jq
sudo apt install jq
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install
pushbullet.sh
in code Editor & Find the Line# pushbullet API key
add your API Key and start Sending Push NotesExecute the Script