My goal is to post Google Forms responses to a Discord channel via a webhook. I'm using Google Apps Script in order to do this.
I'm expecting a regular case of the webhook being posted whenever I submit the form, though Discord always throws back {"message": "Cannot send an empty message", "code": 50006}.
Is there something wrong with the code I have written that would poison the JSON beyond use?
Here is the code that the onFormSubmit trigger would be using.
Assume that webHookURL is correct.
messageTemplate is the exact JSON that has been sent by the trigger after formatting has been performed in onFormSubmission and I have tested messageTemplate in a separate function that just sends it after performing JSON.stringify on it.
let messageTemplate = {
"content": "Test",
"embeds": [{
"color": 10751,
"fields": [
{
"name": "Question 1",
"value": "Option 1"
},
{
"name": "Question 2",
"value": "Test"
}
]
}]
}
function onFormSubmission(e) {
let FormResponse = e.response;
let QuestionResponses = FormResponse.getItemResponses();
let newMessage = messageTemplate
for (var i = 0; i < QuestionResponses.length; i++) {
let QuestionResponse = QuestionResponses[i];
let fieldName = QuestionResponse.getItem().getTitle();
let fieldValue = QuestionResponse.getResponse();
newMessage.embeds[0].fields[i] = { "name": fieldName, "value": fieldValue };
}
let requestPayload = JSON.stringify(newMessage)
let requestOptions = {
header: {
'Content-Type': 'application/json',
},
method: 'POST',
payload: requestPayload,
muteHttpExceptions: true,
}
let response = UrlFetchApp.fetch(webHookURL, requestOptions)
Logger.log("POSTed")
Logger.log(response.getContentText())
Logger.log(requestOptions.payload);
}
P.S: I have scoured this site already for all related questions and attempted to implement fixes, but nothing has helped my case.