Prerequisites

The prerequisites for this project are:

Step 1: Install the Snips Platform components

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

Step 2: Create a new Node.js project

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.

Step 3: Import the assistant model from the Console

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/

Step 4: Start the Snips Platform

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 mosquitto
brew services start snips-audio-server
brew services start snips-hotword
brew services start snips-tts
brew services start snips-nlu
brew services start snips-asr
brew services start snips-dialogue

You may stop the services at a later point by entering the following:

brew services stop snips-audio-server
brew services stop snips-hotword
brew services stop snips-tts
brew services stop snips-nlu
brew services stop snips-asr
brew services stop snips-dialogue

Step 5: Adding action code

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"}]}

Next steps

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: