dotfiles

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

commit e5c16d0c00c2f1dca5e0d0dc43e4c406dc593595
parent a72fd8fc3c0c6ac0b2f645693b27825bab329f83
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Mon,  4 Nov 2019 13:13:39 +0100

Use stdin for message, allow multiple recipients, add help and version options

Diffstat:
A.local/bin/text.sh | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+), 0 deletions(-)

diff --git a/.local/bin/text.sh b/.local/bin/text.sh @@ -0,0 +1,93 @@ +#!/bin/sh +set -e +my_number="+18582473666" +version=0.1 + +show_help() { + echo "usage: ${0##*/} [OPTIONS] RECIPIENT1 [RECIPIENT2 ...]" + echo "will send stdin as a text message to each RECIPIENT via twilio." + echo "RECIPIENTs can be phone numbers with country prefix" + echo "(e.g. +45xxyyxxyy), or known aliases." + echo + echo "OPTIONS are one or more of the following:" + echo " -h, --help show this message" + echo " -v, --version show version and license information" + echo " -- do not consider any following arguments as options" + +} + +show_version() { + echo "${0##*/} version $version" + echo "Licensed under the GNU Public License, v3+" + echo "written by Anders Damsgaard, anders@adamsgaard.dk" + echo "https://src.adamsgaard.dk/dotfiles" +} + + +die() { + printf '%s\n' "$1" >&2 + exit 1 +} + +send_twilio_text() { + response=$(curl -X POST --fail --show-error --silent \ + "https://api.twilio.com/2010-04-01/Accounts/$(pass Online/twilio-account-sid)/Messages.xml"\ + --data-urlencode "From=$my_number" \ + --data-urlencode "To=$1" \ + --data-urlencode "Body=$message" \ + -u "$authstring" ) + if [ $? -gt 0 ]; then + die "Failed to send SMS: $response" + fi +} + +while :; do + case "$1" in + -h|-\?|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + --) # end all options + shift + break + ;; + -?*) + die 'error: unknown option specified' + ;; + *) # no more options + break + esac + shift +done + +if [ $# -lt 1 ]; then + show_help + exit 1 +fi + +authstring="$(pass Online/twilio-account-sid):$(pass Online/twilio-auth-token)" +message="$(cat)" +for r in "$@"; do + case "$r" in + susan) + r="+4542742942" + ;; + anders) + r="+4542743142" + ;; + anders-us) + r="+18582478944" + ;; + christian) + r="+4530246587" + ;; + simon) + r="+4551887891" + ;; + esac + send_twilio_text "$r" +done