How to make a webapp/server that can read & react to emails

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:

receive_mail.py:

handlers_mail.py:

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!

How to teach Spyno to spy on (almost) anything

Spyno likes JSON. It’s a JSONivore. So when you make a Spyno agent, you need to feed it a source URL in JSON format.

But what if you want to spy on things that are not in JSON? No worries. We have two tricks:

Trick 1. Yahoo Query Language YQL free service

“Use YQL to convert XML to JSON & vice versa. Access atom, rss, micro formats and more. You can even load CSV files from anywhere.”

So whether your favorite site is in RSS or XML, no problem. Just send it to Google Feed to transform it to JSON.

For example, say one of your guilty pleasures is Perez Hilton (don’t ask):

  1. Find the RSS feed URL (E.g., http://i.perezhilton.com/?feed=rss2)
  2. Drop the following query into this YQL form:

  3. That returns you the following Endpoint, which you can copy-paste into Spyno

Trick 2. Google Feed API

“With the Feed API you can download any public Atom, RSS, or Media RSS feed using only JavaScript, so you can easily mash up feeds with your content and other APIs.”

Trick 3. Kimonolabs API

Kimono is like the Swiss Army kitchen sink that turns any website into a JSON feed. It’s designed for scraping websites. But what’s special about it is that you can teach it what you want scraped. All you have to do is open the website you’re interested in using Kimono, then click elements on the page you want:

And these two tricks are about all you need to make JSON feeds for your new Spyno Agent.

Happy scraping, Spyno trainers!

How to view Spyno from anywhere

When you’re on your PC, open Spyno by clicking on him at the top right of Chrome:

OH HAI

OH HAI

But what if you’re not at your PC? Visit your Public URL, of course!

Here’s how you get the Public URL for your Spyno page:

  1. Type chrome://extensions in your Chrome’s address bar. Hit Enter.
  2. Visit Spyno’s options page
  3. Click

    Click

  4. Scroll to the bottom. Click “Get code”:
    Click

    Click

  5. Sign in with Google and you’ll see this:
    Code Page

    The link marked (1) is your public URL. Send that to your phone to view Spyno from your phone. Copy the code marked (2) to your PC’s Spyno option page. Spyno uses that code to update your public page.


    Paste your code here and click Save

    Paste your code here and click Save

  6. Ta-da!

Now you can take Spyno with you anywhere.

Next: How to teach Spyno to spy on (almost) anything

Secret Spyno: How to customize your Spyno page layout

Spyno Agents appear in columns, like a newspaper. The number of columns you see depends on the width of your screen. Agents are drawn left to right until the edge of the screen. Then they continue on the left in the next row, like a typewriter.

E.g.,

1 | 2 | 3 | 4
5 | 6 | 7 | 8
9 |

To control the order in which your Agents are drawn, edit the Layout in the options page. Layout takes 2 formats:

1) a list

2) a list of tabs

Make your own Spyno page

This is what the tabs layout above gives

Tip: Spacers

Sometimes you’ll find that some columns end up longer than others. You may add “Spacer” agents to skip long columns. Just add a “spacer” in the layout like I did in the tabs example above.

Next: How to view your public Spyno page from anywhere

Secret Spyno: How to add a new agent

Your Spyno Agents do all the work, so you don’t have to. Train agents for each website you want to gather information from. You tell each agent what information to grab, as well as how to present the information to you. Here’s how…

  1. Visit in Chrome’s address bar
  2. Click

    Open the options page

  3. You'll see this Spyno options page

    You’ll see this Spyno options page

  4. Scroll to the bottom and click in the box for "Add New Agent"

    Scroll to the bottom and click in the box for “Add New Agent”

Agent example (options explained below):

Line 2 name: unique name for your Spyno agent (required)
Line 4 periodInMinutes: how often you would like Spyno to fetch from the source URL
Line 6 url: the source URL that has the info you want Spyno to fetch
Line 7 extract: this section tells Spyno what info to grab and how you want it presented
Line 8 root: JSONPath to the list of records you’re interested in
Line 9 fields: JSONPath to the fields you’re interested in, and what name you want to give them
Line 14 template: Handlebars template to tell Spyno how to draw the info you fetched. Spyno automatically packages your info into a list of “rows”, so your template should always have

You may include any html in your template, including ‹style›. E.g.,

Next: How to design a layout for your Spyno page