Last active
December 17, 2015 08:59
-
-
Save 4np/5583898 to your computer and use it in GitHub Desktop.
Some bash commands for easily interacting with Twilio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Twilio Commands | |
# put this in your ~/.profile | |
TWILIO_MASTER_SID=put here your master SID | |
TWILIO_MASTER_TOKEN=put here your master token | |
TWILIO_BASE_URL=https://api.twilio.com/2010-04-01/ | |
TWILIO_CREDENTIALS=$TWILIO_MASTER_SID":"$TWILIO_MASTER_TOKEN | |
twilio() { | |
if [ ! "$1" ]; then | |
echo "Usage:" | |
echo "------" | |
echo "twilio subaccounts" | |
echo "twilio status <subaccount friendly name>" | |
echo "twilio activate <subaccount friendly name>" | |
echo "twilio suspend <subaccount friendly name>" | |
echo "twilio close <subaccount friendly name>" | |
echo "twilio call <subaccount friendly name> <international phone number> <text to say on answer>" | |
echo "twilio sms <subaccount friendly name> <international phone number> <text to sms> (max 160 characters)" | |
echo "" | |
echo "Examples:" | |
echo "---------" | |
echo "twilio suspend \"SUBACCOUNT CREATED AT 2013-05-15 05:01 AM\"" | |
echo "twilio call demo +31612345678 \"i like bananas and they like me"\" | |
echo "" | |
echo "Note:" | |
echo "-----" | |
echo "- closing a sub account is *irreversible*!" | |
echo "- parameters that contain spaces should be wrapped in \"double quotes\"" | |
elif [ "$2" ]; then | |
# get the SID for this sub account | |
FRIENDLY_NAME="$(ruby -r cgi -e 'puts CGI::escape(ARGV[0])' "$2")" | |
OUTPUT=`curl --stderr /dev/null -G $TWILIO_BASE_URL"Accounts.json" -d "FriendlyName="$FRIENDLY_NAME -u $TWILIO_CREDENTIALS` | |
SID=`echo "$OUTPUT"| jshon -e accounts -a -e sid | sed 's/"//g'` | |
STATUS=`echo "$OUTPUT"| jshon -e accounts -a -e status | sed 's/"//g'` | |
if [ $SID ]; then | |
if [ $1 == "status" ]; then | |
echo "sub account '"$2"' is "$STATUS | |
elif [ $1 == "activate" ]; then | |
STATUS=`curl --stderr /dev/null -XPOST $TWILIO_BASE_URL"Accounts/"$SID".json" -d "Status=active" -u $TWILIO_CREDENTIALS | jshon -e status | sed 's/"//g'` | |
if [ $STATUS == "active" ]; then | |
echo "activated sub account "$2 | |
else | |
echo "could not activate sub account "$2 | |
fi | |
elif [ $1 == "suspend" ]; then | |
STATUS=`curl --stderr /dev/null -XPOST $TWILIO_BASE_URL"Accounts/"$SID".json" -d "Status=suspended" -u $TWILIO_CREDENTIALS | jshon -e status | sed 's/"//g'` | |
if [ $STATUS == "suspended" ]; then | |
echo "suspended sub account "$2 | |
else | |
echo "could not suspend sub account "$2 | |
fi | |
elif [ $1 == "close" ]; then | |
STATUS=`curl --stderr /dev/null -XPOST $TWILIO_BASE_URL"Accounts/"$SID".json" -d "Status=closed" -u $TWILIO_CREDENTIALS | jshon -e status | sed 's/"//g'` | |
if [ $STATUS == "closed" ]; then | |
echo "closed sub account "$2 | |
else | |
echo "could not close sub account "$2 | |
fi | |
elif [ $1 == "call" ]; then | |
if [ $3 ]; then | |
# determine from number | |
FROM=`curl --stderr /dev/null -G $TWILIO_BASE_URL"Accounts/"$SID"/IncomingPhoneNumbers.json" -u $TWILIO_CREDENTIALS | jshon -e incoming_phone_numbers -a -e phone_number | sed 's/"//g'` | |
TO="$(ruby -r cgi -e 'puts CGI::escape(ARGV[0])' "$3")" | |
# url encode the TWiML reponse | |
TWIML="<Response><Say voice=\"alice\">"$4"</Say></Response>" | |
TWIML_ENCODED="$(ruby -r cgi -e 'puts CGI::escape(ARGV[0])' "$TWIML")" | |
TWIML_URL="http://twimlets.com/echo?Twiml="$TWIML_ENCODED | |
TWIML_URL_ENCODED="$(ruby -r cgi -e 'puts CGI::escape(ARGV[0])' "$TWIML_URL")" | |
RETURN=`curl --stderr /dev/null -XPOST $TWILIO_BASE_URL"Accounts/"$SID"/Calls.json" -u $TWILIO_CREDENTIALS -d "From="$FROM -d "To="$TO -d "Url="$TWIML_URL_ENCODED | jshon -e to | sed 's/"//g'` | |
echo "calling "$RETURN"..." | |
else | |
echo "no phonenumber specified..." | |
fi | |
elif [ $1 == "sms" ]; then | |
if [ $3 ]; then | |
# determine from number | |
FROM=`curl --stderr /dev/null -G $TWILIO_BASE_URL"Accounts/"$SID"/IncomingPhoneNumbers.json" -u $TWILIO_CREDENTIALS | jshon -e incoming_phone_numbers -a -e phone_number | sed 's/"//g'` | |
TO="$(ruby -r cgi -e 'puts CGI::escape(ARGV[0])' "$3")" | |
BODY="$(ruby -r cgi -e 'puts CGI::escape(ARGV[0])' "$4")" | |
RETURN=`curl --stderr /dev/null -XPOST $TWILIO_BASE_URL"Accounts/"$SID"/SMS/Messages.json" -u $TWILIO_CREDENTIALS -d "From="$FROM -d "To="$TO -d "Body="$BODY | jshon -e to | sed 's/"//g'` | |
echo "sending sms to "$RETURN": "$4 | |
else | |
echo "no phonenumber specified..." | |
fi | |
fi | |
else | |
echo "no such sub account "$2 | |
fi | |
elif [ $1 == "subaccounts" ]; then | |
SUBACCOUNTS_RAW=`curl --stderr /dev/null -X GET $TWILIO_BASE_URL"Accounts.json" -u $TWILIO_CREDENTIALS` | |
SUBACCOUNTS=(`echo $SUBACCOUNTS_RAW|jshon -e accounts -a -e friendly_name -u|sed 's/ /%/g'`) | |
STATUSSES=(`echo $SUBACCOUNTS_RAW|jshon -e accounts -a -e status -u`) | |
total=${#SUBACCOUNTS[*]} | |
echo -e "\033[1;37mThere are \033[1;35m"$total"\033[1;37m subaccounts available in your master account:\033[0;00m" | |
echo "" | |
for (( i=0; i<=$(( $total - 1 )); i++)) | |
do | |
A=`echo ${SUBACCOUNTS[$i]}|sed 's/%/ /g'` | |
S=${STATUSSES[$i]} | |
if [ $S == "active" ]; then | |
echo -e "\033[1;32mactive \033[1;37m"$A"\033[0;00m" | |
elif [ $S == "suspended" ]; then | |
echo -e "\033[1;33msuspended \033[1;37m"$A"\033[0;00m" | |
elif [ $S == "closed" ]; then | |
echo -e "\033[1;31mclosed \033[1;37m"$A"\033[0;00m" | |
else | |
echo -e "\034[1;31munknown? \033[1;37m"$A"\033[0;00m" | |
fi | |
done | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment