How to post to Slack from Google Forms

How to post to Slack from Google Forms

Tom Kavanagh's photo
Tom Kavanagh
·Mar 19, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Connecting a Google Form to Slack can be a great way of collecting required information and notifying your team.

In this post we'll create a new Slack App and connect it to a Google Form, to allow us to automatically post new form submissions to a Slack channel.

Create a Slack App

First we need to create a Slack App. To create a Slack app, go to api.slack.com/apps.

Screen Shot 2022-02-18 at 4.55.00 pm.png

Click Create New App and select From scratch.

Screen Shot 2022-02-18 at 4.43.31 pm.png

Name your app and select the Workspace to develop your app in. Click Create App.

Screen Shot 2022-02-18 at 5.03.42 pm.png

On the Basic Information tab, under Display Information, you can customise how your app appears when posting in Slack.

Screen Shot 2022-02-18 at 5.15.59 pm.png

Click Save Changes at the bottom of the page when you're finished customising.

Open Incoming Webhooks under Add features and functionality or find it in the sidebar.

Screen Shot 2022-02-18 at 5.19.17 pm.png

Turn on Activate Incoming Webhooks. Then click Add New Webhook to Workspace. This will allow your Slack app to receive data from your Google Form.

Screen Shot 2022-02-18 at 5.19.50 pm.png

Select the channel your Slack app should post to and click Allow.

Screen Shot 2022-02-18 at 5.23.45 pm.png

You now have a Webhook URL ready to be used in the next steps!

Screen Shot 2022-02-18 at 5.26.22 pm.png

Configuring your Google Form

Now we have our Slack app ready, Create your Google Form.

Screen Shot 2022-02-25 at 4.46.53 pm.png

Click the Responses tab and create a spreadsheet for your responses by clicking the Google Sheets logo and Create a new spreadsheet.

Screen Shot 2022-02-25 at 4.48.13 pm.png

In your new spreadsheet, click Extensions and open Apps Script. Name your project at the top of the page.

Screen Shot 2022-02-25 at 4.53.02 pm.png

Replace the file contents with the following:


function getLastDataRow(sheet) {
  var sheet = SpreadsheetApp.getActiveSheet()
  Logger.log(sheet.getLastRow() + " Is the last Row.");

  var range = sheet.getDataRange();
  Logger.log(range.getLastRow() + " Is the last Row.");

  //Range Values
  var data = range.getValues();
  Logger.log(data[range.getLastRow()-1]);

  return data[range.getLastRow()-1]
}

function sendSlackMessage() {
  var lastFormEntry = getLastDataRow()
  Logger.log("last row data:+"+lastFormEntry)
  const url = "WEBHOOK_URL"
  const params = {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify({
      "text": "New Leads",
      "blocks": [
        {
          "type": "divider"
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": lastFormEntry[1]
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": lastFormEntry[2]
          }
        },
        {
          "type": "divider"
        },
      ]
    })

  }

  const sendMsg = UrlFetchApp.fetch(url, params)
  var respCode = sendMsg.getResponseCode()
  Logger.log(sendMsg)
  Logger.log(respCode)
}

The first function, getLastRow(sheet) will retrieve the data from the most recent Form submission which is the last row in the responses spreadsheet.

The second function, sendSlackMessage() sends the spreadsheet data to our Slack app webhook.

We need to Copy the Webhook URL on the Slack app we created and replace WEBHOOK_URL in the function.

Screen Shot 2022-02-25 at 5.00.27 pm.png

After you have replaced the WEBHOOK_URL, click the save icon. We now need to make this script run the Google Form is submitted.

In the sidebar, open Triggers.

Screen Shot 2022-02-25 at 5.19.09 pm.png

Click Add Trigger. For Choose which function to run, select sendSlackMessage(). Change the Select event type to On form submit and click Save.

Screen Shot 2022-02-25 at 5.44.35 pm.png

You may receive a Sign-in pop up from Google confirming you authorise this function access the responses Google sheet. Click Allow.

Open the Google form and Submit a new entry.

Screen Shot 2022-02-25 at 5.37.36 pm.png

The new entry should be sent to Slack!

Screen Shot 2022-02-25 at 5.43.28 pm.png

new_leads.gif

Now you have finished connecting your Google Form to Slack! You can customise the formatting of the Slack message by editing the blocks section of the sendSlackMessage() function, adding extra columns and following the documentation on the Slack Block Kit Builder.

This setup could be used for external customers also, allowing them to submit feedback or specialised orders to a Google Form and having it send to the relevant Slack Channels.

 
Share this