anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

anastasis-authorization-sms-telesign.sh (3831B)


      1 #!/bin/bash
      2 # This file is in the public domain.
      3 # Send an SMS using Telesign API
      4 set -eu
      5 
      6 # Check shared secrets
      7 if [ -x "$TELESIGN_AUTH_TOKEN" ]
      8 then
      9     echo "TELESIGN_AUTH_TOKEN not set in environment"
     10     exit 1
     11 fi
     12 
     13 if [ $# -ne 1 ]; then
     14     echo "Usage: $0 <phone_number>" 1>&2
     15     exit 1
     16 fi
     17 
     18 PHONE_NUMBER="$1"
     19 MESSAGE=$(cat -)
     20 
     21 TMPFILE=$(mktemp /tmp/telesign-sms-logging-XXXXXX)
     22 
     23 RESPONSE=$(curl --silent --show-error --fail \
     24   --url https://rest-api.telesign.com/v1/messaging \
     25   --request POST \
     26   --header "Authorization: Basic $TELESIGN_AUTH_TOKEN" \
     27   --header "Content-Type: application/x-www-form-urlencoded" \
     28   --data account_livecycle_event=transact \
     29   --data "phone_number=$PHONE_NUMBER" \
     30   --data-urlencode "message=$MESSAGE" \
     31   --data "message_type=OTP")
     32 
     33 echo "$RESPONSE" > "$TMPFILE"
     34 REFERENCE_ID=$(jq -r '.reference_id' "$TMPFILE")
     35 
     36 if [ "$REFERENCE_ID" == "null" ];
     37 then
     38     echo "Failed to retrieve reference ID." 1>&2
     39     exit 1
     40 fi
     41 
     42 STATUS_CODE=$(echo "$RESPONSE" | jq -r '.status.code')
     43 
     44 case "$STATUS_CODE" in
     45     "200")
     46 	# Delivered to headset. Should basically never happen here.
     47 	exit 0
     48         ;;
     49     "203"|"292"|"295")
     50 	# Delivered to gateway
     51 	sleep 2
     52         ;;
     53     "207"|"211"|"220"|"221"|"222"|"231"|"237"|"238")
     54 	# Failure to deliver (hard)
     55 	echo "Could not deliver" 1>&2
     56 	exit 1
     57         ;;
     58     "210")
     59 	# Temporary phone error
     60 	;;
     61     "250")
     62 	# Final status unknown
     63 	echo "Final status unknown, assuming success" 1>&2
     64 	exit 0
     65 	;;
     66     "290")
     67 	# Message in progress, go into loop below
     68 	sleep 2
     69         ;;
     70     "502"|"503"|"504"|"505"|"506"|"507"|"508"|"509"|"510"|"511"|"512"|"513"|"514"|"515"|"517"|"520"|"521")
     71 	echo "Carrier problem ($STATUS_CODE)" 1>&2
     72 	exit 1
     73 	;;
     74     "10000")
     75 	# Internal error at telesign...
     76 	echo "Telesign internal error" 1>&2
     77 	exit 1
     78 	;;
     79     "10019"|"10020")
     80 	# Rate limit exceeded. Treating as hard failure for now.
     81 	echo "Rate limit exceeded" 1>&2
     82 	exit 1
     83 	;;
     84     *)
     85 	# Many possible status codes for failure...
     86 	echo "Message delivery failed: $STATUS_CODE" 1>&2
     87 	exit 1
     88 	;;
     89 esac
     90 
     91 MAX_ITERATIONS=12
     92 
     93 # Poll for message status
     94 echo "Polling message status (reference_id: $REFERENCE_ID)..." 1>&2
     95 for N in $(seq 1 "$MAX_ITERATIONS")
     96 do
     97     STATUS_RESPONSE=$(curl --silent --show-error --fail \
     98       --url "https://rest-api.telesign.com/v1/messaging/$REFERENCE_ID" \
     99       --header "Authorization: Basic $TELESIGN_AUTH_TOKEN")
    100 
    101     echo "$STATUS_RESPONSE" >> "$TMPFILE"
    102     
    103     STATUS_CODE=$(echo "$STATUS_RESPONSE" | jq -r '.status.code')
    104     DESCRIPTION=$(echo "$STATUS_RESPONSE" | jq -r '.status.description')
    105 
    106     case "$STATUS_CODE" in
    107 	"200")
    108 	    # Delivered to headset. Great!
    109 	    echo "Delivered to headset" 1>&2
    110 	    exit 0
    111             ;;
    112 	"203"|"290"|"292"|"295")
    113 	    # Delivered to gateway, wait a bit for an update
    114 	    sleep 2
    115             ;;
    116 	"210")
    117 	    # Temporary phone error
    118 	    sleep 15
    119 	    ;;
    120 	"207"|"211"|"220"|"221"|"222"|"231"|"237"|"238")
    121 	    # Failure to deliver (hard)
    122 	    echo "Could not deliver" 1>&2
    123 	    exit 1
    124             ;;
    125 	"250")
    126 	    # Final status unknown
    127 	    echo "Final status unknown, assuming success" 1>&2
    128 	    exit 0
    129 	    ;;
    130 	"502"|"503"|"504"|"505"|"506"|"507"|"508"|"509"|"510"|"511"|"512"|"513"|"514"|"515"|"517"|"520"|"521")
    131 	    echo "Carrier problem ($STATUS_CODE)" 1>&2
    132 	    exit 1
    133 	    ;;
    134 	"10000")
    135 	    # Internal error at telesign...
    136 	    echo "Telesign internal error" 1>&2
    137 	    exit 1
    138 	    ;;
    139 	"10019"|"10020")
    140 	    # Rate limit exceeded. Treating as hard failure for now.
    141 	    echo "Rate limit exceeded" 1>&2
    142 	    exit 1
    143 	    ;;
    144 	*)
    145 	    # Many possible status codes for failure...
    146 	    echo "Message delivery failed: $STATUS_CODE" 1>&2
    147 	    exit 1
    148 	    ;;
    149     esac
    150 done
    151 
    152 echo "Unclear message delivery status $STATUS_CODE ($DESCRIPTION) after $MAX_ITERATIONS iterations. Assuming failure." 1>&2
    153 exit 1
    154