Created
October 7, 2024 13:17
-
-
Save dfop02/abda439f51bdcd2b8758bb5014b83d33 to your computer and use it in GitHub Desktop.
App Script - Form trigger to notify Google Chat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Reference: https://developers.google.com/workspace/chat/quickstart/webhooks#apps-script | |
| // Webhook URL from Google Chat | |
| const WEBHOOK_URL = 'https://chat.googleapis.com/v1/spaces/SPACE_ID/messages'; | |
| function sendChatNotification(e) { | |
| // Get the data from the form submission (Google Sheet row) | |
| var sheet = e.source.getActiveSheet(); | |
| var row = e.range.getRow(); | |
| // Assuming form data starts at row 2, adjust as necessary | |
| var data = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0]; | |
| // Example: Assuming columns are: Name, Email, Message | |
| var name = data[0]; | |
| var email = data[1]; | |
| var message = data[2]; | |
| // Construct the message to send to Google Chat | |
| var chatMessage = { | |
| "text": `New Form Submission:\n\nName: ${name}\nEmail: ${email}\nMessage: ${message}` | |
| }; | |
| // Send the message to the Google Chat webhook | |
| notifyGoogleChat(chatMessage); | |
| } | |
| function notifyGoogleChat(data) { | |
| const options = { | |
| "method": "post", | |
| "headers": {"Content-Type": "application/json; charset=UTF-8"}, | |
| "payload": JSON.stringify(data) | |
| }; | |
| const response = UrlFetchApp.fetch(WEBHOOK_URL, options); | |
| console.log(response); | |
| } | |
| // Set up trigger to run when form submits data | |
| function createTrigger() { | |
| ScriptApp.newTrigger('sendChatNotification') | |
| .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()) | |
| .onFormSubmit() | |
| .create(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment