Search code examples
pythonactions-on-googlegoogle-home

How to make the Google Assistant get data from the web using a python script


I made a python script that is getting data about a timetable from a url. I now want to be able to ask my Google Home for certain hours or changes in my timetable which would be provided by the python script.

How can i get Google Home to take the information out of the python script and respond it to me when I say the keywords? Or is there a better way than doing it with python?

This is the script that gets the information needed

import requests

r = requests.get("*URL*")

jay = r.json()
result = jay["result"]["displaySchedule"]["lessonTimes"]


for i in result:
    try:
        print (i["changes"])
    except KeyError:
        continue

Solution

  • You will need to look into using Actions on Google, and probably Dialogflow, to integrate with the Google Assistant and provide Natural Language Processing (NLP) for your users' conversations.

    You can use whatever language you want. In this model, AoG will get what the user says, convert it to text, hand it off to Dialogflow which will match it with phrases you define, and then call your code. Your code will need to run at a public HTTPS URL at a server you control, receive a JSON POST request, do the processing (based on the code you provided above), and then return JSON in a defined format. (If you were using Node.js, there are some libraries to help you with this, but it isn't difficult if you're familiar with python, webapps, and JSON.)