Created
December 5, 2015 11:42
-
-
Save rickardp/2ab051bc7f68cd2dcaec to your computer and use it in GitHub Desktop.
Trivial command line interface to 46elks for SMS notification sending
This file contains 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
#!/bin/sh | |
# | |
# How to use: | |
# * Sign up for account at 46elks.com. Note down API user/password (not same as account credentials) | |
# * Install this script and chmod 755 it | |
# * Put the credentials in the file /etc/sms.conf OR ~/.smsconf | |
# | |
# Now you can run the script e.g. echo "My message" | smsnotify [-f from_name] to_number1 to_number2... | |
_FROM=`hostname -s` | |
args=`getopt f: $*` | |
if [ $? != 0 ]; then | |
echo 'Usage: $0 [-f <from_name>] message...' | |
exit 2 | |
fi | |
set -- $args | |
for i; do | |
case "$i" in | |
-f) | |
_FROM=$2 | |
shift 2 | |
;; | |
esac | |
done | |
shift 1 | |
_TO=$* | |
if [ -f $HOME/.smsconf ]; then | |
. $HOME/.smsconf | |
elif [ -f /etc/sms.conf ]; then | |
. /etc/sms.conf | |
else | |
echo "Please create /etc/sms.conf or \$HOME/.smsconf" | |
echo "Export the SMSUSER and SMSPASSWORD variables" | |
exit 1 | |
fi | |
if [ -z "$SMSUSER" ]; then | |
echo "SMSUSER not specified" | |
exit 1 | |
fi | |
if [ -z "$SMSPASSWORD" ]; then | |
echo "SMSPASSWORD not specified" | |
exit 1 | |
fi | |
if [ -z "${_TO}" ]; then | |
echo 'No recipients' | |
exit 2 | |
fi | |
_MESSAGE=$(cat) | |
if [ -z "${_MESSAGE}" ]; then | |
echo 'Empty message. Refusing to send.' | |
exit 2 | |
fi | |
_MESSAGE=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$_MESSAGE") | |
_FROM=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$_FROM") | |
for i in ${_TO}; do | |
j=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$i") | |
echo "Sending '${_MESSAGE}' from ${_FROM} to $i" | |
curl -u "$SMSUSER:$SMSPASSWORD" --data "from=${_FROM}&to=${i}&message=${_MESSAGE}" https://api.46elks.com/a1/SMS | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment