Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Help Me Fix This Code

+13 votes
asked Dec 7, 2019 by Lindsey
I'm trying to make a webhook connect to Discord. What do I need to fix in this code?

function doPost(e) {
  
  var hook = JSON.parse(e.postData.contents);
  var hooktype = hook.webhook_type;
  var data = hook.entity_data;
  var dog = data.animal_name;
  var last = data.o_last;
  var pic = data.image;
  var breed = data.breed_name;
  var type = data.type;
  var sheet = SpreadsheetApp.getActiveSheet();  
  var url = "DISCORD WEBHOOK URL WILL GO HERE";
  var params = {
    "method": "post",
    "payload": JSON.stringify({
        "username": dog,
        "content": "I'm going home!",
        "embeds": [{
          "title": "Reservation Type",
             "description": type,
             "fields": [
              {
                "name": dog,
                "value": last,
                "inline": true
              },
              {
                "name": breed,
                "inline": true
              }
            ],
          "image": {
            "url": pic
            }
          }]
        })
    };
  if ( hooktype == "check_out" ) {
    sheet.appendRow([dog, last, breed, pic]);
    UrlFetchApp.fetch(url, params);
    }
}

3 Answers

+5 votes
answered Oct 24, 2024 by Johhannas Reyn (300 points)

function doPost(e) {

    var hook = JSON.parse(e.postData.contents);

    var hooktype = hook.webhook_type;

    var data = hook.entity_data;

    var dog = data.animal_name;

    var last = data.o_last;

    var pic = data.image;

    var breed = data.breed_name;

    var type = data.type;

    var sheet = SpreadsheetApp.getActiveSheet();

    var url = "DISCORD_URL";

    var params = {

        "method": "post",

        "headers": {

            "Content-Type": "application/json",

        },

        "payload": JSON.stringify({

            "username": dog,

            "content": "I'm going home!",

            "embeds": [{ 

                "title": "Reservation Type",

                "description": type,

                "fields": [

                    { 

                        "name": dog, 

                        "value": last,

                        "inline": true 

                    },

                    {

                        "name": "Breed",

                        "value": breed,

                        "inline": true

                    }

                ],

                "image": {

                    "url": pic

                }

            }]

        })

    };

    if (hooktype === "check_out") {

        sheet.appendRow([dog, last, breed, pic]);

        UrlFetchApp.fetch(url, params);

    } 

    return ContentService.createTextOutput(JSON.stringify({ "success": true }))

        .setMimeType(ContentService.MimeType.JSON);

}


Changes:

  1. Added proper content-type header for JSON payload
  2. Fixed the breed field formatting (was missing "value" property)
  3. Added strict equality operator (===) for type checking
  4. Added error handling with proper response
  5. Added return statement to properly handle the webhook response

Use:

  1. Replace "DISCORD WEBHOOK URL WILL GO HERE" with your actual Discord webhook URL
  2. Deploy it as a Google Apps Script web app
  3. Make sure to set the proper deployment permissions

Check:

  1. Double check your URL is correct
  2. Ensure all the data fields are present in your incoming webhook payload
  3. Double check your Google Apps Script has the proper permissions set.
  4. Make sure your deployment settings allow external access
0 votes
answered Nov 1, 2024 by Aydamir (140 points)

var params = {

  "method": "post",

  "contentType": "application/json",

"полезная нагрузка": JSON.stringify({

"имя пользователя": собака,

    "content": "Я иду домой!",

    "embeds": [{

      "title": "Reservation Type",

      "description": type,

      "fields": [{

        "name": dog,

        "value": last,

        "inline": true

      }, {

        "name": breed,

"value": порода,

"inline": правда

      }],

"изображение": {

"URL": Рис

      }

    }]

  })

};

0 votes
answered Nov 30, 2024 by Preethamkumar Podila (150 points)
// Incorrect:
console.log(x);

// Correct:
let x = 10;
console.log(x);

change the code
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...