Google is known for having an API for just about everything and one of them can be used to receive SMS notifications from your embedded electronics project, your robot or from some sensor grid in your evil underground laboratory. In this case “free” comes with a price. You’re limited to ~60 characters. But, that should be enough for sending simple notifications or alerts. I have originally used this to alert users that they have received a new message in a project that was supposed to make texting from mobile phones a lot cheaper by using mobile data. And it did (the cost of one SMS was around 0.01$) but the project never gained momentum, mainly because I didn’t need cheap text messages anymore. Long story short, the project involved one HTTP API implemented in Python (on Google App Engine) and one J2ME application for phones. This worked because mobile Internet access was cheaper than SMS messages in my country.
Anyways, let’s get back to free SMS notifications. Here’s a simple Python class that you can use to send SMS notifications:
1 import atom 2 import gdata.calendar 3 import gdata.calendar.service 4 import time 5 6 TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.000Z' 7 CALENDAR_URL = 'http://www.google.com/calendar/feeds/default/private/full' 8 9 class GoogleSms: 10 def __init__(self, username, password): 11 self.username = username 12 self.password = password 13 14 service = gdata.calendar.service.CalendarService() 15 service.email = username 16 service.password = password 17 service.source = 'GoogleSms' 18 service.ProgrammaticLogin() 19 20 self.calendar_service = service 21 22 def send(self, message): 23 # Set time to one hour from now 24 event_time = time.strftime(TIME_FORMAT, time.gmtime(time.time()+3600)) 25 26 event = gdata.calendar.CalendarEventEntry() 27 event.title = atom.Title(text=message) 28 event.content = atom.Content(text=message) 29 30 # Send a reminder 60 minutes before the event. 31 # Since the event is 60 minutes from now, we will receive the message 32 # in a few seconds. 33 reminder = gdata.calendar.Reminder(minutes=60) 34 reminder.method = 'sms' 35 36 when = gdata.calendar.When(event_time) 37 when.reminder.append(reminder) 38 39 event.when.append(when) 40 41 try: 42 # Add the entry to calendar 43 cal_event = self.calendar_service.InsertEvent(event, CALENDAR_URL) 44 except gdata.service.RequestError, request_exception: 45 raise 46 47 if __name__ == '__main__': 48 sms = GoogleSms('[email protected]', 'yourpassword') 49 sms.send('Alert!')
Basically, we are abusing the Google Calendar API in order to send SMS notifications. We create an event that is one hour from now and setup a reminder one hour before the event. This way the reminder is sent in a few seconds from inserting the event. So, there you have it, a simple and free way of sending SMS notifications. There’s a lot of space for improvements here but I will leave that as an exercise for the reader.
Update: You need to have a mobile phone number associated with your Google Calendar. Go to Settings → Mobile Setup in Google Calendar to setup your phone number.