My first App
  • Updated on 09 Jul 2020
  • 2 minutes to read
  • Contributors
  • Print
  • Share
  • Dark
    Light

My first App

  • Print
  • Share
  • Dark
    Light

'Hello World!' App

A FrontM.js App is a JavaScript function that receives a State as its input parameter and returns an array of Intents:

const App = state => {

  let main = new Intent('main');
  main.onResolution = async () => {
    state.addStringResponse('Hello World!');
  };

  return [main];
};

This App is a very basic example and returns only one Intent called "main".

main Intent

An object of the Intent Class, called main, is created during runtime:

let main = new Intent('main');

main is not a mandatory Intent but, when present, will always run when the user starts a conversation.

Allowing interaction

The 'Hello World' App does nothing i.e. it won’t respond to a user question. In order to do that, we could assoctate an NLP agent as follows.

const App = state => {

  state.nlp.addNlp('FMApp'); 
  let main = new Intent('main');
  main.onResolution = async () => {
      state.runFunctionOnceAnHour(() => {
      state.addStringResponse('Hello World! How are you?');
    });
  };

  return [main];
};

The NLP agent "FMApp" could implement a questions and answers, chitchat, etc, making the bot more interactive.

The runFunctionOnceAnHour method

Since the main Intent object is always executed when a user starts a conversation, it doesn’t need to say "Hello" more than once if the user leaves the conversation and then re-enters it within the hour. This is why we add the runFunctionOnceAnHour method.

Natural Language Processing (NLP)

Developers can associate a NLP agent to their apps with the following instructions passing the agent ID:

state.nlp.addNlp('FMApp'); 

The current FrontM architecture uses Google Dialogflow or AWS Lex as NLP engines; we will, however, soon be using an internal solution.

Smart suggestions

Next, we can offer the user a couple of suggestions on what to send to the App:

const App = state => {

  state.nlp.addNlp('FMApp'); 
  let main = new Intent('main');
  main.onResolution = async () => {
    state.runFunctionOnceAnHour(() => {
      state.addStringResponse('Hello World! How are you?');
    });
    state.addEnglishSmartSuggestions(['Hello', 'How can you help me?']);
  };

  return [main];
};

We have added the following line to the script:

state.addEnglishSmartSuggestions(['Hello', 'How can you help me?']);

So instead of the user typing a response, the App will display two suggestions they can select from. It will then act exactly as if the user has typed them.

Here is another way of adding smart suggestions:

const App = state => {
 state.nlp.addNlp('FMApp'); 
 let main = new Intent('main');
 main.setEnglishSmartSuggestions([['Hello', 'How can you help me?']);
  main.onResolution = async () => {
    state.runFunctionOnceAnHour(() => {
      state.addStringResponse('Hello World! How are you?');
    });
  };

  return [main];
};

The difference between the two methods is that, in the second one, the suggestions are displayed all the time and not just after the code has executed.

Was this article helpful?