Browse Source

Switch to using TextMagic

Samuel W. Flint 2 years ago
parent
commit
adb446c3a4
2 changed files with 77 additions and 11 deletions
  1. 6 11
      send-notification
  2. 71 0
      tm

+ 6 - 11
send-notification

@@ -2,17 +2,12 @@
 
 if [ $# -eq 2 ] ; then
     notify-send "${1}" "${2}" 
-    curl -s \
-         -F "token=acioo737xpg83udi17sdsevmjox5eg" \
-         -F "user=u4wih2x18os74469f6zj9jggd7mbon" \
-         -F "message=\"$2\"" \
-         -F "title=\"$1\"" \
-         https://api.pushover.net/1/messages.json
+    if [ ! -z $TM_USERNAME ] ; then
+        tm send --text="${2}" --phones=$NOTIFICATION_PHNUM > /dev/null
+    fi
 else
     notify-send "${1}"
-    curl -s \
-         -F "token=acioo737xpg83udi17sdsevmjox5eg" \
-         -F "user=u4wih2x18os74469f6zj9jggd7mbon" \
-         -F "message=\"$1\"" \
-         https://api.pushover.net/1/messages.json
+    if [ ! -z $TM_USERNAME ] ; then
+        tm send --text="${1}" --phones=$NOTIFICATION_PHNUM > /dev/null
+    fi
 fi

+ 71 - 0
tm

@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+#
+# TextMagic REST API Client
+#
+# Copyright (C) 2015 TextMagic Ltd.
+
+readonly E_OK=0
+readonly E_INVALID_PARAMETERS=1
+readonly E_MISSING_CREDENTIALS=2
+
+################
+# CONFIGURATION
+################
+readonly BASE_URL="https://rest.textmagic.com/api/v2"       # API base URL
+readonly USERNAME=${TM_USERNAME:-}                                        # Account username
+readonly TOKEN=${TM_APIKEY:-}                                           # API access token
+readonly CURLOPTS=""                                        # Custom cURL options
+
+function usage() {
+    echo "Usage:"
+    echo -e "\t$0 send [parameters]"
+    echo -e "\t$0 <http-method> <resource> [parameters]\n"    
+    echo "Examples:"
+    echo -e "\t$0 send --phones=99912345,9997890 --text=\"Hello guys\""
+    echo -e "\t$0 GET /replies"
+    echo -e "\t$0 POST /messages --phones=99912345 --text=\"Hello guys\""    
+    echo -e "\t$0 PUT /user --firstName=Charles --lastName=Conway --company=\"TextMagic Ltd.\""
+    echo -e "\t$0 DELETE /contacts/183905"
+}
+
+function request() {
+    local method path param data curl_string
+    
+    if [ -z $BASE_URL ] || [ -z $USERNAME ] || [ -z $TOKEN ]; then
+        echo "Please configure your access credentials first"
+        exit $E_MISSING_CREDENTIALS
+    fi
+    
+    method="$1"; [[ $# > 0 ]] && shift
+    case "$method" in
+        GET|DELETE|POST|PUT)
+            if [[ $# = 0 ]] || [ -z "$1" ] || [ "${1#/}" = "$1" ]; then
+                usage
+                exit $E_INVALID_PARAMETERS
+            fi            
+            path=${BASE_URL}${1} && shift
+            [[ "$method" = "GET" ]] && curlopts="-G"
+            curlopts="$curlopts $CURLOPTS"
+                
+            i=1
+            for param in "$@"; do
+                [[ $param == --*=* ]] && param=${param#*--} && data[i]="--data-urlencode '${param%%=*}=${param#*=}'" && i=$((i+1))  
+            done
+            
+            curl_string="curl -s -X $method $curlopts -H 'X-TM-Username: $USERNAME' -H 'X-TM-Key: $TOKEN' ${data[@]} $path"
+            eval "$curl_string"
+            
+            exit $E_OK
+            ;;
+        *)
+            usage
+            exit $E_INVALID_PARAMETERS
+            ;;
+    esac
+}
+
+if [[ "$1" = "send" ]]; then
+    shift
+    request POST /messages "$@"
+fi
+request "$@"