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.

Prerequisites

For this tutorial, we assume you have the following:

Preparing your device

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

Your Pi must be on the same network as your computer. For help on setting up a Pi on a Wi-Fi network, see our Raspberry Pi Network Connection guide.

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.

Creating the assistant

Head over to the Snips Console:

Open 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.

Adding the app

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

This will generate a Snipsfile in the current working directory.

Pointing to your Console assistant

Open the Snipsfile, find the line starting with assistant_url, and replace it with assistant_id: YOUR_ASSISTANT_ID, where YOUR_ASSISTANT_ID is the ID of your assistant:

assistant_id: YOUR_ASSISTANT_ID

The ID can be found in the URL of your assistant in the Console:

Adding the handler code

When your created the project above, Sam automatically added a sample app under the skills section in the Snipsfile. Remove this section, and replace it with the following:

skills:
  - name: calculator
    intents:
      - intent: GetSum
        action: |
          {%
          sum = int(snips.intent.firstTerm) + int(snips.intent.secondTerm)
          snips.dialogue.speak(str(sum))
          %}
      - intent: GetProduct
        action: |
          {%
          product = int(snips.intent.firstTerm) * int(snips.intent.secondTerm)
          snips.dialogue.speak(str(product))
          %}

What is happening here? One app, named calculator, has been added. It reacts to two intents, GetSum and GetProduct which are present in the Calculator Bundle.

The action section contains a code block with some handler code. The code here is written in Python. It must be delimited by the {% and %} characters. In this code, an object snips is accessible. It gives access to two objects: snips.intent and snips.dialogue. The snips.intent object gives you access to slot values: firstTermand secondTerm. The snips.dialogue object can be used to speak a sentence, using the speak() method.For more information on the snips object, see the Snips Object Reference.

We leave it to you to add the remaining GetDifference and GetQuotion intents.

Testing the app locally

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 test command. For instance, to test that the app properly reacts to a GetSum intent, enter the following command:

$ sam test skill calculator intent=GetSum firstTerm=3 secondTerm=5

You should see the output TTS says: 8.

Deploying to your device

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!

If you have any issues, check out our in-depth Assistant Troubleshooting Guide.

Last updated