Collect and process information from Google Chat users (original) (raw)

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

A dialog featuring a variety of different widgets.

Figure 1: Asample 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:

Prerequisites

Node.js

A Google Chat app that receives and responds to interaction events. To create an interactive Chat app using an HTTP service, complete this quickstart.

Python

A Google Chat app that receives and responds to interaction events. To create an interactive Chat app using an HTTP service, complete this quickstart.

Java

A Google Chat app that receives and responds to interaction events. To create an interactive Chat app using an HTTP service, complete this quickstart.

Apps Script

A Google Chat app that receives and responds to interaction events. To create an interactive Chat app in Apps Script, complete this 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 apps can build the cards using the following widgets:

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

For an example of a Chat app that uses this contact form, see the following code:

Node.js

Python

Java

Apps Script

For more examples of interactive widgets that you can use to collect information, seeDesign an interactive card or dialog.

Receive data from interactive widgets

Whenever users click a button, Chat apps receive an interaction event dependent on the location of the button:

HTTP

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

Apps Script

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

HTTP

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

Apps Script

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

To receive the data, your Chat app handles the interaction event 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 interaction event, and an example value.

Form input widget Type of input data Input value from the interaction event Example value
textInput stringInputs event.common.formInputs.contactName.stringInputs.value[0] Kai O
selectionInput stringInputs To get the first or only value, event.common.formInputs.contactType.stringInputs.value[0] Personal
dateTimePicker that only accepts dates. dateInput event.common.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:

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

Node.js

Python

Java

Apps Script

When a user clicks the button, your Chat app receives aCARD_CLICKED interaction event from which you canreceive 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 dialog or card message.

Node.js

Python

Java

Apps Script

To process and close a dialog, you return anActionResponseobject that specifies whether you want to send a confirmation message, update the original message or card, or just close the dialog. For steps, seeClose a dialog.

Troubleshoot

When a Google Chat app orcard 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, seeTroubleshoot and fix Google Chat errors.