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

2 comments:

  1. This article was immensely helpful in getting Google Form responses to post to a channel in SLACK so thank you. Did you know that Slack has added WebHooks to their Workflow Manager? Now you can create a new workflow with a WebHook trigger thus bypassing the need to create a custom Slack app. The only thing I did differently from your code was I dropped the TEXT variable and made the payload a set of Key and Value pairs like this that match the variables I setup in the Workflow WebHook in SLACK.

    var payload = {
    innovation: innovation,
    collaboration: collaboration,
    communication: communication,
    advancement: advancement,
    wellbeing: wellbeing
    }

    ReplyDelete
    Replies
    1. Hi

      Thank you for your feedback and thank you for sharing about Webhooks in Workflow Manager - I'll definitely be taking a look at this.

      Kind regards
      Phil

      Delete