Comment on page
Apps 101: Building a simple calculator app
A tutorial on building a app performing basic arithmetic operations using the Snipsfile.
This tutorial is outdated and it will be soon updated.
For this tutorial, we assume you have the following:
This tutorial is using the Snips Assistant Manager (
sam) for setting up and deploying the assistant to your Pi. Make sure it is installed on your computer. If not, check out the Getting Started Guide. Once installed, connect to your Pi:
$ sam connect
If you haven’t done so already, install the Snips toolbelt on the Pi:
$ sam init
This will take a little moment, which you may use to start the next step, and create an assistant in the Console.
Head over to the Snips Console:
Create an account if you don’t have one yet, and create a new assistant with your name of choice. For this tutorial, we will call it “Von Neumann”:
Add the Calculator bundle, which add intents that detect queries for sums, products, differences and quotients (don't forget to uncheck Only show apps with actions):
If you want to add more intents, such as computing the square root of a number, you can do so by adding a new intent to the assistant.
Once the assistant has been created in the Console, you can create the app that will bind intents with the actual action of performing the arithmetic operations and speak the answer. First, create a new project locally on your computer:
$ sam create
Open the Snipsfile, find the line starting with
assistant_url, and replace it with
assistant_id: YOUR_ASSISTANT_ID, where
YOUR_ASSISTANT_IDis the ID of your assistant:
The ID can be found in the URL of your assistant in the Console:
When your created the project above, Sam automatically added a sample app under the
skillssection in the Snipsfile. Remove this section, and replace it with the following:
- name: calculator
- intent: GetSum
sum = int(snips.intent.firstTerm) + int(snips.intent.secondTerm)
- intent: GetProduct
product = int(snips.intent.firstTerm) * int(snips.intent.secondTerm)
What is happening here? One app, named
calculator, has been added. It reacts to two intents,
GetProductwhich are present in the Calculator Bundle.
actionsection contains a code block with some handler code. The code here is written in Python. It must be delimited by the
%}characters. In this code, an object
snipsis accessible. It gives access to two objects:
snips.intentobject gives you access to slot values:
snips.dialogueobject can be used to speak a sentence, using the
speak()method.For more information on the
snipsobject, see the Snips Object Reference.
We leave it to you to add the remaining
Before deploying the assistant to your Pi, it is wise to check that the app works as expected. You can simulate intent messages using the
sam testcommand. For instance, to test that the app properly reacts to a
GetSumintent, enter the following command:
$ sam test skill calculator intent=GetSum firstTerm=3 secondTerm=5
You should see the output
TTS says: 8.
Once you are satisfied that your app is working, you can deploy to your device:
$ sam deploy
After a short time, your assistant should be ready to take your voice commands. Try out:
Hey Snips, what is 10 times 15
You should hear the answer being spoken out!