dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

sendtext (1274B)


      1 #!/bin/sh
      2 set -e
      3 
      4 pass() {
      5 	gpg2 -q -d "${HOME}/.password-store/${1}.gpg"
      6 }
      7 
      8 my_number="$(pass Telephone/anders-twilio)"
      9 version=0.1
     10 
     11 show_help() {
     12 	echo "usage: ${0##*/} [OPTIONS] RECIPIENT1 [RECIPIENT2 ...]"
     13 	echo "will send stdin as a text message to each RECIPIENT via twilio."
     14 	echo "RECIPIENTs can be phone numbers with country prefix"
     15 	echo "(e.g. +45xxyyxxyy), or known aliases."
     16 	echo
     17 	echo "OPTIONS are one or more of the following:"
     18 	echo "   -h     show this message"
     19 }
     20 
     21 die() {
     22 	printf '%s\n' "$1" >&2
     23 	exit 1
     24 }
     25 
     26 send_twilio_text() {
     27 	response=$(curl -X POST --fail --show-error --silent \
     28 		"https://api.twilio.com/2010-04-01/Accounts/$(pass Online/twilio-account-sid)/Messages.xml"\
     29 		--data-urlencode "From=$my_number" \
     30 		--data-urlencode "To=$1" \
     31 		--data-urlencode "Body=$message" \
     32 		-u "$authstring" )
     33 	if [ $? -gt 0 ]; then
     34 		die "Failed to send SMS: $response"
     35 	fi
     36 }
     37 
     38 while :; do
     39 	case "$1" in
     40 		-h)
     41 			show_help
     42 			exit 0
     43 			;;
     44 		*)  # no more options
     45 			break
     46 	esac
     47 	shift
     48 done
     49 
     50 if [ $# -lt 1 ]; then
     51 	show_help
     52 	exit 1
     53 fi
     54 
     55 authstring="$(pass Online/twilio-account-sid):$(pass Online/twilio-auth-token)"
     56 message="$(cat)"
     57 for r in "$@"; do
     58 	case "$r" in
     59 		+[0-9]*)
     60 			to="$r";;
     61 		*)
     62 			to="$(pass Telephone/$r)";;
     63 	esac
     64 	send_twilio_text "$to"
     65 done