In the last post, we talked about how to teach your Google Home device (or Google Assistant on your Android phone) to send emails.
But that itself is not too interesting (unless you really enjoy spamming yourself). What would be more useful is if you create a program to read and act on the emails automatically. Because, then, you have the ingredients for building your own Virtual Assistant.
The simplest (and cheapest way) I found to do this is using Google App Engine. It has a generous daily free quota. For all my home automation projects, I haven’t run above the free limit.
Start by following the “hello world” instructions to make your first web app:
https://cloud.google.com/appengine/docs/standard/python/quickstart
Then, add the files/code below to enhance your web app to handle emails. (Note: the code I provided below is an example of how I use email to control my home sprinkler system. This lets me say “Turn on sprinkler 1” to my Google Home Mini, which triggers IFTTT to send an email to my web app, which reads the email and triggers my home sprinkler system to turn on zone 1.)
app.yaml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
application: your_appengine_application_name version: 1-0 runtime: python27 api_version: 1 threadsafe: true default_expiration: "30m" handlers: - url: /_ah/mail/.+ script: receive_mail.app login: admin inbound_services: - mail |
receive_mail.py:
1 2 3 4 5 |
from handlers_mail import * import webapp2 from google.appengine.ext.webapp.mail_handlers import InboundMailHandler app = webapp2.WSGIApplication([ReceiveMail.mapping()], debug=True) |
handlers_mail.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import logging import webapp2 from google.appengine.ext.webapp.mail_handlers import InboundMailHandler class ReceiveMail(InboundMailHandler): def receive(self, message): tos = message.to.split(',') for to in tos: logging.info("received mail from %s %s %s" \ % (message.sender, to, message.subject)) if 'sprinkler' in to: handleSprinklerEmail(message) def handleSprinklerEmail(message): if 'your@email.com' not in message.sender: # sender unknown - ignore this message return logging.info("sprinkler email request from %s %s" \ % (message.sender, message.subject)) # process email to get body bodies = message.bodies('text/plain') bodyText = '' for ctype, body in bodies: lastLine = [i for i in body.decode().split('\n') if i][-1] bodyText = lastLine break # you can now decide what to do based on: # message.subject # bodyText |
After you set up the above, you can send emails to your Google App Engine web app using its special address:
this_can_be_anything@your_appengine_application_name.appspotmail.com
Now that you have a web app, there’s much more you can do beyond handling emails. It opens up a couple more opportunities for your home automation. E.g.:
- Use this web app as a trusted HTTP server that your smart devices can fetch instructions from
- Use this web app to serve a web GUI for your devices/projects
- Use this web app (which supports HTTPS) to authenticate users and securely control your smart devices
Give it a go. I think the code I gave you above should work. If I missed something, please let me know at aaron@secretsciencelab.com. Happy hacking!