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.
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. Create the Trigger via the Script Editor in the Google Sheet rather than in the Google Form itself.
Download
Submit a Google Form to a Slack channel via a Webhook folder (please use 'File' > 'Make a copy' for your own version).
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.
ReplyDeletevar payload = {
innovation: innovation,
collaboration: collaboration,
communication: communication,
advancement: advancement,
wellbeing: wellbeing
}
Hi
DeleteThank 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
Hello. Thank you for great article. Can you advise please why i'm getting error: TypeError: Cannot read property 'namedValues' of undefined
ReplyDeleteCode:
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());
}
Hi Ivan
DeleteHave 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
Sure. Correct trigger was created, but error still present
DeleteHi Ivan
DeleteUnfortunately 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
im getting the same error as Ivan.
ReplyDeleteTypeError: 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 ******************
Hi
DeleteAssuming 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
Note: There are two versions of form-submit triggers, one for Sheets (where form responses are collected) and one for Google Forms. The event object passed to a Sheets form-submit trigger function is simpler and returns the response values in simple arrays. The event object passed to a Forms form-submit trigger function provides more information, contained in a FormResponse object.
ReplyDeletetl;dr to use the above script, trigger if from the linked spreadsheet, not the form.
Thank you - I have added a little bit more detail to the post to help clarify this more for people.
DeleteKind regards
Phil
Hi,
ReplyDeleteThanks for this excellent code. I have a question, how to add a divider in this following code. I will really appreciate if you can help me. Thanks once again
Thank you Ashir.
DeleteUnfortunately I have not dabbled any further with posting into Slack via Apps Script since this post, so I will leave you message here in case others can contribute to your request.
Ta