Skip to content

Instantly share code, notes, and snippets.

@cmj
Created August 13, 2023 02:51
Show Gist options
  • Save cmj/b6e67ff66d70dffd34ce66d9334b115e to your computer and use it in GitHub Desktop.
Save cmj/b6e67ff66d70dffd34ce66d9334b115e to your computer and use it in GitHub Desktop.
Toggle, control lights and other smarthome devices
// https://github.com/endoplasmic/google-assistant
const path = require('path');
const GoogleAssistant = require('google-assistant');
const config = {
auth: {
keyFilePath: path.resolve(__dirname, '/home/cmj/.config/google-assistant/client_secret_xxxxxxxxxxxx.apps.googleusercontent.com.json'),
savedTokensPath: path.resolve(__dirname, 'tokens.json'),
},
conversation: {
audio: {
encodingIn: 'LINEAR16', // supported are LINEAR16 / FLAC (defaults to LINEAR16)
sampleRateIn: 16000, // supported rates are between 16000-24000 (defaults to 16000)
encodingOut: 'LINEAR16', // supported are LINEAR16 / MP3 / OPUS_IN_OGG (defaults to LINEAR16)
sampleRateOut: 24000, // supported are 16000 / 24000 (defaults to 24000)
},
lang: 'en-US',
// https://www.philips-hue.com/en-us/explore-hue/works-with/the-google-assistant/google-commands-for-hue-lights
textQuery: 'wave hue light %75 blue',
// other sockets, led strips, etc.
//textQuery: 'toggle crown lights',
//textQuery: 'toggle socket',
//textQuery: 'all lights 100% red',
//textQuery: 'toggle crown lights,
//textQuery: 'toggle front porch,
isNew: true, // set this to true if you want to force a new conversation and ignore the old state
screen: {
isOn: true, // set this to true if you want to output results to a screen
},
},
};
const assistant = new GoogleAssistant(config.auth);
// starts a new conversation with the assistant
const startConversation = (conversation) => {
// setup the conversation and send data to it
// for a full example, see `examples/mic-speaker.js`
conversation
.on('audio-data', (data) => {
// do stuff with the audio data from the server
// usually send it to some audio output / file
})
.on('end-of-utterance', () => {
// do stuff when done speaking to the assistant
// usually just stop your audio input
})
.on('transcription', (data) => {
// do stuff with the words you are saying to the assistant
})
.on('response', (text) => {
// do stuff with the text that the assistant said back
})
.on('volume-percent', (percent) => {
// do stuff with a volume percent change (range from 1-100)
})
.on('device-action', (action) => {
// if you've set this device up to handle actions, you'll get that here
})
.on('screen-data', (screen) => {
// if the screen.isOn flag was set to true, you'll get the format and data of the output
})
.on('ended', (error, continueConversation) => {
// once the conversation is ended, see if we need to follow up
if (error) console.log('Conversation Ended Error:', error);
else if (continueConversation) assistant.start();
else console.log('Conversation Complete');
})
.on('data', (data) => {
// raw data from the google assistant conversation
// useful for debugging or if something is not covered above
})
.on('error', (error) => {
// handle error messages
})
};
// will start a conversation and wait for audio data
// as soon as it's ready
assistant
.on('ready', () => assistant.start(config.conversation))
.on('started', startConversation);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment