Returning text back to the user can be done a few different ways.

Either via the return method or using the request parameter if there will be a delay with the response.

Simple return example

module.exports = class RepeatIntent extends Intent {

  setup() {
    this.train([
      new RegExp(/^repeat/,'i'),
      new RegExp(/^say/,'i')
    ], {
      collection: 'strict'
    });
  }

  response(request) {
    let result = request.input.text;

    result = result.replace(/^repeat/i,'').trim();
    result = result.replace(/^say/i,'').trim();
    result = result.replace(/^after me/i,'').trim();

    return result;
  }

}
Repeat after me I am great!
I am great!
Say hey
hey

Returning an array

The return method can also return an array which will output to the user as multiple lines of chat.

module.exports = class HelloIntent extends Intent {

  setup() {
    this.train(['hello']);
  }

  response() {
    return [
      'Hey!',
      'Very nice to meet you',
      'Let me know if you need any help'
    ];
  }

}
Hello
Hey!
Very nice to meet you
Let me know if you need any help

Returning a random response

Your intent can use the _.sample() method from the Underscore.js library to return random responses.

const _ = require('underscore');

module.exports = class DoingIntent extends Intent {

  setup() {
    this.train([
      'doing',
      'up to',
      'going on',
      'sup'
    ]);
  }

  response() {
    var choices = [
      "Helping as many people as I can and entertaining them with cat facts!",
      "Calculations, currency and checking the time in different countries",
      "Browsing tech sites and trying to figure out what I\'ll be doing in 10 years time"
    ];
    return _.sample(choices);
  }

}
What are you doing?
Calculations, currency and checking the time in different countries

Delaying a response

If you need to fetch data from a remote source and you could have a delay a Javascript promise can be used.

You can also handle finishing the request manually using request.end() which is used in the CountSixSeconds example below.

module.exports = class FiveSecondsIntent extends Intent {

  setup() {
    this.train(['five seconds']);
  }

  response() {
    return new Promise(function(resolve, reject){
      setTimeout(() => {
        resolve('5 seconds are up');
      }, 5 * 1000);
    });
  }

}
Five seconds
Five seconds later
5 seconds are up

If you want to asynchronous read a file from the skill Data directory manually.

module.exports = class CatfactsIntent extends Intent {

  setup() {
    this.train([
      'catfact',
      'cat facts',
      'cat fact'
    ]);
  }

  response(request) {
    var filename = request.app.Path.get('skills.app')+'/CatFacts/Data/catfacts.txt';

    return new Promise(function(resolve, reject) {
      var fs = require('fs');
      fs.readFile(filename, 'utf8', function(err, data) {
        var lines = data.split(/\r?\n/);
        resolve(_.sample(lines));
      });
    });
  }

}
Give me a fact about cats
Most cats adore sardines

Multiple delayed responses

To send multiple responses from one intent the request must return false and the request must be ended manually using request.end().

In this example the intent will count to six while keeping the individual session alive until it counts to 6 and then the request is manually ended.

If the request is not ended the queue timeout will be called.

module.exports = class CountSixSecondsIntent extends Intent {

  setup() {
    this.train(['count to six']);
  }

  response(request) {
    setTimeout(() => { request.send('1'); }, 1 * 1000);
    setTimeout(() => { request.send('2'); }, 2 * 1000);
    setTimeout(() => { request.send('3'); }, 3 * 1000);
    setTimeout(() => { request.send('4'); }, 4 * 1000);
    setTimeout(() => { request.send('5'); }, 5 * 1000);
    setTimeout(() => {
      request.send('6');
      request.end();
    }, 6 * 1000);

    return false;
  }

}
Count to six
1 second later
1
1 second later
2
1 second later
3
1 second later
4
1 second later
5
1 second later
6
Session ended

Manual request

A manual request within GI can be added within skills, intents and anywhere with access to the main app. It requires at least two parameters, client_id and session_id.

Manual requests are useful for timed responses such as reminders and using with the event system to send users welcome messages when they use the assistant for the first time.

Key Required Default Description
client_id Yes - Client id the user is connecting through
session_id Yes - Session id for the user
type No message Type of request
skip_queue No false If the queue should be respected, useful for reminders
fast No false Simulate typing delays in response, set to false for reminders
intent No null Intent to call. If set type is set to intent
action No null Intent action to call
text No null Required if no intent has been specified. Users text, e.g. “what time is it in London?”

Request example

It’s possible to create a new request for the user to call multiple intents. Defining the intent will force the user to the defined intent. Using the fast key will stop any simulated typing delays.

module.exports = class MultipleIntent extends Intent {

  setup() {
    this.train(['multiple'], {
      collection: 'strict'
    });
  }

  response(response) {
    response.send('Let\'s ping!');

    response.app.request({
      client_id: response.client.client_id,
      session_id: response.session.session_id,
      intent: 'App.Example.Intent.Ping',
      fast: true
    });

    return true;
  }

}
Multiple
Let's ping!
Pong