Get started with action snippets in the console

Overview

Actions run once an Intent has been caught.

Snippets are the easiest way to create quick and easy actions. They are useful if you just want to implement a simple vocal feedback, or very simple logic. For more complex actions, we recommend you to code your action directly on your computer and use the Github option to link it to your app.

We're going to present how code snippets work on a simple smart lights example, in which the action strictly repeats what it understood (without performing anything else).

To get started, in the Actions tab of your App, select Code Snippets:

Let's consider your application contains two intents: lightsSet and lightsTurnOff. For each of these intents, an optional slot house_room can be defined, to make it possible for the users to determine in which room they want to control the lights (eg "Turn the lights on in the kitchen")

Before you start coding, the code snippet for each intent is going to be empty:

The code of the action for the two intents could like like this:

lightsSet

if len(intentMessage.slots.house_room) > 0:
house_room = intentMessage.slots.house_room.first().value # We extract the value from the slot "house_room"
result_sentence = "Turning on lights in : {}".format(str(house_room)) # The response that will be said out loud by the TTS engine.
else:
result_sentence = "Turning on lights"
current_session_id = intentMessage.session_id
hermes.publish_end_session(current_session_id, result_sentence)

lightsTurnOff

if len(intentMessage.slots.house_room) > 0:
house_room = intentMessage.slots.house_room.first().value # We extract the value from the slot "house_room"
result_sentence = "Turning off lights in : {}".format(str(house_room)) # The response that will be said out loud by the TTS engine.
else:
result_sentence = "Turning off lights"
current_session_id = intentMessage.session_id
hermes.publish_end_session(current_session_id, result_sentence)

You’re all set! Once intents are trained, and the action code logic is implemented, you can deploy the assistant on your Raspberry Pi!

For this, we recommend using Sam, our Snips Assistant Manager (Sam).

To know more about snippets, keep reading. To have even more details on how they are deployed, jump right into Deploy your app.

App Parameters

In this example, we didn't define parameters. When parameters are defined in the App Parameters section, they will be contained in a dictionary.

To access global or end-user parameters, you can respectively useconf['global']['parameterName'] or conf['secret']['parameterName']

How it works behind the scene

We have written a template python code required to run your code snippets when intents are detected:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import ConfigParser
from hermes_python.hermes import Hermes
from hermes_python.ontology import *
import io
CONFIGURATION_ENCODING_FORMAT = "utf-8"
CONFIG_INI = "config.ini"
class SnipsConfigParser(ConfigParser.SafeConfigParser):
def to_dict(self):
return {section : {option_name : option for option_name, option in self.items(section)} for section in self.sections()}
def read_configuration_file(configuration_file):
try:
with io.open(configuration_file, encoding=CONFIGURATION_ENCODING_FORMAT) as f:
conf_parser = SnipsConfigParser()
conf_parser.readfp(f)
return conf_parser.to_dict()
except (IOError, ConfigParser.Error) as e:
return dict()
def subscribe_intent_callback(hermes, intentMessage):
conf = read_configuration_file(CONFIG_INI)
action_wrapper(hermes, intentMessage, conf)
def action_wrapper(hermes, intentMessage, conf):
{{#each action_code as |a|}}{{a}}
{{/each}}
if __name__ == "__main__":
with Hermes("localhost:1883") as h:
h.subscribe_intent("{{intent_id}}", subscribe_intent_callback) \
.start()

When you install your assistant with Sam, it will also install snippets. To do that, we use the command snips-template render Snippets you write in the console will be injected in {{#each action_code as |a|}}{{a}}

Hermes Python

We built a library called Hermes python. It handles the emmission and reception of messages on MQTT. It also exposes an intentMessage object that contains the intent's information extracted from the user's requests.

Snips Config Parser

For each snippets, the app parameters are stored in a file called config.ini. To access global parameters from the snippet, use conf['global']['parameterName']. For end-user parameters use conf['secret']['parameterName']