Skip to content

Instantly share code, notes, and snippets.

@rdancer
Last active June 30, 2016 19:21
Show Gist options
  • Save rdancer/90bb4cc938da5313229f67543664543d to your computer and use it in GitHub Desktop.
Save rdancer/90bb4cc938da5313229f67543664543d to your computer and use it in GitHub Desktop.
#!/bin/bash
# staffomatic-replacement-needed - SMS alert for the Staffomatic.com scheduling platform
# USAGE
#
# This script reads an RFC-822-formatted message from stdin and sends an SMS alert via the TextMarketer.co.uk API.
#
# Add something like this to ~/.procmailrc:
#
# # Flags:
# # b: Pass only body
# # c: Continue processing filters
# :0bc
# * From:.*[email protected]
# * Subject: Replacement needed
# | /path/to/staffomatic-replacement-needed
#
#
# DEPENDENCIES
#
# * URI::Escape::XS
# * jq(1)
# * sed(1)
# * bash(1)
#
# To install on Ubuntu 14.04:
#
# sudo apt-get install liburi-escape-xs-perl jq sed bash
#
# * Google.com API account & key
# * TextMarketer.co.uk funded account with login & password
#
# Create a private (chmod 0600) file ~/.shadow, and populate it with the secrets:
#
# # https://console.developers.google.com/apis/credentials
# GOOGLE_API_KEY=AIzaSyBQ0gWCJ2Ckdl6UKJ-4JlpABnsjuWKivGg
# # https://messagebox.textmarketer.co.uk/
# SMS_GATEWAY_USER_NAME=yby9E6
# SMS_GATEWAY_PASS_PHRASE=Vw8bVD
set -e
function debug()
{
: echo "$*"
}
function shorturl()
{
set -x
source ~/.shadow
if test x"$GOOGLE_API_KEY" = x; then echo GOOGLE_API_KEY not set - must be set as an environment variable or in ~/.shadow >&2; exit 2; fi
curl -s "https://www.googleapis.com/urlshortener/v1/url?key=${GOOGLE_API_KEY}" \
-H 'Content-Type: application/json' \
-d "{'longUrl': '$*'}" \
| jq .id \
| sed 's/"//g'
}
function urlEscape()
{
perl -M'URI::Escape::XS qw/uri_escape/' \
-e 'print uri_escape($ARGV[0]);' \
"$*"
}
function sendSms()
{
escapedSmsBody="$( urlEscape "$sms" )"
debug escapedSmsBody: "$escapedSmsBody"
base=https://api.textmarketer.co.uk/gateway
phoneNumber=447542894196
source ~/.shadow
if test x"$SMS_GATEWAY_USER_NAME" = x -o x"$SMS_GATEWAY_PASS_PHRASE" = x; then echo SMS_GATEWAY_USER_NAME or SMS_GATEWAY_PASS_PHRASE not set - must be set as an environment variable or in ~/.shadow >&2; exit 2; fi
url="${base}/?username=${SMS_GATEWAY_USER_NAME}&password=${SMS_GATEWAY_PASS_PHRASE}&to=${phoneNumber}&message=${escapedSmsBody}&orig=rdancer"
debug SMS gateway request URL: "$url"
response="$( curl "$url" 2>/dev/null )"
debug API Response: "$response"
debug Exit code: $?
}
while read line; do
debug -- x$line
case x$line in
xStart:)
read startTime
debug startTime: $startTime
;;
xEnd:)
read endTime
debug endTime: $endTime
;;
# Donald Duck is looking for a replacement for the folllowing shift Mo, 27.06:
x*"looking for a replacement"*)
# Translate to English, strip punctuation, and normalize (note: sometimes the
# abbreviation is German 2-letter, sometimes English 2-letter, and
# sometimes English 3-letter!)
#
# "Di, 27.06:" -> "Tue 27/06"
# "Su, 10.07:" -> "Sun 10/07"
#
# Deutsch English
# ---------------------
# Montag Monday
# Dienstag Tuesday
# Mittwoch Wednesday
# Donnerstag Thursday
# Freitag Friday
# Samstag Saturday
# Sonntag Sunday
date="`echo $line | sed 's/.*shift //;s/[,:]//g;s/\./\//' | sed 's/Mo/Mon/;s/Di/Tue/;s/Mi/Wed/;s/Do/Thu/;s/Fr/Fri/;s/Sa/Sat/;s/So/Sun/' | sed 's/Mo/Mon/;s/Tu/Tue/;s/We/Wed/;s/Th/Thu/;s/Fr/Fri/;s/Sa/Sat/;s/Su/Sun/;'`"
debug date: $date
;;
# Go to shift: https://deliveroo-uk.staffomaticapp.com/#locations/13862/schedules/219331/shifts/9159470/applications
x"Go to shift:"*)
url="`echo $line | sed 's/.* http/http/'`"
break
;;
esac
done
shortUrl="`shorturl $url`"
debug shortUrl: "$shortUrl"
sms="$( echo -e "Deliveroo: replacement needed $date $startTime-$endTime\\n$shortUrl" )"
debug sms: "$sms"
sendSms "$sms"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment