google-home-client-go | Google Home client for Golang

Posted: June 17, 2020


You can have Google Home speak a text.

Link: https://github.com/evalphobia/google-home-client-go


Overview

Google Home can play audio file from network.To have Google Home speak, you should pass the url of voice audio file. You can generate audio file through Google’s TTS(Text-To-Speech) service. Google has a paid service for Cloud TTS (It’s free for first 1M letters though). But for hobby use, it’s bothersome to setup Google Cloud account.

You maybe have used Google Translate to translate other language in your browser. You also might push Listen button to hear the pronunciation in the page. That’s what we used in the library.

The audio file link will be this format.

https://translate.google.com/translate_tts?tl=<language>&q=<text>

But using just this url and a few use, you can easily get rate limit for Google Translate.

You need add token to use this API from same IP address and device. You can get the token from the Google Translate page (in JS code).

To get this token, google-home-client-go use evalphobia/google-tts-go in the internal logic.

Example Code

code below is from README,

import (
	"github.com/evalphobia/google-home-client-go/googlehome"
)

func main() {
	cli, err := googlehome.NewClientWithConfig(googlehome.Config{
		Hostname: "192.168.0.1",
		Lang:     "en",
		Accent:   "GB",
	})
	if err != nil {
		panic(err)
	}

	// Speak text on Google Home.
	cli.Notify("Hello")

	// Change language
	cli.SetLang("ja")
	cli.Notify("こんにちは、グーグル。")

	// Or set language in Notify()
	cli.Notify("你好、Google。", "zh")

  //...
}

You can omit googlehome.Config{}.Hostname and use it from env variable GOOGLE_HOME_HOST.

To have Google Home speak via Slack bot, I suggest to use these two libs,