Collect and process information from Google Chat users

This guide describes how Google Chat apps can collect and process information from users by building form inputs in card-based interfaces.

In Google Chat, add-ons appear to users as Google Chat apps. To learn more, see the Extend Google Chat overview.

A dialog featuring a variety of different widgets.
Figure 1: A Chat app that opens a dialog to collect contact information.

Chat apps request information from users to perform actions in or outside of Chat, including in the following ways:

  • Configure settings. For example, to let users customize notification settings or configure and add the Chat app to one or more spaces.
  • Create or update information in other Google Workspace applications. For example, let users create a Google Calendar event.
  • Let users access and update resources in other apps or web services. For example, a Chat app can help users update the status of a support ticket directly from a Chat space.

Prerequisites

Node.js

A Google Workspace Add-on that works in Google Chat. To build one, complete the HTTP quickstart.

Apps Script

A Google Workspace Add-on that works in Google Chat. To build one, complete the Apps Script quickstart.

Build forms using cards

To collect information, Chat apps design forms and their inputs, and build them into cards. To display cards to users, Chat apps can use the following Chat interfaces:

  • Chat messages that contain one or more cards.
  • Dialogs, which are cards that open in a new window from messages and homepages.

Chat apps can build the cards using the following widgets:

  • Form input widgets that request information from users. Optionally, you can add validation to form input widgets, to ensure that users input and format information correctly. Chat apps can use the following form input widgets:

    • Text inputs (textInput) for free-form or suggested text.
    • Selection inputs (selectionInput) are selectable UI elements such as checkboxes, radio buttons, and drop-down menus. Selection input widgets can also populate items from static or dynamic data sources. For example, users can select from a list of Chat spaces that they're a member of.
  • A button widget so that users can submit values that they've input in the card. After a user clicks the button, the Chat app can then process the information that it receives.

In the following example, a card collects contact information using a text input, date time picker, and selection input:

For more examples of interactive widgets that you can use to collect information, see Design an interactive card or dialog in the Google Chat API documentation.

Receive data from interactive widgets

Whenever users click a button, its Chat apps action is triggered with information about the interaction. In the commonEventObject of the event payload, the formInputs object contains any values that the user inputs.

You can retrieve the values from the object commonEventObject.formInputs.WIDGET_NAME, where WIDGET_NAME is the name field that you specified for the widget. The values are returned as a specific data type for the widget.

The following shows a portion of an event object where a user inputted values for each widget:

{
  "commonEventObject": { "formInputs": {
    "contactName": { "stringInputs": {
      "value": ["Kai 0"]
    }},
    "contactBirthdate": { "dateInput": {
      "msSinceEpoch": 1000425600000
    }},
    "contactType": { "stringInputs": {
      "value": ["Personal"]
    }}
  }}
}

To receive the data, your Chat app handles the event object to get the values that users input into widgets. The following table shows how to get the value for a given form input widget. For each widget, the table shows the data type that the widget accepts, where the value is stored in the event object, and an example value.

Form input widget Type of input data Input value from the event object Example value
textInput stringInputs event.commonEventObject.formInputs.contactName.stringInputs.value[0] Kai O
selectionInput stringInputs To get the first or only value, event.commonEventObject.formInputs.contactType.stringInputs.value[0] Personal
dateTimePicker that only accepts dates. dateInput event.commonEventObject.formInputs.contactBirthdate.dateInput.msSinceEpoch. 1000425600000

Transfer data to another card

After a user submits information from a card, you might need to return additional cards to do any of the following:

  • Help users to complete longer forms by creating distinct sections.
  • Let users preview and confirm information from the initial card, so that they can review their answers before submitting.
  • Dynamically populate the remaining parts of the form. For example, to prompt users to create an appointment, a Chat app could display an initial card that requests the reason for the appointment, and then populates another card that provides available times based on the appointment type.

To transfer the data input from the initial card, you can build the button widget with actionParameters that contain the widget's name and the value the user inputs, as shown in the following example:

{
  "buttonList": { "buttons": [{
    "text": "Submit",
    "onClick": { "action": {
      "function": "submitForm",
      "parameters": [
        {
          "key": "WIDGET_NAME",
          "value": "USER_INPUT_VALUE"
        },
        // Can specify multiple parameters
      ]
    }}
  }]}
}

Where WIDGET_NAME is the name of the widget and the USER_INPUT_VALUE is what the user inputs. For example, for a text input that collects a person's name, the widget name is contactName and an example value is Kai O.

When a user clicks the button, your Chat app receives an event object from which you can receive data.

Respond to a form submission

After receiving the data from a card message or dialog, the Chat app responds by either acknowledging receipt or returning an error.

In the following example, a Chat app sends a text message to confirm that it has successfully received a form submitted from a card message.

Node.js

/**
 * Google Cloud Function that handles all Google Workspace Add On events for
 * the contact manager app.
 *
 * @param {Object} req Request sent from Google Chat space
 * @param {Object} res Response to send back
 */
exports.contactManager = function contactManager(req, res) {
  const chatEvent = req.body.chat;
  const chatMessage = chatEvent.messagePayload.message;

  // Handle MESSAGE events
  if(chatEvent.messagePayload) {
    return res.send(handleMessage(chatMessage, chatEvent.user));
  // Handle CARD_CLICKED events
  } else if(chatEvent.buttonClickedPayload) {
    switch(req.body.commonEventObject.parameters.actionName) {
        case "openDialog":
            return res.send(openDialog());
        case "openNextCard":
            return res.send(openNextCard(req.body));
        case "submitForm":
            return res.send(submitForm(req.body));
    }
  }
};

/**
 * Submits information from a dialog or card message.
 *
 * @param {Object} event the interactive event with form inputs.
 * @return {Object} a message response that posts a private message.
 */
function submitForm(event) {
  const chatUser = event.chat.user;
  const contactName = event.commonEventObject.parameters["contactName"];

  return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
    privateMessageViewer: chatUser,
    text: "✅ " + contactName + " has been added to your contacts."
  }}}}};
}

Apps Script

/**
 * Sends private text message that confirms submission.
 *
 * @param {Object} event the interactive event with form inputs.
 * @return {Object} a message response that posts a private message.
 */
function submitForm(event) {
  const chatUser = event.chat.user;
  const contactName = event.commonEventObject.parameters["contactName"];

  return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: {
    privateMessageViewer: chatUser,
    text: "✅ " + contactName + " has been added to your contacts."
  }}}}};
}

To process and close a dialog, you return an RenderActions object that specifies whether you want to send a confirmation message, update the original message or card, or just close the dialog. For steps, see Close a dialog.

Troubleshoot

When a Google Chat app or card returns an error, the Chat interface surfaces a message saying "Something went wrong." or "Unable to process your request." Sometimes the Chat UI doesn't display any error message, but the Chat app or card produces an unexpected result; for example, a card message might not appear.

Although an error message might not display in the Chat UI, descriptive error messages and log data are available to help you fix errors when error logging for Chat apps is turned on. For help viewing, debugging, and fixing errors, see Troubleshoot and fix Google Chat errors.