Search code examples
javascriptgoogle-apps-scriptgoogle-chat

Google ChatBot - TextInput fields required


I'm developing a chatbot using Google App Scripts and I was wondering how to get a textInputs field required/ mandatory. For example, I have a dialog Box with an input text (as reported below) and i would like to get this field mandatory to fill in for the user. How can I do it?

Thanks a lot.

{....
 "textInput": {
                      "label": "Name",
                      "type": "SINGLE_LINE",
                      "name": "contactName"
                    }
....}

This is the entire Dialog Box with different text input fields and I would like get them required to fill in for the user.

return {
    "action_response": {
      "type": "DIALOG",
      "dialog_action": {
        "dialog": {
          "body": {
            "sections": [
              {
                "header": "Add new contact",
                "widgets": [
                  {
                    "textInput": {
                      "label": "Name",
                      "type": "SINGLE_LINE",
                      "name": "contactName"
                    }
                  },
                  {
                    "textInput": {
                      "label": "Address",
                      "type": "MULTIPLE_LINE",
                      "name": "address"
                    }
                  },
                  {
                    "decoratedText": {
                      "text": "Add to favorites",
                      "switchControl": {
                        "controlType": "SWITCH",
                        "name": "saveFavorite"
                      }
                    }
                  },
                  {
                    "decoratedText": {
                      "text": "Merge with existing contacts",
                      "switchControl": {
                        "controlType": "SWITCH",
                        "name": "mergeContact",
                        "selected": true
                      }
                    }
                  },
                  {
                    "buttonList": {
                      "buttons": [
                        {
                          "text": "Next",
                          "onClick": {
                            "action": {
                              "function": "openSequentialDialog"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }
      }
    }
  };
}

Solution

  • Google chat doesn't have that feature, because there is no submit event like HTML submit form does. Only onClick event. So you should add your own validation on the button onClick event script.

    Based on your code maybe you can do with like below:

    const formValues = event.common?.formInputs;
    const contactName = formValues?.['contactName']?.stringInputs.value[0]?.trim();
    if(!contactName) {
    return buildContactForm(event); // this is your own contact form builder
    }
    

    Currently, I also creating google chat apps that have validation, you can check my example validation here. So I returned the form again if the the required field doesn't inputted.