Angular


Integrating with Alan

To integrate an Angular app with Alan:

  1. Add the Alan Web SDK to your app. You can do it in two ways:

    • With the npm package

    • In the browser library mode

    To install the Alan Web SDK with the npm package:

    1. In the Terminal, run the command:

      Terminal
      npm install @alan-ai/alan-sdk-web --save
      
    2. Import alanBtn to your Angular component:

      Client app
      import alanBtn from "@alan-ai/alan-sdk-web";
      

    To load the Alan Web SDK in the browser library mode, add the alan_lib.min.js library to the index.html file using the <script> tag:

    Client app
    <script type="text/javascript" src="https://studio.alan.app/web/lib/alan_lib.min.js"></script>
    
  2. Add the Alan button to your component:

    Client app
    export class AppComponent {
    
      alanBtnInstance;
    
      constructor(){
        this.alanBtnInstance = alanBtn({
          key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
          onCommand: (commandData) => {
            if (commandData.command === 'go:back') {
              // Call the client code that will react to the received command
            }
          },
        });
      }
    }
    

Note

Regularly update the @alan-ai/alan-sdk-web package your project depends on. To check if a newer version is available, run npm outdated. To update the alan package, run npm update @alan-ai/alan-sdk-web. For more details, see npm documentation.

Specifying the Alan button parameters

You can specify the following parameters for the Alan button added to your app:

Name

Type

Description

key

string

The Alan SDK key for a project in Alan Studio.

authData

JSON object

The authentication or configuration data to be sent to the voice script. For details, see authData.

rootEl

HTMLElement

The element where Alan button will be added. If no rootEl is provided, the Alan button is added to the body.

showOverlayOnMicPermissionPrompt

Boolean

The property signaling whether the overlay fade effect must be used when the microphone permission prompt in the browser is displayed. The overlay effect makes the prompt more noticeable and helps make sure users provide microphone access to the voice assistant.

onCommand

function

A callback for handling commands from the Alan voice script. In this callback, you can set up logic on how your app must react to commands received from the voice script. For details, see onCommand handler.

onButtonState

function

A callback for receiving the connection state of the Alan button. For details, see onButtonState handler.

onConnectionStatus

function

A callback for receiving the connection state of the voice project run in the Alan Cloud. For details, see onConnectionStatus handler.

onEvent

function

A callback responsible for handling events received from Alan. For details, see onEvent handler.

Changing the Alan button position

By default, the Alan button is placed in the bottom right corner of the window. To change the Alan button position, you can use the following options for the alanBtn variable:

  • left: sets the Alan button position from the left edge

  • right: sets the Alan button position from the right edge

  • top: sets the Alan button position from the top edge

  • bottom: sets the Alan button position from the bottom edge

  • zIndex: sets the z-order of the Alan button

Client app
alanBtn({
   key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
   bottom: '50px',
   left: '50px',
   zIndex: 10
});

Using client API methods

You can use the following client API methods in your Angular app:

setVisualState()

Use the setVisualState() method to inform the voice assistant about the app’s visual context. For details, see setVisualState().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public setVisualState() {
    this.alanBtnInstance.setVisualState({data: "your data"});
  }
}
app.component.html
<div>
  <a class="card" (click)="setVisualState()">
    <span>Set visual state</span>
  </a>
</div>

callProjectApi()

Use the callProjectApi() method to send data from the client app to the voice script and trigger activities without voice commands. For details, see callProjectApi().

Voice script
projectAPI.setClientData = function(p, param, callback) {
  console.log(param);
};
app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public callProjectApi() {
    this.alanBtnInstance.callProjectApi("setClientData", {value:"your data"}, function (error, result){
      // handle error and result here
    });
  }
}
app.component.html
<div>
  <a class="card" (click)="callProjectApi()">
    <span>Call setClientData method</span>
  </a>
</div>

playText()

Use the playText() method to play specific text in the client app. For details, see playText().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public playText() {
    this.alanBtnInstance.playText("Hello, I am Alan!");
  }
}
app.component.html
<div>
  <a class="card" (click)="playText()">
    <span>Play text</span>
  </a>
</div>

playCommand()

Use the playCommand() method to execute a specific command in the client app. For details, see playCommand().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
      onCommand: (commandData: any) => {
        if (commandData.command === 'goBack') {
          // Call client code that will react to the received command
        }
      },
    });
  }

  public playCommand() {
    this.alanBtnInstance.playCommand({command: "goBack"});
  }
}
app.component.html
<div>
  <a class="card" (click)="playCommand()">
    <span>Play command</span>
  </a>
</div>

activate()

Use the activate() method to activate the Alan button programmatically. For details, see activate().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public activateButton() {
    this.alanBtnInstance.activate();
  }
}
app.component.html
<div>
  <a class="card" (click)="activateButton()">
    <span>Activate Alan button</span>
  </a>
</div>

deactivate()

Use the deactivate() method to deactivate the Alan button programmatically. For details, see deactivate().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public deactivateButton() {
    this.alanBtnInstance.deactivate();
  }
}
app.component.html
<div>
  <a class="card" (click)="deactivateButton()">
    <span>Deactivate Alan button</span>
  </a>
</div>

isActive()

Use the isActive() method to check the Alan button state: active or not. For details, see isActive().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public isActive() {
    console.log("The button is active: " + this.alanBtnInstance.isActive());
  }
}
app.component.html
<div>
  <a class="card" (click)="isActive()">
    <span>Check button state</span>
  </a>
</div>

remove()

Use the remove() method to remove the Alan button from the parent element. For details, see remove().

app.component.ts
export class AppComponent {

  alanBtnInstance;

  constructor(){
    this.alanBtnInstance = alanBtn({
      key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
    });
  }

  public removeButton() {
    this.alanBtnInstance.remove();
  }
}
app.component.html
<div>
  <a class="card" (click)="removeButton()">
    <span>Remove Alan button</span>
  </a>
</div>

Using handlers

You can use the following Alan handlers in your Angular app:

onCommand handler

Use the onCommand handler to handle commands sent from the voice script. For details, see onCommand handler.

Client app
this.alanBtnInstance = alanBtn({
  key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
  onCommand: (commandData: any) => {
    if (commandData.command === 'go:back') {
      // Call client code that will react to the received command
    }
  },
});

onButtonState handler

Use the onButtonState handler to capture and handle the Alan button state changes. For details, see onButtonState handler.

Client app
this.alanBtnInstance = alanBtn({
  key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
  onButtonState: function (e) {
    console.info('onButtonState', e);
  },
});

onConnectionStatus handler

Use the onConnectionStatus handler to capture and handle the connection status for the voice project. For details, see onConnectionStatus handler.

Client app
this.alanBtnInstance = alanBtn({
  key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
  onConnectionStatus: function(status) {
    console.log("The status is " + status);
  },
});

onEvent handler

Use the onEvent handler to capture and handle events emitted by Alan: get user’s utterances, voice assistant responses and so on. For details, see onEvent handler.

Client app
this.alanBtnInstance = alanBtn({
  key: 'YOUR_KEY_FROM_ALAN_STUDIO_HERE',
  onEvent: function (e) {
    console.info('onEvent', e);
  },
});

What’s next?

../../../_images/git-purple.svg

Example apps

Find and explore examples of voice-enabled apps on the Alan AI GitHub repository.

View on GitHub