Last active
August 29, 2015 14:04
-
-
Save lavoiesl/e0d1d9173e25bccb63a3 to your computer and use it in GitHub Desktop.
https://pushover.net/ CLI tool
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 | |
| ## | |
| # https://pushover.net/ CLI tool | |
| # | |
| # @license MIT | |
| # @author http://blog.lavoie.sl/ | |
| # @link https://gist.github.com/lavoiesl/e0d1d9173e25bccb63a3 | |
| # | |
| # Supports APP bookmarks, example: | |
| # $ pushover.sh -A myapp | |
| # Will prompt you: Application 'myapp' does not exist, please provide its token: | |
| # After providing it, you may continue or Ctrl-C to abort. The token will be saved in ~/.pushover. | |
| # Following runs will use the saved token | |
| # | |
| # Dependencies: cURL and Python | |
| # | |
| usage() { | |
| echo "Usage: $0 [-c config_file] [-a app_token] [-A app_bookmark] [-u user_token] [-t title] [-s sound] [-p priority] [-T timestamp] [-d device] [-l URL [-L URL_title]] [-qiI] message" >&2 | |
| exit 255 | |
| } | |
| # Store configs below in your .pushover to use as default (typacilly USER_TOKEN and maybe APP_TOKEN) | |
| CONFIG_FILE="$HOME/.pushover" | |
| APP_TOKEN="" | |
| USER_TOKEN="" | |
| TITLE="" | |
| MESSAGE="" | |
| DEVICE="" | |
| SOUND="pushover" # https://pushover.net/api#sounds | |
| PRIORITY="0" | |
| TIMESTAMP="$(date "+%s")" | |
| URL="" | |
| URL_TITLE="" | |
| QUIET=0 # Use -q to reduce output | |
| INTERACTIVE="$([ ! -t 0 ]; echo $?)" # Use -I to turn questions off. By default, test for interactive terminal | |
| prompt_value() { | |
| local prompt="$1" | |
| local default="$2" | |
| local p="$prompt " | |
| if [[ "$INTERACTIVE" -eq "0" ]]; then | |
| if [[ -n "$default" ]]; then | |
| echo "$default" | |
| else | |
| echo "Non-interactive session, unable to answer: '$prompt'" >&2 | |
| exit 1 | |
| fi | |
| fi | |
| [[ -n "$default" ]] && p="$prompt ($default) " | |
| read -p "$p" reply | |
| [[ -z "$reply" ]] && reply="$default" | |
| if [[ -z "$reply" ]]; then | |
| prompt_value "$prompt" "$default" | |
| exit | |
| fi | |
| echo "$reply" | |
| } | |
| confirm() { | |
| local message="$1" | |
| [ "$(prompt_value "$message" "Y")" = "Y" ] && return 0 | |
| return 1 | |
| } | |
| save_config() { | |
| local config="$1" | |
| local value="$(prompt_value "$config ?")" | |
| [ "$?" -eq 0 ] || exit 1 | |
| echo "$config=\"$value\"" >> "$CONFIG_FILE" | |
| echo "$value" | |
| } | |
| ensure_option() { | |
| local config="$1" | |
| local value="$(eval "echo \$${config}")" | |
| if [ -z "$value" ]; then | |
| value="$(prompt_value "$config ?")" | |
| [ "$?" -eq 0 ] || exit 1 | |
| eval "${config}=\"${value}\"" | |
| fi | |
| } | |
| app_bookmark() { | |
| local bookmark="$1" | |
| APP_TOKEN="$(eval "echo \${APP_TOKEN_${bookmark}}")" | |
| if [ -z "$APP_TOKEN" ]; then | |
| APP_TOKEN="$(prompt_value "Application '$bookmark' does not exist, please provide its token:")" | |
| [ "$?" = "0" ] || exit 1 | |
| echo "APP_TOKEN_${bookmark}=\"$token\"" >> "$CONFIG_FILE" | |
| fi | |
| } | |
| # We need to do a first pass to parse CONFIG_FILE | |
| while getopts "qiIc:a:A:u:t:s:p:T:d:l:L:" opt; do | |
| case $opt in | |
| c) CONFIG_FILE="$OPTARG" ;; | |
| q) QUIET=1 ;; | |
| i) INTERACTIVE=1 ;; | |
| I) INTERACTIVE=0 ;; | |
| \?) ;; | |
| :) | |
| echo "Option -$OPTARG requires an argument." >&2 | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| unset OPTIND # reset getopts | |
| if [ -f "$CONFIG_FILE" ]; then | |
| source "$CONFIG_FILE" | |
| fi | |
| while getopts "qiIc:a:A:u:t:s:p:T:d:l:L:" opt; do | |
| case $opt in | |
| c) ;; | |
| q) ;; | |
| i) ;; | |
| I) ;; | |
| a) APP_TOKEN="$OPTARG" ;; | |
| A) app_bookmark "$OPTARG" ;; | |
| u) USER_TOKEN="$OPTARG" ;; | |
| t) TITLE="$OPTARG" ;; | |
| s) SOUND="$OPTARG" ;; | |
| p) PRIORITY="$OPTARG" ;; | |
| T) TIMESTAMP="$OPTARG" ;; | |
| d) DEVICE="$OPTARG" ;; | |
| l) URL="$OPTARG" ;; | |
| L) URL_TITLE="$OPTARG" ;; | |
| \?) | |
| echo "Invalid option: -$OPTARG" >&2 | |
| exit 1 | |
| ;; | |
| :) | |
| echo "Option -$OPTARG requires an argument." >&2 | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| if [ ! -f "$CONFIG_FILE" -a "$INTERACTIVE" = "1" ] && confirm "${CONFIG_FILE} is missing, would you like to create it ?"; then | |
| echo "Creating ${CONFIG_FILE}." >&2 | |
| touch "${CONFIG_FILE}" | |
| APP_TOKEN="$(save_config APP_TOKEN)" | |
| USER_TOKEN="$(save_config USER_TOKEN)" | |
| fi | |
| MESSAGE="$1" | |
| # Mandatory arguments | |
| ensure_option MESSAGE | |
| ensure_option APP_TOKEN | |
| ensure_option USER_TOKEN | |
| output=$(curl -s \ | |
| --form-string "token=$APP_TOKEN" \ | |
| --form-string "user=$USER_TOKEN" \ | |
| --form-string "message=$MESSAGE" \ | |
| --form-string "title=$TITLE" \ | |
| --form-string "timestamp=$TIMESTAMP" \ | |
| --form-string "priority=$PRIORITY" \ | |
| --form-string "sound=$SOUND" \ | |
| --form-string "device=$DEVICE" \ | |
| --form-string "url=$URL" \ | |
| --form-string "url_title=$URL_TITLE" \ | |
| https://api.pushover.net/1/messages.json | python -m json.tool) | |
| status="$?" | |
| if [ "$status" -eq 0 ]; then | |
| if echo "$output" | grep -qF '"status": 1'; then | |
| if [ "$QUIET" -eq 0 ]; then | |
| # print output.request | |
| echo -n "Success: " | |
| echo "$output" | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["request"]' | |
| fi | |
| exit 0 | |
| else | |
| # print output.errors, separated by newlines | |
| echo "$output" | python -c 'import json,sys;obj=json.load(sys.stdin);print "\n".join(obj["errors"])' | |
| exit 1 | |
| fi | |
| else | |
| exit $status | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment