Tuesday, 30 March 2021

Submit a Google Form to a Slack channel via a Webhook

The following Google Apps Script is designed to take a Google Form submission and send the data to a specific Slack channel via a Webhook.

Messages can be posted in to a Slack channel via the Google Form
Screenshot of message when posted in Slack channel

Create a Slack App

Assuming you already have Slack set up and established, you will need to create an App here and enable 'Incoming Webhooks' which will generate you a long URL to connect directly with your Workspace and channel.


Google Apps Script code

The code itself is quite straightforward and you may wish to refer to this blog post which goes into a little bit more detail about getting data from a Google Form submission.

Once we have the data we just need to construct/format our Slack message so it looks like the image above. So here we piece together static text with data captured from the Google Form:

var text = "Training with: " + mainContact + "\n";
text += "In department: " + department + "\n";

Next we combine the data and some other features into a payload that can be sent via the Slack Webhook. Here we are bringing in the message text from above, defining the name that should appear from the App ('Teaching-Bot' in this example) and then the emoji icon to use:

var payload = {
    text: text,
    username: "Teaching-Bot",
    icon_emoji: ":teacher:"
};

Finally we set the options to send the data (post) and call the UrlFetchApp. Remember you will also need to create a project trigger to run this Function, of the event type - On form submit.

 

Download

Submit a Google Form to a Slack channel via a Webhook folder (please use 'File' > 'Make a copy' for your own version).

No comments:

Post a comment