The design philosophy of SnipsKit is:
- It should be easy to create a Snips app with minimal boilerplate code.
- All SnipsKit code should behave by default in a sensible way, tuned for a Snips installation.
This document presupposes that you:
Creating Snips apps using Hermes Python¶
You can create a Snips app using Hermes Python by subclassing the
HermesSnipsApp class. This is a convenience class that removes a lot of boilerplate code, which it executes behind the curtains without you having to think about it.
But you still have to know how to use the Hermes Python library, because you’ll use it to read slots, publish messages, and so on. Think of the
HermesSnipsApp class as an addon that makes your life in the Hermes Python world a little easier.
Creating a simple Snips app using Hermes Python¶
A simple Snips app that listens to a specific intent and then says a message, is created like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env python3 from snipskit.hermes.apps import HermesSnipsApp from snipskit.hermes.decorators import intent class SimpleSnipsApp(HermesSnipsApp): @intent('User:ExampleIntent') def example_intent(self, hermes, intent_message): hermes.publish_end_session(intent_message.session_id, 'I received ExampleIntent') if __name__ == "__main__": SimpleSnipsApp()
You can download the file hermes_listen_for_intent.py from our GitHub repository.
Let’s dissect this code. In line 1, we signal to the shell that this file is to be run by the Python 3 interpreter. In lines 2 and 3 we import the
HermesSnipsApp class and the
intent() decorator that we use.
Beginning from line 6 we define a class for our Snips App, inheriting from the
HermesSnipsApp app. By inheriting from this class, you get a lot of functionality for free, which we’ll explain in a minute.
In line 9, we define a callback method. This method will be called when an intent is recognized. Which intent? This is defined by the decorator
intent() in line 8. So this line says: “If the intent ‘User:ExampleIntent’ is recognized, call the method example_intent.
Then inside the example_intent method, you can use the hermes and intent_message objects from the Hermes Python library. In this simple case, we end the session by saying a message.
In line 14, we check if the Python file is run on the commandline. If it is, we create a new object of the class we defined. When we initialize this object, it automatically reads the MQTT connection settings from the snips.toml file, connects to the MQTT broker, registers the method with the
intent() decorator as a callback method for the intent ‘User:ExampleIntent’ and starts the event loop so the app starts listening to events from the Snips platform.