Skip to content

Instantly share code, notes, and snippets.

@dfop02
Created October 7, 2024 13:17
Show Gist options
  • Select an option

  • Save dfop02/abda439f51bdcd2b8758bb5014b83d33 to your computer and use it in GitHub Desktop.

Select an option

Save dfop02/abda439f51bdcd2b8758bb5014b83d33 to your computer and use it in GitHub Desktop.
App Script - Form trigger to notify Google Chat
// 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