Documentation

Powered by Algolia

Triggering activities without voice commands

When building a voice assistant, it may be necessary to trigger an action without a voice command from the user. For example, you may want the voice assistant to provide details about an item when the user taps this item in the app.

In this tutorial, we will let users hear the current count when the Click me! link is tapped. To do this, we will use Alan’s project API. The project API functionality allows sending any information from the app to the voice script and triggering activities without a voice command. To use it, we need to do the following:

  1. Define a new project API method in the voice script
  2. Call this method from the app

What you will learn

  • How to send arbitrary data from the voice script to the app
  • How to trigger activities without a voice command

What you will need

Step 1. Add a project API method to the voice script

First, we will define a project API method in the voice script. In the code editor in Alan Studio, add the following code:

projectAPI.getCount = function(p, param, callback) {
    p.play(`It is ${param.count}, keep going`);
    callback();
};

When this method is called, Alan will do the only thing — play the current count to the user.

Step 2. Call the project API method from the app

Now, we need to call the defined method from the app.

  1. In the App.js file, update the following import statement to include useRef:

    ...
    import React, { useState, useEffect, useRef } from 'react';
    ...
  2. Update the app:

    const App = () => {
      ..
      const notInitialRender = useRef(false);
    
      useEffect(() => {
        if (notInitialRender.current) {
          AlanManager.activate();
          AlanManager.callProjectApi("getCount", {count}, function(error, result) {
            console.log(error, result);
          })
        } else {
          notInitialRender.current = true
        }
      }, [count]);
    ...
    }

Here is how it works: every time the count changes, the Alan button is automatically activated with Alan's activate() client API method and the getCount project API method is called. The current count is passed to the voice script, and Alan plays the following phrase on the server side: It is <count>, keep going.

You can try it: tap the link in the app. Alan will accompany each tap with the count phrase playback.

What's next?

Have a look at the next tutorial: Navigating between screens with voice.