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

8 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
  2. Hello. Thank you for great article. Can you advise please why i'm getting error: TypeError: Cannot read property 'namedValues' of undefined

    Code:

    function onFormSubmit(e) {
    var formValues = e.namedValues;
    var Address = formValues['Address'][0];
    //
    var webhookUrl = 'https://hooks.slack.com/services/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';
    //
    var text = "Training with:"+ Address + "\n";
    //
    var payload = {
    text: text,
    username: "Teaching-Bot",
    icon_emoji: ":teacher:"
    };

    var options = {
    method: "post",
    contentType: "application/json",
    //Convert the JavaScript object to a JSON string
    payload: JSON.stringify(payload)
    };
    //
    var result = UrlFetchApp.fetch(webhookUrl, options);
    Logger.log(result.getContentText());
    }

    ReplyDelete
    Replies
    1. Hi Ivan

      Have you followed the steps about remembering to create a project trigger so that the Apps Script code knows to run when the Google Form has been submitted?

      Kind regards
      Phil

      Delete
    2. Sure. Correct trigger was created, but error still present

      Delete
    3. Hi Ivan

      Unfortunately in that instance I am unsure then as that error is usually to do with the Trigger and the Event data that comes from submitting the Google Form (as it's stopping at the very first line).

      Maybe log 'e' with something like "Logger.log(e)" and see what that returns?

      Kind regards
      Phil

      Delete
  3. im getting the same error as Ivan.

    TypeError: Cannot read property 'namedValues' of undefined (line 14, file "Code")

    line 12 // ****************** get data from Google Form ******************
    line 13 // get all Form data
    line 14 var formValues = e.namedValues;

    var emailAddress = formValues['Email address'][0];
    Logger.log('emailAddress is: ' + emailAddress);
    var mainContact = formValues['Main Contact'][0];
    Logger.log('mainContact is: ' + mainContact);
    var sessionDetails = formValues['Session Details'][0];
    Logger.log('sessionDetails is: ' + sessionDetails);
    var date = formValues['Date'][0];
    Logger.log('date is: ' + date);
    var department = formValues['Department'][0];
    Logger.log('department is: ' + department);
    // ****************** get data from Google Form ******************

    ReplyDelete
    Replies
    1. Hi

      Assuming you've followed the same troubleshooting as I mentioned to Ivan, do you want to temporarily give me 'edit' access to your file to see if I can spot anything else? If you want to use the 'Contact Form' on the blog to share it so the link is not public.

      Kind regards
      Phil

      Delete