The prerequisites for this project are:
For this step, we assume that you have Homebrew version 1.7.x or higher installed. If not, please follow the installation instructions on the Homebrew website.
First, add the Snips tap to Homebrew. In a terminal window, enter the following command:
brew tap snipsco/homebrew-snips
Then, install the Snips Platform components:
brew install snips-asr snips-hotword snips-dialogue snips-watch \snips-audio-server snips-nlu snips-tts
Additionally, install the Mosquitto MQTT client:
brew install mosquitto
Next, create a folder for your project in a location of your choice. Let's name it "HelloSnips-js". Open a terminal window, change to that directory, and type the following to initialise the Node.js project:
npm init
This will create a file named package.json
used for configuring your project, managing dependencies and more.
If you haven't done so already, head over to console.snips.ai and create your assistant. If this is the first time you are creating an assistant, make sure to follow the Quick Start Console guide:
Once ready, download the assistant file, unzip it, and place it in the following folder:
/usr/local/share/snips/assistant/
For this demo, we need several services to be running. In the terminal window, type the following to start the various Snips Platform components:
brew services start mosquittobrew services start snips-audio-serverbrew services start snips-hotwordbrew services start snips-ttsbrew services start snips-nlubrew services start snips-asrbrew services start snips-dialogue
You may stop the services at a later point by entering the following:
brew services stop snips-audio-serverbrew services stop snips-hotwordbrew services stop snips-ttsbrew services stop snips-nlubrew services stop snips-asrbrew services stop snips-dialogue
In order for the assistant to actually do something, such as telling you the weather forecast, we will have to write some handler code. This is code which is executed when a certain event happens on the Snips Platform, such as when an intent has been detected.
In the Node project, we start by adding the mqtt
package dependency.
npm install mqtt --save
Now, create a file called index.js
, and paste the following code,:
var mqtt = require('mqtt');var hostname = "mqtt://localhost:1883";var client = mqtt.connect(hostname);client.on('connect', function () {console.log("[Snips Log] Connected to MQTT broker " + hostname);client.subscribe('hermes/#');});client.on('message', function (topic, message) {if (topic === "hermes/asr/startListening") {onListeningStateChanged(true);} else if (topic === "hermes/asr/stopListening") {onListeningStateChanged(false);} else if (topic.match(/hermes\/hotword\/.+\/detected/g) !== null) {onHotwordDetected()} else if (topic.match(/hermes\/intent\/.+/g) !== null) {onIntentDetected(JSON.parse(message));}});function onIntentDetected(intent) {console.log("[Snips Log] Intent detected: " + JSON.stringify(intent));}function onHotwordDetected() {console.log("[Snips Log] Hotword detected");}function onListeningStateChanged(listening) {console.log("[Snips Log] " + (listening ? "Start" : "Stop") + " listening");}
Run this code using Node:
node index.js
You should be askde by macOS for recording permissions, and once granted, see a message of a successful connection to the Snips MQTT broker. Say something to your computer's microphone:
Hey Snips, what is the weather in New Delhi
You should see the logs updating:
[Snips Log] Connected to MQTT broker mqtt://localhost:1883[Snips Log] Hotword detected[Snips Log] Start listening[Snips Log] Stop listening[Snips Log] Intent detected: {"sessionId":"c84b5aa5-3f14-4218-975e-8872b9217933","customData":null,"siteId": "default","input":"what is the weather going to be tomorrow in new delhi","intent":{"intentName":"searchWeatherForecast","probability":0.73845243},"slots":[{"rawValue":"new delhi","value":{"kind":"Custom","value":"New Delhi"},"range":{"start":44,"end":53},"entity":"locality","slotName":"forecast_locality"}]}
The ball is in your camp! You now have a basic skeleton app running, capable of reacting to a new intent, and other platform messages. For more information on the messages emitted from the Snips Platform, check out the following references: