|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START subsequent_steps] |
| 18 | +/** |
| 19 | + * Responds to a message in Google Chat. |
| 20 | + * |
| 21 | + * @param {Object} event The event object from the Google Workspace add-on. |
| 22 | + * @return {Object} response that handles dialogs. |
| 23 | + */ |
| 24 | +function onMessage(event) { |
| 25 | + // Reply with a message that contains a button to open the initial dialog |
| 26 | + return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: { |
| 27 | + text: "To add a contact, use the `ADD CONTACT` button below.", |
| 28 | + accessoryWidgets: [ |
| 29 | + // [START open_dialog_from_button] |
| 30 | + { buttonList: { buttons: [{ |
| 31 | + text: "ADD CONTACT", |
| 32 | + onClick: { action: { |
| 33 | + function: "openInitialDialog", |
| 34 | + interaction: "OPEN_DIALOG" |
| 35 | + }} |
| 36 | + }]}} |
| 37 | + // [END open_dialog_from_button] |
| 38 | + ] |
| 39 | + }}}}}; |
| 40 | +} |
| 41 | + |
| 42 | +// [START open_initial_dialog] |
| 43 | +/** |
| 44 | + * Opens the initial step of the dialog that lets users add contact details. |
| 45 | + * |
| 46 | + * @param {Object} event The event object from the Google Workspace add-on. |
| 47 | + * @return {Object} open the dialog. |
| 48 | + */ |
| 49 | +function openInitialDialog(event) { |
| 50 | + return { action: { navigations: [{ pushCard: { sections: [{ widgets: [ |
| 51 | + { textInput: { |
| 52 | + name: "contactName", |
| 53 | + label: "First and last name", |
| 54 | + type: "SINGLE_LINE" |
| 55 | + }}, |
| 56 | + { dateTimePicker: { |
| 57 | + name: "contactBirthdate", |
| 58 | + label: "Birthdate", |
| 59 | + type: "DATE_ONLY" |
| 60 | + }}, |
| 61 | + { selectionInput: { |
| 62 | + name: "contactType", |
| 63 | + label: "Contact type", |
| 64 | + type: "RADIO_BUTTON", |
| 65 | + items: [ |
| 66 | + { text: "Work", value: "Work", selected: false }, |
| 67 | + { text: "Personal", value: "Personal", selected: false } |
| 68 | + ] |
| 69 | + }}, |
| 70 | + { buttonList: { buttons: [{ |
| 71 | + text: "NEXT", |
| 72 | + onClick: { action: { function : "openConfirmationDialog" }} |
| 73 | + }]}} |
| 74 | + ]}]}}]}}; |
| 75 | +} |
| 76 | +// [END open_initial_dialog] |
| 77 | + |
| 78 | +/** |
| 79 | + * Opens the second step of the dialog that lets users confirm details. |
| 80 | + * |
| 81 | + * @param {Object} event The event object from the Google Workspace add-on. |
| 82 | + * @return {Object} update the dialog. |
| 83 | + */ |
| 84 | +function openConfirmationDialog(event) { |
| 85 | + // Retrieve the form input values |
| 86 | + const name = event.commonEventObject.formInputs["contactName"].stringInputs.value[0]; |
| 87 | + const birthdate = event.commonEventObject.formInputs["contactBirthdate"].dateInput.msSinceEpoch; |
| 88 | + const type = event.commonEventObject.formInputs["contactType"].stringInputs.value[0]; |
| 89 | + // Display the input values for confirmation |
| 90 | + return { action: { navigations: [{ pushCard: { sections: [{ widgets: [ |
| 91 | + { textParagraph: { text: "Confirm contact information and submit:" }}, |
| 92 | + { textParagraph: { text: "<b>Name:</b> " + name }}, |
| 93 | + { textParagraph: { text: "<b>Birthday:</b> " + new Date(birthdate) }}, |
| 94 | + { textParagraph: { text: "<b>Type:</b> " + type }}, |
| 95 | + // [START set_parameters] |
| 96 | + { buttonList: { buttons: [{ |
| 97 | + text: "SUBMIT", |
| 98 | + onClick: { action: { |
| 99 | + function: "submitDialog", |
| 100 | + // Pass input values as parameters for last dialog step (submission) |
| 101 | + parameters: [ |
| 102 | + { key: "contactName", value: name }, |
| 103 | + { key: "contactBirthdate", value: birthdate }, |
| 104 | + { key: "contactType", value: type } |
| 105 | + ] |
| 106 | + }} |
| 107 | + }]}} |
| 108 | + // [END set_parameters] |
| 109 | + ]}]}}]}}; |
| 110 | +} |
| 111 | +// [END subsequent_steps] |
| 112 | + |
| 113 | +/** |
| 114 | + * Handles submission and closes the dialog. |
| 115 | + * |
| 116 | + * @param {Object} event The event object from the Google Workspace add-on. |
| 117 | + * @return {Object} close the dialog with a status in text notification or message. |
| 118 | + */ |
| 119 | +function submitDialog(event) { |
| 120 | + // [START status_notification] |
| 121 | + // Validate the parameters. |
| 122 | + if (!event.commonEventObject.parameters["contactName"]) { |
| 123 | + return { action: { |
| 124 | + navigations: [{ endNavigation: { action: "CLOSE_DIALOG"}}], |
| 125 | + notification: { text: "Failure, the contact name was missing!" } |
| 126 | + }}; |
| 127 | + } |
| 128 | + // [END status_notification] |
| 129 | + |
| 130 | + // [START status_message] |
| 131 | + return { hostAppDataAction: { chatDataAction: { createMessageAction: { message: { |
| 132 | + text: "✅ " + event.commonEventObject.parameters["contactName"] + " has been added to your contacts." |
| 133 | + }}}}}; |
| 134 | + // [END status_message] |
| 135 | +} |
0 commit comments