Skip to content

Building a new Action

Javi Jiménez edited this page Jun 26, 2016 · 1 revision

Build your own actions is quite easy, like intents is just create a stateless function. Actions functions only receive one parameter:

  • state: the actual object state.

successful functions have two ways for communicate the action:

  • return method for sync functions
  • resolve Promise method for async functions

So the easiest and basic example could be:

export default (state) => {
  state.action = { value: 'Hello world!' };
  return (state);
}

As you can see we just create the attribute action and return the state to Ava. But life sometimes is more difficult, so now we will create an async Action which will request something to a external data source:

import { entities } from 'ava-ia/lib/helpers'

export default (state) => {

  return new Promise( async (resolve, reject) => {
    response = await externalDataSource( {tokens: state.tokens} );

    state.action = {
      engine: 'mock',
      type: entities.knowledge,
      value: response.value
    };

    resolve(state);
  });
}

In this example we use resolve method 'cause we are inside a Promise, as you see still being easy create any kind of actions.

Feel free to offer new features, improvements or anything you can think of. This project makes sense with your participation and experience using Ava.

Clone this wiki locally