challenger-send-sms-telesign.sh (3924B)
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 to $PHONE_NUMBER failed: $STATUS_CODE" 1>&2 87 echo "$RESPONSE" 1&>2 88 exit 1 89 ;; 90 esac 91 92 MAX_ITERATIONS=12 93 94 # Poll for message status 95 echo "Polling message status (reference_id: $REFERENCE_ID)..." 1>&2 96 for N in $(seq 1 "$MAX_ITERATIONS") 97 do 98 STATUS_RESPONSE=$(curl --silent --show-error --fail \ 99 --url "https://rest-api.telesign.com/v1/messaging/$REFERENCE_ID" \ 100 --header "Authorization: Basic $TELESIGN_AUTH_TOKEN") 101 102 echo "$STATUS_RESPONSE" >> "$TMPFILE" 103 104 STATUS_CODE=$(echo "$STATUS_RESPONSE" | jq -r '.status.code') 105 DESCRIPTION=$(echo "$STATUS_RESPONSE" | jq -r '.status.description') 106 107 case "$STATUS_CODE" in 108 "200") 109 # Delivered to headset. Great! 110 echo "Delivered to headset" 1>&2 111 exit 0 112 ;; 113 "203"|"290"|"292"|"295") 114 # Delivered to gateway, wait a bit for an update 115 sleep 2 116 ;; 117 "210") 118 # Temporary phone error 119 sleep 15 120 ;; 121 "207"|"211"|"220"|"221"|"222"|"231"|"237"|"238") 122 # Failure to deliver (hard) 123 echo "Could not deliver" 1>&2 124 exit 1 125 ;; 126 "250") 127 # Final status unknown 128 echo "Final status unknown, assuming success" 1>&2 129 exit 0 130 ;; 131 "502"|"503"|"504"|"505"|"506"|"507"|"508"|"509"|"510"|"511"|"512"|"513"|"514"|"515"|"517"|"520"|"521") 132 echo "Carrier problem ($STATUS_CODE)" 1>&2 133 exit 1 134 ;; 135 "10000") 136 # Internal error at telesign... 137 echo "Telesign internal error" 1>&2 138 exit 1 139 ;; 140 "10019"|"10020") 141 # Rate limit exceeded. Treating as hard failure for now. 142 echo "Rate limit exceeded" 1>&2 143 exit 1 144 ;; 145 *) 146 # Many possible status codes for failure... 147 echo "Message delivery to $PHONE_NUMBER failed: $STATUS_CODE" 1>&2 148 echo "$RESPONSE" 1&>2 149 exit 1 150 ;; 151 esac 152 done 153 154 echo "Unclear message delivery status $STATUS_CODE ($DESCRIPTION) after $MAX_ITERATIONS iterations. Assuming failure." 1>&2 155 exit 1