#!/bin/bash # # send-sms.sh # # Usage: # echo "Hello world" | ./send-sms.sh +41791234567 # # Environment: # ASPSMS_USERKEY # ASPSMS_PASSWORD # # Exit codes: # # 0 SMS delivered # # 1 Usage/configuration error # 2 Local/network/API communication failure # 3 ASPSMS authentication failure # 4 Invalid phone number # 5 Recipient unavailable (phone off / no coverage / expired) # 6 Mobile operator/provider failure # 7 ASPSMS rejected request # 8 Delivery timeout (status never became final) # 9 Unknown failure # set -euo pipefail API="https://webapi.aspsms.com" if [[ $# -ne 1 ]] then echo "Usage: $0 PHONE_NUMBER" >&2 exit 1 fi : "${ASPSMS_USERKEY:?ASPSMS_USERKEY not set}" : "${ASPSMS_PASSWORD:?ASPSMS_PASSWORD not set}" PHONE="$1" MESSAGE="$(cat -)" if [[ -z "$MESSAGE" ]] then echo "Empty message" >&2 exit 1 fi TRANSREF="$(date +%s)-$$-$RANDOM" # # Send SMS # SEND_JSON="$( curl -fsS \ --get \ "$API/SendSimpleSMS" \ --data-urlencode "Userkey=$ASPSMS_USERKEY" \ --data-urlencode "Password=$ASPSMS_PASSWORD" \ --data-urlencode "Recipient=$PHONE" \ --data-urlencode "MessageData=$MESSAGE" \ --data-urlencode "TransactionReferenceNumber=$TRANSREF" )" || { echo "Communication with ASPSMS failed" >&2 exit 2 } ERROR_CODE=$(jq -r '.ErrorCode // empty' <<<"$SEND_JSON") ERROR_DESC=$(jq -r '.ErrorDescription // empty' <<<"$SEND_JSON") case "$ERROR_CODE" in 1) ;; 3|8|9) echo "$ERROR_DESC" >&2 exit 3 ;; 20|206) echo "$ERROR_DESC" >&2 exit 4 ;; 24) echo "$ERROR_DESC" >&2 exit 7 ;; "") echo "Unexpected response:" >&2 echo "$SEND_JSON" >&2 exit 2 ;; *) echo "$ERROR_DESC" >&2 exit 7 ;; esac # # Poll delivery status # # # DeliveryStatus: # -1 not yet known # 0 delivered # 1 buffered # 2 failed # # Reason codes of interest: # # 000 unknown subscriber # 101 unknown subscriber # 107 absent subscriber # 108 delivery failed # 105/113 SC congestion # 118-126 network/provider failures # 206 invalid destination address # for ((i=0;i<60;i++)) do SEC=$(expr $i / 10) # Wait at least 100ms, then go up every second by 1 second sleep $SEC.1 STATUS_JSON="$( curl -fsS \ --get \ "$API/SendingStat03/Get" \ --data-urlencode "Userkey=$ASPSMS_USERKEY" \ --data-urlencode "Password=$ASPSMS_PASSWORD" \ --data-urlencode "TransactionReferenceNumber=$TRANSREF" )" || continue DELIVERY=$(jq -r '.[0].DeliveryStatus // empty' <<<"$STATUS_JSON") REASON=$(jq -r '.[0].ReasonCode // ""' <<<"$STATUS_JSON") case "$DELIVERY" in 0) exit 0 ;; 1) continue ;; 2) case "$REASON" in 206|000|101) exit 4 ;; 107|108) exit 5 ;; 105|113|118|119|120|121|122|123|124|125|126) exit 6 ;; *) exit 9 ;; esac ;; -1|"") continue ;; esac done exit 8 #| Exit | Meaning | #| ---- | ----------------------------------------------------------------------------- | #| 0 | SMS successfully delivered | #| 1 | Usage or local configuration error | #| 2 | Unable to contact ASPSMS/API/network error | #| 3 | Authentication failure (invalid user key/password) | #| 4 | Invalid or unknown recipient number | #| 5 | Recipient unreachable (phone switched off, out of coverage, delivery expired) | #| 6 | Mobile operator/network/provider failure | #| 7 | ASPSMS rejected the request (temporary service issues, invalid request, etc.) | #| 8 | Timed out waiting for a final delivery report | #| 9 | Delivery failed for an unclassified reason |