DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cyber28
Cyber28

Posted on • Updated on

How I made Google Forms automatically post submissions in our Discord server

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')
}
Enter fullscreen mode Exit fullscreen mode

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'
  })
}
Enter fullscreen mode Exit fullscreen mode

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.
Setting up the trigger
Webhook in action

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'
  })
}
Enter fullscreen mode Exit fullscreen mode

The final webhook in action

Top comments (7)

Collapse
dolfies profile image
dolfies

Is it possible to prevent it from posting empty fields, instead of it just saying none?

Collapse
maksq7 profile image
Maksq7

Π‘ΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΊΠ°ΠΊ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ Ρ‡Ρ‚ΠΎΠ± Π½Π΅ ΠΏΠΎΠΊΠ°Π·Ρ‹Π²Π°Π»ΠΎ Π½Π΅ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½Ρ‹Π΅ Ρ„ΠΎΡ€ΠΌΡ‹ Π° Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Ρ‚Π΅ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ заполняли

Collapse
bdeavilla profile image
bdeavilla

Do you have a method to split the items list so that larger surveys can be posted to Discord?

Collapse
cyber28 profile image
Cyber28 Author

Not right now, but it wouldn't be too hard to do.

Collapse
anonymyous678 profile image
Anonymyous678

How can i set response for question name as webhook username?

Collapse
cyber28 profile image
Cyber28 Author

You could modify discordPayload, refer to discord's documentation for more info

Collapse
jag11222 profile image
Jag11222

Did you figure out a way to split the items yet?

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.