piątek, 22 marca 2013

Automatic SMS Alerts Using Google Calendar

The idea is to add an event to Google calendar with some short start time from now (say 15 minutes) and SMS reminder feature.


#!/bin/bash
# reminder via google with 15 minutes from now start time
#
MESSAGE="Neptune ALARM: something nasty has happened!"

# In case of service failure try 3 times:
for i in 1 2 3; do

# Compute 15min from now time with date:
NOW=`date "+%s"`
TIME_SHF1=$(($NOW + 900 ))
FWD_TIME1=`date +"%d/%m/%Y %H:%M" -d"@$TIME_SHF1"`;

# Check for optional script argument
if [ -n "$1" ] ; then MESSAGE="Neptune ALARM: $1" ; fi

# use GoogleCL to add to calendar with 15m from now/1m reminder
google calendar add "$MESSAGE $FWD_TIME1" --reminder="1m"

RC=$?

if [ $RC -ne 0 ] ; then
sleep 30;
else
break ;
fi

done

The script uses GoogleCL (a python-based command-line utility for accessing Google). Installing GoogleCL is easy:


sudo apt-get install googlecl
# or (in case there is no ready-to install package):
wget http://googlecl.googlecode.com/files/googlecl-0.9.13.tar.gz
tar -zxvf googlecl-0.9.13.tar.gz
cd googlecl-0.9.13
sudo python setup.py install

Note: googleCL is launched via google not googleCL command.

The first time one uses googleCL, ie:


google calendar add "test from neptune 22/03/2013 10:30:22"

it will prompt for one's Google username. One has to type it in and hit enter. Next the user is asked to grant access permissions.

On non-gui systems (text-based) w3m browser usually will be launched. When connected to google one has to press q y and the text silimlar to the following will be displayed:


Please log in and/or grant access via your browser
at https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=\
4%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&hd=default then hit enter.

One has to copy the above link to another machine (with gui browser). Next one has to grant access and then go back to the terminal and hit enter to complete the authorization.

BTW the access token is stored in:


~/.local/share/googlecl/access_tok_USERNAME

If access is granted from some machine it will probably suffice to copy the access token file to another machine to grant access for it as well (not tested).

The configuration file for googleCL is in:


~/.config/googlecl

If access is grated successfully the message similar to the following will be displayed:


Event created: http://www.google.com/calendar/event?eid=\ anFsdGFucXFwdmZnbW1sMmcwc2t1NDI3cm8gbG9vc2VoZWFkcHJvcDFAbQ

Brak komentarzy:

Prześlij komentarz