Constructor

AssistantApp

new AssistantApp(options, requestData)

Constructor for AssistantApp object. Should not be instantiated; rather instantiate one of the subclasses ActionsSdkApp or DialogflowApp.

Parameters

Name Description

options

Object

JSON configuration.

requestData

function()

Function that returns the request data object to be processed.

Enumerations

BuiltInArgNames

read-only

string

List of built-in argument names.

Properties

Name Description

PERMISSION_GRANTED

Permission granted argument.

OPTION

Option selected argument.

TRANSACTION_REQ_CHECK_RESULT

Transaction requirements check result argument.

DELIVERY_ADDRESS_VALUE

Delivery address value argument.

TRANSACTION_DECISION_VALUE

Transactions decision argument.

CONFIRMATION

Confirmation argument.

DATETIME

DateTime argument.

SIGN_IN

Sign in status argument.

REPROMPT_COUNT

Reprompt count for consecutive NO_INPUT intents.

IS_FINAL_REPROMPT

Flag representing finality of NO_INPUT intent.

NEW_SURFACE

New surface value argument.

ConversationStages

read-only

number

List of possible conversation stages, as defined in the Conversation object.

Properties

Name Description

UNSPECIFIED

Unspecified conversation state.

NEW

A new conversation.

ACTIVE

An active (ongoing) conversation.

InputTypes

read-only

number

List of possible user input types.

Properties

Name Description

UNSPECIFIED

Unspecified.

TOUCH

Input given by touch.

VOICE

Input given by voice (spoken).

KEYBOARD

Input given by keyboard (typed).

SignInStatus

read-only

string

List of possible sign in result status values.

Properties

Name Description

UNSPECIFIED

OK

CANCELLED

ERROR

StandardIntents

read-only

string

List of standard intents that the app provides.

Properties

Name Description

MAIN

App fires MAIN intent for queries like [talk to $app].

TEXT

App fires TEXT intent when action issues ask intent.

PERMISSION

App fires PERMISSION intent when action invokes askForPermission.

OPTION

App fires OPTION intent when user chooses from options provided.

TRANSACTION_REQUIREMENTS_CHECK

App fires TRANSACTION_REQUIREMENTS_CHECK intent when action sets up transaction.

DELIVERY_ADDRESS

App fires DELIVERY_ADDRESS intent when action asks for delivery address.

TRANSACTION_DECISION

App fires TRANSACTION_DECISION intent when action asks for transaction decision.

CONFIRMATION

App fires CONFIRMATION intent when requesting affirmation from user.

DATETIME

App fires DATETIME intent when requesting date/time from user.

SIGN_IN

App fires SIGN_IN intent when requesting sign-in from user.

NO_INPUT

App fires NO_INPUT intent when user doesn't provide input.

CANCEL

App fires CANCEL intent when user exits app mid-dialog.

NEW_SURFACE

App fires NEW_SURFACE intent when requesting handoff to a new surface from user.

SupportedPermissions

read-only

string

List of supported permissions the app supports.

Properties

Name Description

NAME

The user's name as defined in the UserProfile object

DEVICE_PRECISE_LOCATION

The location of the user's current device, as defined in the Location object.

DEVICE_COARSE_LOCATION

City and zipcode corresponding to the location of the user's current device, as defined in the Location object.

SurfaceCapabilities

read-only

string

List of surface capabilities supported by the app.

Properties

Name Description

AUDIO_OUTPUT

The ability to output audio.

SCREEN_OUTPUT

The ability to output on a screen

Methods

askForConfirmation

askForConfirmation(prompt, dialogState)

Asks user for a confirmation.

Parameters

Name Description

prompt

Optional

string

The confirmation prompt presented to the user to query for an affirmative or negative response. If undefined or null, Google will use a generic yes/no prompt.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Example

const app = new DialogflowApp({ request, response });
const WELCOME_INTENT = 'input.welcome';
const CONFIRMATION = 'confirmation';

function welcomeIntent (app) {
  app.askForConfirmation('Are you sure you want to do that?');
}

