Automating Slack Notifications for Google Forms Responses: A Simple Guide Using Google Apps Script (GAS) and Slack App
Tags: tutorial, gas, productivity
Published: 2023-12-18 02:39:09


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://dev-to-uploads.s3.amazonaws.com/uploads/articles/opbn46rkex6membjlhfn.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdfj6etg413cs41uu5ox.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54o76oijjbzp5yv03ozj.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n80hj9f7z8qfkt6yxc1h.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4hs2zf8lc83t5jzgax5.png)

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.**

```js:sendSlack.gs
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);
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfr9kgw9n3w8db3wro5m.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kh8lnenpu0q0sj4fy85.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ychmqjbztzvc6uuumjr.png)

## 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.