andrewsnyder$ okgoogle

Bring the power of the Google Assistant to your command line.

Andrew Snyder
3 min readNov 19, 2020

Have you ever wanted to be able to talk to the Google Assistant in the command line, and have it speak back into your headphones? No?
Neither had I, but once I got it set up it’s actually pretty sweet. I can tell Google to yell at my kids across all my home speakers without even needing to open my mouth. It’s great.

If you are like me and also enjoy great things, and aren’t afraid of the terminal, read on.

PREREQUISITE: This article relies on the installation of Assistant Relay, a local Google Assistant server.

Also python3. You probably already have it but get it if you don’t.

Once you follow the instructions to install and configure Assistant relay and are able to make commands through the dashboard, return here to set up the cli utility.

Ready? Let’s go!

At this point you should be able to login to http://localhost:3000/sandbox, and send a command from your user, such as “What’s the weather like today?”, and successfully get a response.

This article assumes you are using MacOS, but you should be able to make adjustments to meet your needs

Show me the code

Let’s make a script okgoogle.sh with the following code

#!/bin/bashresponse_url=$(curl — silent -d command=”’$*’” -d user=YOUR_USERNAME_HERE localhost:3000/assistant | \
python3 -c “import sys, json; print(json.load(sys.stdin)[‘audio’])”)
curl — silent localhost:3000"$response_url" -o ~/Desktop/response.wavopen -a MuteChromesleep 0.5afplay ~/Desktop/response.wavopen -a MuteChromeopen -a “Activate iTerm”rm ~/Desktop/response.wav

Let’s look at it line by line.

The first step is to make our command to the assistant. This will take any arguments and parse them into a string. The command response gets saved on the returned relay server url, so we save that. Make sure to use your own username.

response_url=$(curl — silent -d command=”’$*’” -d user=YOUR_USERNAME_HERE localhost:3000/assistant | \
python3 -c “import sys, json; print(json.load(sys.stdin)[‘audio’])”)

Now we save that url to a local file somewhere. I just put it on my desktop since it's temp but you can put it wherever you want.

curl — silent localhost:3000"$response_url" -o ~/Desktop/response.wav

MuteChrome is a custom AppleScript I wrote to mute my chrome audio before playing the assistant response. This is optional, but I'll put it below in the article. That way if I have music playing (which I always do) it will mute briefly so I can hear the response. A brief sleep for smooth transition.

open -a MuteChromesleep 0.5

Then we play back the assistant audio response we’ve downloaded.

afplay ~/Desktop/response.wav

Once the audio has played, I resume my chrome audio (it’s a toggle so the command is the same) and then refocus my terminal. Again, this is optional. The only reason the terminal loses focus is the mute chrome command.

open -a MuteChromeopen -a “Activate iTerm”

Then finally, we just want to clean up that temp audio file. You could keep it for history if you want, but I don’t see much value in that since your google account should keep history anyway.

rm ~/Desktop/response.wav

Wrapping Up

Now that we’ve made our script, you will need to add it to your global path and make it executable. In your terminal run

mv okgoogle.sh /usr/local/bin
chmod +x /usr/local/bin/okgoogle.sh

Now finally, let’s give it a shot.

okgoogle what can you do?

You should hear a friendly voice rattle off some response.

Outro

As promised, the aforementioned MuteChrome AppleScript is as follows
(the key command being the default for the Chrome extension from here.)

on run {input, parameters}    tell application "Google Chrome"        activate    end tell    tell application "System Events"        keystroke "," using {option down, shift down}    end tellend run

That concludes this tutorial, enjoy your new keyboard assistant!

--

--