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.
Click Create New App
and select From scratch
.
Name your app and select the Workspace to develop your app in. Click Create App
.
On the Basic Information
tab, under Display Information
, you can customise how your app appears when posting in Slack.
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.
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.
Select the channel your Slack app should post to and click Allow
.
You now have a Webhook URL ready to be used in the next steps!
Configuring your Google Form
Now we have our Slack app ready, Create your Google Form.
Click the Responses
tab and create a spreadsheet for your responses by clicking the Google Sheets logo and Create a new spreadsheet
.
In your new spreadsheet, click Extensions
and open Apps Script
. Name your project at the top of the page.
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.
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
.
Click Add Trigger
. For Choose which function to run
, select sendSlackMessage()
.
Change the Select event type
to On form submit
and click Save
.
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.
The new entry should be sent to Slack!
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.