I’ve spent a lot of time talking to the Google Assistant since joining the Voice Lab team. I’ve also spent a lot of time trawling through the platform’s documentation. It makes me wonder what would have happened if I could have just ask Google Assistant all my questions. Would pairing with an “all-knowing robot” be the ultimate way to get things done?
In an office somewhere in the future...
Human: Hey Google, there are a lot of blog posts on serverless technology, it must be the next big thing. How can I make something that’s serverless?
Google Assistant: Ok, I can help with that. Google has Cloud Functions for Firebase. This tutorial will help you make a HTTPS function.
Some time passes…
Human: Hey Google, this is terrible. Every time I want to test my changes I have to deploy my function; it takes several minutes and the time is really adding up. How do I test it locally?
Google Assistant: There are two ways to test a Cloud Function locally. The first is using the Firebase cli. Use the firebase serve command to get a local URL that you can hit.
Human: Hey Google, this doesn’t work. Why can’t I see my changes?
Google Assistant: Are you sure it doesn’t work?
Human: Yes! Oh wait…
Google Assistant: Did you forget to transpile your Typescript code?
Human: Yes. Stupid all-knowing robot.
Human: Hey Google, tell me the other way to test my function.
Google Assistant: You can use the firebase functions:shell command from the Firebase cli then call the function by its name.
Not much time passes…
Human: Hey Google, I want to use query params now. How do I do that from the shell?
Google Assistant: Try this:
It’s just like using regular the request npm module. Here are a few more examples.
The next day...
Human: Hey Google, I’m using an API key now. I’m very responsible so I have set my key as an environment variable and not done anything silly like checking my key into GitHub. How do I set up local environment variables?
Google Assistant: Your variables should be stored as JSON in a file called .runtimeconfig.json. This file needs to be at the root of your functions directory. Running firebase functions:config:get > .runtimeconfig.json will make the file for you. Don’t forget to add it to your .gitignore file.
Later that evening…
Human: Hey Google, I need to put stuff in a database now.
Google Assistant: Sure, no problem. I can help you with putting stuff in a database. You can use a Firebase Realtime Database.
Human: Do you ever recommend anything not made by Google?!
This looks good. There is web documentation I can use. That’s close enough right?
Google Assistant: No, to use Realtime Database with Cloud Functions follow the admin documentation. It’s quite different.
Several months later…
Human: Hey Google, remember how you helped me with my cloud function project?
Google Assistant: Yes, I do remember helping with your cloud function project. I remember everything we talk about. Has it stopped working?
Human: No, but I’m making a Google Action now and I can’t run the cloud function locally. How do I run it?
Google Assistant: How nice of you to make something for me. I can help you with that. It is a bit more complicated to run a cloud function for a Google Action locally as the payload is quite complicated.
What you need to do is make a POST request to your function. You can look at the documentation to see the data you need to send with a POST request. It’s probably easier to look at the request being made in the Google Actions Simulator to get you started. Once you have the data saved in a file you can try:
Five minutes later...
Human: Hey Google, how can I curl the local URL directly with the json file I have?
Google Assistant: I’m afraid I can’t help you with that, but I’m learning more all the time.
Human: Fine…
Human: Hey Google, play Radio 2
Radio 2 plays...
Unfortunately I have never been able to get my firebase function to work locally with cURL. Aside from that I have found running cloud functions locally really improves the developer experience and it is not often talked about in ‘getting started’ guides. The questions the ‘human’ asks ‘Google Assistant’ are all the changes I have made to the way I develop for Google Assistant to ensure that I can get feedback quickly without having to deploy the code first.
Find out more about the Voice Lab’s mission or get in touch at voicelab@theguardian.com.