Skip to content

Instantly share code, notes, and snippets.

@logicbomb
Last active January 20, 2016 20:17
Show Gist options
  • Save logicbomb/b2709eb365b828bef25b to your computer and use it in GitHub Desktop.
Save logicbomb/b2709eb365b828bef25b to your computer and use it in GitHub Desktop.
Scripts for retrieving and scrubbing twilio message documents
#!/bin/bash
ERR_CODE="∑"
while [ $# -gt 0 ]; do
case "$1" in
-e | --error_code)
ERR_CODE=$2
;;
esac
shift
done
TMP=`mktemp`
echo "date,from,to,error_code,error_message,sid" > $TMP
cat /dev/stdin | jq -r -c --arg code $ERR_CODE '.messages[] | if $code != "∑" then select(.error_code == ($code | tonumber)) else select(.error_code != null) end | [(.date_sent | strptime("%a, %d %b %Y %H:%M:%S +0000") | todate),.from,.to,.error_code,.error_message,.sid] | @csv' >> $TMP ; cat $TMP
#!/bin/bash
while [ $# -gt 0 ]; do
case "$1" in
-s | --start_date)
START=$2
;;
-f | --from)
IFS="," read -r -a FROM <<< $2
;;
-t | --to)
IFS="," read -r -a TO <<< $2
;;
-p | --page_size)
PAGE_SIZE=$2
;;
esac
shift
done
if [[ -z $PAGE_SIZE || $PAGE_SIZE > 1000 ]]; then
PAGE_SIZE=1000
fi
if [[ -z $START ]]; then
START=`date +'%Y-%m-%d'`
fi
OUTPUT=`mktemp`
if [[ $? -ne 0 ]]; then
>&2 echo "An error occurred creating output file ($?)"
exit 999
fi
TWILIO_API_ROOT="https://api.twilio.com"
function build_document {
if [[ "$1" != "To" && "$1" != "From" && -n "$1" ]]; then
echo "the first argument to build_document must either be 'From', 'To', or empty (received '$1')"
exit 999
fi
if [[ -n $1 ]]; then
PHONE_PARAM="&$1=$2"
else
PHONE_PARAM=""
fi
NEXT_URI="/2010-04-01/Accounts/$TWILIO_ACCOUNTID/Messages.json?PageSize=$PAGE_SIZE&DateSent>=$START$PHONE_PARAM"
>&2 echo $NEXT_URI
TEMP=`mktemp`
until [[ "$NEXT_URI" = "null" ]]; do
TEMP=`mktemp`
if [[ $? -ne 0 ]]; then
>&2 echo "An error occurred creating a temporary file ($?)"
exit 999
fi
curl -G -s -S $TWILIO_API_ROOT$NEXT_URI -u $TWILIO_ACCOUNTID:$TWILIO_SID > $TEMP
if [[ $? -ne 0 ]]; then
>&2 echo "An error occurred retrieving $NEXT_URI ($?)"
exit 999
fi
URI=`cat $TEMP | jq -r '.uri'`
NEXT_URI=`cat $TEMP | jq -r '.next_page_uri'`
CUR_PAGE=`cat $TEMP | jq '.page'`
LAST_PAGE=`cat $TEMP | jq '.num_pages'`
if [[ -z $CUR_PAGE || -z $LAST_PAGE ]]; then
>&2 echo "Could not parse document returned from $NEXT_URI"
exit 999
fi
>&2 echo "Retrieved page $CUR_PAGE of $LAST_PAGE from $URI"
cat $TEMP >> $OUTPUT
done
}
if [[ ${#FROM[@]} > 0 || ${#TO[@]} > 0 ]]; then
for NUM in ${FROM[@]}
do
build_document "From" $NUM
done
for NUM in ${TO[@]}
do
build_document "To" $NUM
done
else
build_document
fi
cat $OUTPUT
@logicbomb
Copy link
Author

These scripts make it easy to retrieve and generate twilio error reports. To get twilio messages the following environment variables must be set: TWILIO_ACCOUNTID and TWILIO_SID

Get all of the messages starting from a certain time (default is today): ./fetch_twilio_messages -s 2016-01-11
Get all of the messages sent from a set of numbers: ./fetch_twilio_messages -f +15551231234,+15551231235,+15551231236
Get all of the messages sent to a set of numbers: ./fetch_twilio_messages -t +15551231234,+15551231235,+15551231236

Find all errors in the retrieved messages and save it as a CSV: ./fetch_twilio_messages | ./check_twilio_errors.sh > errors.csv
Find specific errors in the retrieved messages: ./fetch_twilio_messages | ./check_twilo_errors.sh -e 30005 > errors.csv

Look up information about error codes here
These script requires jq to parse the input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment