Google Home and IFTTT automation

Automation is something people were curious about since ancient times. No surprise, most of us will benefit from automating routines that consume time and bring no obvious benefit. Have you ever dreamed to become the master of DIY automation? In this article, I will give a quick example, that automating things yourself is a simple and joyful process, and the limit of it – your imagination.

If you already know from the title what I am willing to describe and what is IFTTT and Flask then feel free to jump right to the “IFTTT + Python and Flask” section or even to the full code below.

Minimal requirements to get a result described in this article:
  • PC with Windows or Ubuntu
  • Python installed

IFTTT

IFTTT is the free tool to connect many of your apps together and make them talk. It allows you to set applets – little connectors that behave in a way of “If This Then That”. There are thousands of applets free and ready to be used. Example of one of this:
If Breaking news by NASAthen Send me an email at your@email.com

Sounds simple, isn’t it? It is simpler than checking your email actually. There are many services already available free of charge – they are endpoints which may be connected by applets. Most of the popular services you will find there: Google Assistant/Gmail/Photos/Drive/etc, Slack, Facebook, Instagram, Smart Home devices and so many others.

Python and Flask

Well, Python and Flask shouldn’t be a secret if you are a software developer, but if you are not – no worries. Python is an extremely friendly programming language and Flask is a web framework and python library.

The thing is that automating common routines like “send emails if I’ve got new task” is fun, but more fun will be something like if you say “Hey home, night mode” and then all the lights shut down, thermostat sets to nighttime temperature, and your Christmas tree starts flashing dimly. And yet you can accomplish many of these tasks using Python and IFTTT.

Google Assistant + IFTTT + Python and Flask

We are going to create a simple example – make google assistant recognize phrase “my PC action X” where X is any action and then run particular routine on your PC attached to action X (swap Google Assistant to any other IFTTT service of your choice if you don’t have Android 6.0 and higher on your device and don’t own Google Home).

First, we need to write a simple Python RESTful Web Server that will listen to incoming requests and executes our actions. I assume that you already have installed Python on your PC (if you don’t, google and do it first).
To install Flask:

pip install flask-restful

Then we will import flask libraries and dependencies and declare our REST API:

from flask import Flask
from flask_restful import Api, Resource, reqparse
import os


app = Flask(__name__)
api = Api(app)

Add resource class that will be our REST endpoint and will perform our X action:

class Action(Resource):
    def post(self, name):
        return name, 200
        
    def get(self, name):
        return name, 200    

Above ‘Action’ class has two methods which are GET and POST HTTP endpoints where GET method will be called any time we send something like:
http://localhost:5000/action/X

At the end, add our resource class to our REST API and run the web server:

api.add_resource(Action, "/action/")
app.run(debug=True, host='0.0.0.0')

Test it by executing “python yourfilename.py” in console, and don’t close it yet. Navigate to the browser and go to the URL: http://localhost:5000/action/testaction .
The result page should have “testaction” text in it.

Let's Try it All Together

We will redefine our post action routine, so it will play little waw file when a request is received by the above API. If you are an Ubuntu user then change Post routine to:

    def post(self, name):
        # Action example: play a song on Ubuntu when "handsome" 
        # received:
        if(name == "handsome"):
            os.system("aplay you-look-so-handsome.wav")    

For Windows users:

    def post(self, name):
        # Action example: play a song on Windows when "handsome" 
        # received:
        if(name == "handsome"):
            os.system("start you-look-so-handsome.wav")    

Make sure you have the audio file called “you-look-so-handsome.wav” (please look for a better one (o_O) and change the audio name in the code).

Important: Don’t forget to forward port 5000 on your router so IFTTT can access your python webserver. But be aware that forwarding ports gives the ability for any user to access your internal python web server and it is a potential attack point for hackers. (Here is how to Forward a Port)

Finally, we will run our python server and create the IFTTT applet. Navigate to IFTTT website:

  1. Select My Applets and click New Applet
  2. Click +this, type “Google A” and select Google Assistant
  3. Select Say Simple Phrase With Text Ingredient
  4. Type all the information as on the image below and click Create:
  5. Select +that, search and select “Webhooks”
  6. Click “Make a Web Request” and fill the information as on the next screenshot:
  7. Replace “your-real-ip” text to IP address assigned to you by your internet provider then click Create Action and Finish.

Done! Now any time you say “Hey Google, my PC, execute handsome”
Python web server will play a little wav audio file.

The full code can be found on Github: github.com/abordiuh/pyaction-restserver
Full file here:

from flask import Flask
from flask_restful import Api, Resource, reqparse
import os

app = Flask(__name__)
api = Api(app)

class Action(Resource):
    def get(self, name):
        return name, 200

    def post(self, name):
        # For adding additional parameters:
        # parser = reqparse.RequestParser()
        # parser.add_argument("arg_name")
        # args = parser.parse_args()
        # print(args["arg_name"])

        # Action exmple: play song on Ubuntu
        if(name == "handsome"):
            os.system("aplay you-look-so-handsome.wav")


api.add_resource(Action, "/action/")
app.run(debug=True, host='0.0.0.0')