Motivation
A friend of mine invited me to work on a game with him and I, of course, accepted. Early on, we decided that we want beta testers, and a way to apply as a tester. Google Forms immediately came into mind, and I took up the task of writing some code to make it automatically post answers to the form in our Discord server.
Part 1: So... how do I even get started on this?
I knew I needed two things: A Discord webhook, and something to send the request to it with every form submission. I quickly found out about something called Apps Script, which let me interact with a bunch of Google apps (including Forms), and which I could utilise to bring this idea to life.
Part 2: Alright, let's jump into it
First, I wanted to figure out if I could even send an HTTP request from my Forms Add-on and found UrlFetchApp.fetch
function onSubmit(e) {
UrlFetchApp('somecoolurlidk')
}
Part 3: But can I use the Discord Webhook?
Yes. The Discord Webhook documentation is awesome, and I was up and running within minutes.
function onSubmit(e) {
var discordPayload = {
content: 'Form submitted'
}
UrlFetchApp('discordwebhookurl', {
method: 'post',
payload: JSON.stringify(discordPayload),
contentType: 'application/json'
})
}
Part 4: Welp, time to actually hook it up to the form
By going to Edit > Current project's triggers
, I could set up my form to trigger the onSubmit
function when the form is submitted.
Part 5: Putting it all together
I don't have much to say here. Took about an hour of mindlessly browsing the Apps Script documentation, but somehow managed to write this final code:
function onSubmit(e) {
var discordPayload = {
content: 'Form submitted',
embeds: [{
type: 'rich',
title: 'Form submission',
color: 7506394,
fields: []
}]
}
e.response.getItemResponses().forEach(function(i) {
var v = i.getResponse() || 'None'
discordPayload.embeds[0].fields.push({ name: i.getItem().getTitle(), value: v })
})
UrlFetchApp.fetch('youknowwhatgoeshereanywaysandimnotinthemoodtoleakstuff', {
method: 'post',
payload: JSON.stringify(discordPayload),
contentType: 'application/json'
})
}
Top comments (6)
Скажите как сделать чтоб не показывало не заполненые формы а только те которые заполняли
Do you have a method to split the items list so that larger surveys can be posted to Discord?
Not right now, but it wouldn't be too hard to do.
How can i set response for question name as webhook username?
You could modify
discordPayload
, refer to discord's documentation for more infoDid you figure out a way to split the items yet?