DEV Community

Kazuki Yonemoto
Kazuki Yonemoto

Posted on

Automating Slack Notifications for Google Forms Responses: A Simple Guide Using Google Apps Script (GAS) and Slack App

You may want to notify your internal Slack channels when responses are received from Google Forms. While there are several approaches to implementation, including using integration tools, this article introduces a method using Google Apps Script (GAS) and Slack App.

I personally recommend this method for its simplicity and flexibility.

This article involves some programming, but it is presented in a way that even non-engineers can easily implement by copy-pasting.

The necessary steps for implementation are as follows:

  1. Creating a Google Form
  2. Creating a Slack App
  3. Creating a Google Apps Script
  4. Setting up Google Apps Script Triggers
  5. Testing

Creating a Google Form

First, create a form. You are free to set up the form items as you like. Here, I created a form with the following items:

  • Name of person in Charge: Short Text
  • Email Address: Short Text
  • Content of Inquiry: Long Text

Image description

https://www.google.com/intl/en_us/forms/about/

Creating a Slack App

Click the link below and select Create New App.
https://api.slack.com/apps

Choose From scratch, register the app name and workspace.

Image description

Set up the app display settings. You can choose any content you like.

Image description

From the admin panel's left sidebar, select Incoming Webhooks, turn it ON to activate. Click Add New Webhook to Workspace, and select the channel to receive notifications.

Image description

Copy and save the Webhook URL of the activated channel.

Creating a Google Apps Script

Create a script to forward responses from Google Forms to Slack. Open the script editor from the form you created earlier.

Image description

Copy and paste the following code into the editor, replacing SLACK_WEBHOOK_URL with the Webhook URL you copied earlier. Save the file as sendSlack.gs.

Be careful not to disclose the Webhook URL to unauthorized persons.

function sendSlackMessage(body) {
  const SLACK_WEBHOOK_URL = "replace this";
  if (body.length === 0) {
    return;
  }

  const options = {
    headers: {
      "Content-Type": "application/json",
    },
    method: "post",
    payload: JSON.stringify({
      text: body,
    }),
  };

  try {
    UrlFetchApp.fetch(SLACK_WEBHOOK_URL, options);
  } catch (error) {
    Logger.log("Error sending Slack message: " + error.toString());
  }
};

function createMessageBody(itemResponses) {
  let body = "Please check the following inquiry.";

  itemResponses.forEach(response => {
    const question = response.getItem().getTitle();
    const answer = response.getResponse();
    body += "\n\n【" + question + "】\n\n" + answer;
  });

  return body;
}

function main(e) {
  const itemResponses = e.response.getItemResponses();
  const body = createMessageBody(itemResponses);
  sendSlackMessage(body);
}
Enter fullscreen mode Exit fullscreen mode

Image description

Setting up Google Apps Script Triggers

Set up a trigger so that the process you just created is executed when a response is received in the Google Form. Select Triggers from the left nav, then click Add Trigger. Set it up as follows and save.

Image description

  • Function to execute: main
  • Execute the deployment: Head
  • Event source: Form
  • Event type: On form submit

Testing

You are now ready. When you submit a response from the Google Form, a notification should arrive in the Slack channel.

Image description

Conclusion

Using Google Apps Script and Slack App allows direct integration without external tools. This reduces setup effort and makes customizing notifications and processing methods easier.

Using ChatGPT to customize the processing is also recommended. I hope this article contributes to your life hacks.

Top comments (0)