Triggering activities without voice commands (React Native)

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:

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

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