function confirmation (app) {
  if (app.getUserConfirmation()) {
    app.tell('Great! I\'m glad you want to do it!');
  } else {
    app.tell('That\'s okay. Let\'s not do it now.');
  }
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(CONFIRMATION, confirmation);
app.handleRequest(actionMap);

askForDateTime

askForDateTime(initialPrompt, datePrompt, timePrompt, dialogState)

Asks user for a timezone-agnostic date and time.

Parameters

Name Description

initialPrompt

Optional

string

The initial prompt used to ask for a date and time. If undefined or null, Google will use a generic prompt.

datePrompt

Optional

string

The prompt used to specifically ask for the date if not provided by user. If undefined or null, Google will use a generic prompt.

timePrompt

Optional

string

The prompt used to specifically ask for the time if not provided by user. If undefined or null, Google will use a generic prompt.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Example

const app = new DialogflowApp({ request, response });
const WELCOME_INTENT = 'input.welcome';
const DATETIME = 'datetime';

function welcomeIntent (app) {
  app.askForDateTime('When do you want to come in?',
    'Which date works best for you?',
    'What time of day works best for you?');
}

function datetime (app) {
  app.tell({speech: 'Great see you at your appointment!',
    displayText: 'Great, we will see you on '
    + app.getDateTime().date.month
    + '/' + app.getDateTime().date.day
    + ' at ' + app.getDateTime().time.hours
    + (app.getDateTime().time.minutes || '')});
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(DATETIME, datetime);
app.handleRequest(actionMap);

askForDeliveryAddress

askForDeliveryAddress(reason, dialogState) returns Object

Asks user for delivery address.

Parameters

Name Description

reason

string

Reason given to user for asking delivery address.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant.

Returns

Object HTTP response.

Example

// For DialogflowApp:
const app = new DialogflowApp({request, response});
const WELCOME_INTENT = 'input.welcome';
const DELIVERY_INTENT = 'delivery.address';

function welcomeIntent (app) {
  app.askForDeliveryAddress('To make sure I can deliver to you');
}

function addressIntent (app) {
  const postalCode = app.getDeliveryAddress().postalAddress.postalCode;
  if (isInDeliveryZone(postalCode)) {
    app.tell('Great looks like you\'re in our delivery area!');
  } else {
    app.tell('I\'m sorry it looks like we can\'t deliver to you.');
  }
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(DELIVERY_INTENT, addressIntent);
app.handleRequest(actionMap);

// For ActionsSdkApp:
const app = new ActionsSdkApp({request, response});
const WELCOME_INTENT = app.StandardIntents.MAIN;
const DELIVERY_INTENT = app.StandardIntents.DELIVERY_ADDRESS;

function welcomeIntent (app) {
  app.askForDeliveryAddress('To make sure I can deliver to you');
}

function addressIntent (app) {
  const postalCode = app.getDeliveryAddress().postalAddress.postalCode;
  if (isInDeliveryZone(postalCode)) {
    app.tell('Great looks like you\'re in our delivery area!');
  } else {
    app.tell('I\'m sorry it looks like we can\'t deliver to you.');
  }
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(DELIVERY_INTENT, addressIntent);
app.handleRequest(actionMap);

askForNewSurface

askForNewSurface(context, notificationTitle, capabilities, dialogState)

Requests the user to switch to another surface during the conversation.

Parameters

Name Description

context

string

Context why new surface is requested; it's the TTS prompt prefix (action phrase) we ask the user.

notificationTitle

string

Title of the notification appearing on new surface device.

capabilities

Array of string

The list of capabilities required in the surface.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Example

const app = new DialogflowApp({ request, response });
const WELCOME_INTENT = 'input.welcome';
const SHOW_IMAGE = 'show.image';

function welcomeIntent (app) {
  if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
    showPicture(app);
  } else if (app.hasAvailableSurfaceCapabilities(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
    app.askForNewSurface('To show you an image',
      'Check out this image',
      [app.SurfaceCapabilities.SCREEN_OUTPUT]
    );
  } else {
    app.tell('This part of the app only works on screen devices. Sorry about that');
  }
}

function showImage (app) {
  if (!app.isNewSurface()) {
    app.tell('Ok, I understand. You don't want to see pictures. Bye');
  } else {
    showPicture(app, pictureType);
  }
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(SHOW_IMAGE, showImage);
app.handleRequest(actionMap);

askForPermission

askForPermission(context, permission, dialogState)

Asks the Assistant to guide the user to grant a permission. For example, if you want your app to get access to the user's name, you would invoke the askForPermission method with a context containing the reason for the request, and the AssistantApp.SupportedPermissions.NAME permission. With this, the Assistant will ask the user, in your agent's voice, the following: '[Context with reason for the request], I'll just need to get your name from Google, is that OK?'.

Once the user accepts or denies the request, the Assistant will fire another intent: assistant.intent.action.PERMISSION with a boolean argument: AssistantApp.BuiltInArgNames.PERMISSION_GRANTED and, if granted, the information that you requested.

Read more:

Parameters

Name Description

context

string

Context why permission is asked; it's the TTS prompt prefix (action phrase) we ask the user.

permission

string

One of the permissions Assistant supports, each of which comes from AssistantApp.SupportedPermissions.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant.

Returns

A response is sent to the Assistant to ask for the user's permission; for any invalid input, we return null.

Example

const app = new DialogflowApp({request: req, response: res});
const REQUEST_PERMISSION_ACTION = 'request_permission';
const GET_RIDE_ACTION = 'get_ride';

function requestPermission (app) {
  const permission = app.SupportedPermissions.NAME;
  app.askForPermission('To pick you up', permission);
}

function sendRide (app) {
  if (app.isPermissionGranted()) {
    const displayName = app.getUserName().displayName;
    app.tell('I will tell your driver to pick up ' + displayName);
  } else {
    // Response shows that user did not grant permission
    app.tell('Sorry, I could not figure out who to pick up.');
  }
}
const actionMap = new Map();
actionMap.set(REQUEST_PERMISSION_ACTION, requestPermission);
actionMap.set(GET_RIDE_ACTION, sendRide);
app.handleRequest(actionMap);

askForPermissions

askForPermissions(context, permissions, dialogState)

Equivalent to askForPermission, but allows you to prompt the user for more than one permission at once.

Notes:

  • The order in which you specify the permission prompts does not matter - it is controlled by the Assistant to provide a consistent user experience.
  • The user will be able to either accept all permissions at once, or none. If you wish to allow them to selectively accept one or other, make several dialog turns asking for each permission independently with askForPermission.
  • Asking for DEVICE_COARSE_LOCATION and DEVICE_PRECISE_LOCATION at once is equivalent to just asking for DEVICE_PRECISE_LOCATION

Parameters

Name Description

context

string

Context why the permission is being asked; it's the TTS prompt prefix (action phrase) we ask the user.

permissions

Array of string

Array of permissions App supports, each of which comes from AssistantApp.SupportedPermissions.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Returns

A response is sent to Assistant to ask for the user's permission; for any invalid input, we return null.

Example

const app = new DialogflowApp({request: req, response: res});
const REQUEST_PERMISSION_ACTION = 'request_permission';
const GET_RIDE_ACTION = 'get_ride';

function requestPermission (app) {
  const permission = [
    app.SupportedPermissions.NAME,
    app.SupportedPermissions.DEVICE_PRECISE_LOCATION
  ];
  app.askForPermissions('To pick you up', permissions);
}

function sendRide (app) {
  if (app.isPermissionGranted()) {
    const displayName = app.getUserName().displayName;
    const address = app.getDeviceLocation().address;
    app.tell('I will tell your driver to pick up ' + displayName +
        ' at ' + address);
  } else {
    // Response shows that user did not grant permission
    app.tell('Sorry, I could not figure out where to pick you up.');
  }
}
const actionMap = new Map();
actionMap.set(REQUEST_PERMISSION_ACTION, requestPermission);
actionMap.set(GET_RIDE_ACTION, sendRide);
app.handleRequest(actionMap);

askForSignIn

askForSignIn(dialogState)

Hands the user off to a web sign in flow. App sign in and OAuth credentials are set in the Actions Console. Retrieve the access token in subsequent intents using app.getUser().accessToken.

Note: Currently this API requires enabling the app for Transactions APIs. To do this, fill out the App Info section of the Actions Console project and check the box indicating the use of Transactions under "Privacy and consent".

Parameter

Name Description

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Example

const app = new DialogflowApp({ request, response });
const WELCOME_INTENT = 'input.welcome';
const SIGN_IN = 'sign.in';

function welcomeIntent (app) {
  app.askForSignIn();
}

function signIn (app) {
  if (app.getSignInStatus() === app.SignInstatus.OK) {
    let accessToken = app.getUser().accessToken;
    app.ask('Great, thanks for signing in!');
  } else {
    app.ask('I won\'t be able to save your data, but let\'s continue!');
  }
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(SIGN_IN, signIn);
app.handleRequest(actionMap);

askForTransactionDecision

askForTransactionDecision(order, transactionConfig, dialogState)

Asks user to confirm transaction information.

Parameters

Name Description

order

Object

Order built with buildOrder().

transactionConfig

(ActionPaymentTransactionConfig or GooglePaymentTransactionConfig)

Configuration for the transaction. Includes payment options and order options.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Example

const app = new DialogflowApp({request: request, response: response});
const WELCOME_INTENT = 'input.welcome';
const TXN_COMPLETE = 'txn.complete';

let transactionConfig = {
    deliveryAddressRequired: false,
    type: app.Transactions.PaymentType.BANK,
    displayName: 'Checking-1234'
};

let order = app.buildOrder();
// fill order cart

function welcomeIntent (app) {
  app.askForTransaction(order, transactionConfig);
}

function txnComplete (app) {
  // respond with order update
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(TXN_COMPLETE, txnComplete);
app.handleRequest(actionMap);

askForTransactionRequirements

askForTransactionRequirements(transactionConfig, dialogState) returns Object

Checks whether user is in transactable state.

Parameters

Name Description

transactionConfig

(ActionPaymentTransactionConfig or optional GooglePaymentTransactionConfig)

Configuration for the transaction. Includes payment options and order options. Optional if order has no payment or delivery.

dialogState

Optional

Object

JSON object the app uses to hold dialog state that will be circulated back by Assistant. Used in ActionsSdkAssistant.

Returns

Object HTTP response.

Example

const app = new DialogflowApp({request: request, response: response});
const WELCOME_INTENT = 'input.welcome';
const TXN_REQ_COMPLETE = 'txn.req.complete';

let transactionConfig = {
    deliveryAddressRequired: false,
    type: app.Transactions.PaymentType.BANK,
    displayName: 'Checking-1234'
};
function welcomeIntent (app) {
  app.askForTransactionRequirements(transactionConfig);
}

function txnReqCheck (app) {
  if (app.getTransactionRequirementsResult() === app.Transactions.ResultType.OK) {
    // continue cart building flow
  } else {
    // don't continue cart building
  }
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(TXN_REQ_COMPLETE, txnReqCheck);
app.handleRequest(actionMap);

buildBasicCard

buildBasicCard(bodyText) returns BasicCard

Constructs BasicCard with chainable property setters.

Parameter

Name Description

bodyText

Optional

string

Body text of the card. Can be set using setTitle instead.

Returns

BasicCard Constructed BasicCard.

buildCarousel

buildCarousel() returns Carousel

Constructs Carousel with chainable property setters.

Returns

Carousel Constructed Carousel.

buildCart

buildCart(cartId) returns Cart

Constructs Cart with chainable property setters.

Parameter

Name Description

cartId

Optional

string

Unique identifier for the cart.

Returns

Cart Constructed Cart.

buildLineItem

buildLineItem(name, id) returns LineItem

Constructs LineItem with chainable property setters.

Parameters

Name Description

name

string

Name of the line item.

id

string

Unique identifier for the item.

Returns

LineItem Constructed LineItem.

buildList

buildList(title) returns List

Constructs List with chainable property setters.

Parameter

Name Description

title

Optional

string

A title to set for a new List.

Returns

List Constructed List.

buildOptionItem

buildOptionItem(key, synonyms) returns OptionItem

Constructs OptionItem with chainable property setters.

Parameters

Name Description

key

Optional

string

A unique key to identify this option. This key will be returned as an argument in the resulting actions.intent.OPTION intent.

synonyms

(string or optional Array of string)

A list of synonyms which the user may use to identify this option instead of the option key.

Returns

OptionItem Constructed OptionItem.

buildOrder

buildOrder(orderId) returns Order

Constructs Order with chainable property setters.

Parameter

Name Description

orderId

string

Unique identifier for the order.

Returns

Order Constructed Order.

buildOrderUpdate

buildOrderUpdate(orderId, isGoogleOrderId) returns OrderUpdate

Constructs OrderUpdate with chainable property setters.

Parameters

Name Description

orderId

string

Unique identifier of the order.

isGoogleOrderId

boolean

True if the order ID is provided by Google. False if the order ID is app provided.

Returns

OrderUpdate Constructed OrderUpdate.

buildRichResponse

buildRichResponse(richResponse) returns RichResponse

Constructs RichResponse with chainable property setters.

Parameter

Name Description

richResponse

Optional

RichResponse

RichResponse to clone.

Returns

RichResponse Constructed RichResponse.

findArgument_

findArgument_(...targets) returns any type

Find argument with requirements

Parameter

Name Description

targets

Array of string

Argument to find

Value may be repeated.

Returns

any type The argument

getArgumentCommon

getArgumentCommon(argName) returns Object

Get the argument value by name from the current intent. If the argument is included in originalRequest, and is not a text argument, the entire argument object is returned.

Note: If incoming request is using an API version under 2 (e.g. 'v1'), the argument object will be in Proto2 format (snake_case, etc).

Parameter

Name Description

argName

string

Name of the argument.

Returns

Object Argument value matching argName or null if no matching argument.

Example

const app = new DialogflowApp({request: request, response: response});
const WELCOME_INTENT = 'input.welcome';
const NUMBER_INTENT = 'input.number';

function welcomeIntent (app) {
  app.ask('Welcome to action snippets! Say a number.');
}

function numberIntent (app) {
  const number = app.getArgument(NUMBER_ARGUMENT);
  app.tell('You said ' + number);
}

const actionMap = new Map();
actionMap.set(WELCOME_INTENT, welcomeIntent);
actionMap.set(NUMBER_INTENT, numberIntent);
app.handleRequest(actionMap);

getAvailableSurfaces

getAvailableSurfaces() returns Array of Surface

Returns the set of other available surfaces for the user.

Returns

Array of Surface Empty if no available surfaces.

getDateTime

getDateTime() returns DateTime

Gets user provided date and time. Use after askForDateTime.

Returns

DateTime Date and time given by the user. Null if no user date and time given.

getDeliveryAddress

getDeliveryAddress() returns DeliveryAddress

Gets order delivery address. Only use after calling askForDeliveryAddress.

Returns

DeliveryAddress Delivery address information. Null if user denies permission, or no address given.

getDeviceLocation

getDeviceLocation() returns DeviceLocation

If granted permission to device's location in previous intent, returns device's location (see askForPermissions). If device info is unavailable, returns null.

Returns

DeviceLocation Null if location permission is not granted.

Example

const app = new DialogflowApp({request: req, response: res});
// or
const app = new ActionsSdkApp({request: req, response: res});
app.askForPermission("To get you a ride",
  app.SupportedPermissions.DEVICE_PRECISE_LOCATION);
// ...
// In response handler for permissions fallback intent:
if (app.isPermissionGranted()) {
  sendCarTo(app.getDeviceLocation().coordinates);
}

getInputType

getInputType() returns number

Gets type of input used for this request.

Returns

number One of AssistantApp.InputTypes. Null if no input type given.

getRepromptCount

getRepromptCount() returns number

Returns the number of subsequent reprompts related to silent input from the user. This should be used along with the NO_INPUT intent to reprompt the user for input in cases where the Google Assistant could not pick up any speech.

Returns

number The current reprompt count. Null if no reprompt count available (e.g. not in the NO_INPUT intent).

Example

const app = new ActionsSdkApp({request, response});

function welcome (app) {
  app.ask('Welcome to your app!');
}

function noInput (app) {
  if (app.getRepromptCount() === 0) {
    app.ask(`What was that?`);
  } else if (app.getRepromptCount() === 1) {
    app.ask(`Sorry I didn't catch that. Could you repeat yourself?`);
  } else if (app.isFinalReprompt()) {
    app.tell(`Okay let's try this again later.`);
  }
}

const actionMap = new Map();
actionMap.set(app.StandardIntents.MAIN, welcome);
actionMap.set(app.StandardIntents.NO_INPUT, noInput);
app.handleRequest(actionMap);

getSignInStatus

getSignInStatus() returns string

Gets status of user sign in request.

Returns

string Result of user sign in request. One of DialogflowApp.SignInStatus or ActionsSdkApp.SignInStatus Null if no sign in status.

getSurfaceCapabilities

getSurfaceCapabilities() returns Array of string

Gets surface capabilities of user device.

Returns

Array of string Supported surface capabilities, as defined in AssistantApp.SurfaceCapabilities.

getTransactionDecision

getTransactionDecision() returns TransactionDecision

Gets transaction decision information. Only use after calling askForTransactionDecision.

Returns

TransactionDecision Transaction decision data. Returns object with userDecision only if user declines. userDecision will be one of Transactions.ConfirmationDecision. Null if no decision given.

getTransactionRequirementsResult

getTransactionRequirementsResult() returns string

Gets transactability of user. Only use after calling askForTransactionRequirements. Null if no result given.

Returns

string One of Transactions.ResultType.

getUser

getUser() returns User

Gets the User object. The user object contains information about the user, including a string identifier and personal information (requires requesting permissions, see askForPermissions).

Returns

User Null if no value.

Example

const app = new DialogflowApp({request: request, response: response});
// or
const app = new ActionsSdkApp({request: request, response: response});
const userId = app.getUser().userId;

getUserConfirmation

getUserConfirmation()

Gets confirmation decision. Use after askForConfirmation.

False if user replied with negative response. Null if no user
confirmation decision given.

getUserLocale

getUserLocale() returns string

Gets the user locale. Returned string represents the regional language information of the user set in their Assistant settings. For example, 'en-US' represents US English.

Returns

string User's locale, e.g. 'en-US'. Null if no locale given.

Example

const app = new DialogflowApp({request, response});
const locale = app.getUserLocale();

getUserName

getUserName() returns UserName

If granted permission to user's name in previous intent, returns user's display name, family name, and given name. If name info is unavailable, returns null.

Returns

UserName Null if name permission is not granted.

Example

const app = new DialogflowApp({request: req, response: res});
const REQUEST_PERMISSION_ACTION = 'request_permission';
const SAY_NAME_ACTION = 'get_name';

function requestPermission (app) {
  const permission = app.SupportedPermissions.NAME;
  app.askForPermission('To know who you are', permission);
}

function sayName (app) {
  if (app.isPermissionGranted()) {
    app.tell('Your name is ' + app.getUserName().displayName));
  } else {
    // Response shows that user did not grant permission
    app.tell('Sorry, I could not get your name.');
  }
}
const actionMap = new Map();
actionMap.set(REQUEST_PERMISSION_ACTION, requestPermission);
actionMap.set(SAY_NAME_ACTION, sayName);
app.handleRequest(actionMap);

handleRequest

handleRequest(handler)

Handles the incoming Assistant request using a handler or Map of handlers. Each handler can be a function callback or Promise.

Parameter

Name Description

handler

(function() or Map)

The handler (or Map of handlers) for the request.

Example

// Actions SDK
const app = new ActionsSdkApp({request: request, response: response});

function mainIntent (app) {
  const inputPrompt = app.buildInputPrompt(true, '<speak>Hi! <break time="1"/> ' +
        'I can read out an ordinal like ' +
        '<say-as interpret-as="ordinal">123</say-as>. Say a number.</speak>',
        ['I didn\'t hear a number', 'If you\'re still there, what\'s the number?', 'What is the number?']);
  app.ask(inputPrompt);
}

function rawInput (app) {
  if (app.getRawInput() === 'bye') {
    app.tell('Goodbye!');
  } else {
    const inputPrompt = app.buildInputPrompt(true, '<speak>You said, <say-as interpret-as="ordinal">' +
      app.getRawInput() + '</say-as></speak>',
        ['I didn\'t hear a number', 'If you\'re still there, what\'s the number?', 'What is the number?']);
    app.ask(inputPrompt);
  }
}

const actionMap = new Map();
actionMap.set(app.StandardIntents.MAIN, mainIntent);
actionMap.set(app.StandardIntents.TEXT, rawInput);

app.handleRequest(actionMap);

// Dialogflow
const app = new DialogflowApp({request: req, response: res});
const NAME_ACTION = 'make_name';
const COLOR_ARGUMENT = 'color';
const NUMBER_ARGUMENT = 'number';

function makeName (app) {
  const number = app.getArgument(NUMBER_ARGUMENT);
  const color = app.getArgument(COLOR_ARGUMENT);
  app.tell('Alright, your silly name is ' +
    color + ' ' + number +
    '! I hope you like it. See you next time.');
}

const actionMap = new Map();
actionMap.set(NAME_ACTION, makeName);
app.handleRequest(actionMap);

hasAvailableSurfaceCapabilities

hasAvailableSurfaceCapabilities(capabilities) returns boolean

Returns true if user has an available surface which includes all given capabilities. Available surfaces capabilities may exist on surfaces other than that used for an ongoing conversation.

Parameter

Name Description

capabilities

(string or Array of string)

Must be one of SurfaceCapabilities.

Returns

boolean True if user has a capability available on some surface.

hasSurfaceCapability

hasSurfaceCapability(capability) returns boolean

Returns true if user device has a given surface capability.

Parameter

Name Description

capability

string

Must be one of SurfaceCapabilities.

Returns

boolean True if user device has the given capability.

Example

const app = new DialogflowApp({request: req, response: res});
const DESCRIBE_SOMETHING = 'DESCRIBE_SOMETHING';

function describe (app) {
  if (app.hasSurfaceCapability(app.SurfaceCapabilities.SCREEN_OUTPUT)) {
    app.tell(richResponseWithBasicCard);
  } else {
    app.tell('Let me tell you about ...');
  }
}
const actionMap = new Map();
actionMap.set(DESCRIBE_SOMETHING, describe);
app.handleRequest(actionMap);

isFinalReprompt

isFinalReprompt() returns boolean

Returns true if it is the final reprompt related to silent input from the user. This should be used along with the NO_INPUT intent to give the final response to the user after multiple silences and should be an app.tell which ends the conversation.

Returns

boolean True if in a NO_INPUT intent and this is the final turn of dialog.

Example

const app = new ActionsSdkApp({request, response});

function welcome (app) {
  app.ask('Welcome to your app!');
}

function noInput (app) {
  if (app.getRepromptCount() === 0) {
    app.ask(`What was that?`);
  } else if (app.getRepromptCount() === 1) {
    app.ask(`Sorry I didn't catch that. Could you repeat yourself?`);
  } else if (app.isFinalReprompt()) {
    app.tell(`Okay let's try this again later.`);
  }
}

const actionMap = new Map();
actionMap.set(app.StandardIntents.MAIN, welcome);
actionMap.set(app.StandardIntents.NO_INPUT, noInput);
app.handleRequest(actionMap);

isInSandbox

isInSandbox() returns boolean

Returns true if the app is being tested in sandbox mode. Enable sandbox mode in the (Actions console)[console.actions.google.com] to test transactions.

Returns

boolean True if app is being used in Sandbox mode.

isNewSurface

isNewSurface() returns boolean

Returns the result of the AskForNewSurface helper.

Returns

boolean True if user has triggered conversation on a new device following the NEW_SURFACE intent.

isPermissionGranted

isPermissionGranted() returns boolean

Returns true if the request follows a previous request asking for permission from the user and the user granted the permission(s). Otherwise, false. Use with askForPermissions.

Returns

boolean true if permissions granted.

Example

const app = new ActionsSdkApp({request: request, response: response});
// or
const app = new DialogflowApp({request: request, response: response});
app.askForPermissions("To get you a ride", [
  app.SupportedPermissions.NAME,
  app.SupportedPermissions.DEVICE_PRECISE_LOCATION
]);
// ...
// In response handler for subsequent intent:
if (app.isPermissionGranted()) {
 // Use the requested permission(s) to get the user a ride
}