简体   繁体   中英

Send message to discord via google apps script

I'd like to send a bot message in a discord channel via google apps script when a certain event is triggered, but I don't know where to start. Is this even possible? If not, is there a way to do it via github?

EDIT: I have figured out how to get the OAuth tokens, now how do I make the bot send a message?

I know that there's pretty much no chance that the OP still needs this answer, but I'm putting it up here so that others who google this question will be able to find the answer

 var webhooks = { test: "Obtain a webhook for the channel you'd like and put it here." }; function sendMessage(message, channel) { if(webhooks.hasOwnProperty(channel)) var url = webhooks[channel]; else { Logger.log("Error Sending Message to Channel " + channel); return "NoStoredWebhookException"; } var payload = JSON.stringify({content: message}); var params = { headers: {"Content-Type": "application/x-www-form-urlencoded"}, method: "POST", payload: payload, muteHttpExceptions: true }; var res = UrlFetchApp.fetch(url, params); Logger.log(res.getContentText()); } // to call and send a message sendMessage("Hi!", "test");

This is what I typically use for sending messages. Unfortunately, there's no way to receive triggers from the webhooks to my knowledge.

Note: The above answer references discord.js, which is not compatible with Google Apps Script

To start with, here is a documentation from discord.js .

To let your apps script communicate with discord, you can check the External APIs

Google Apps Script can interact with APIs from all over the web. This guide shows how to work with different types of APIs in your scripts.

Examples are available in the provided documentations.

See these useful links for further details.

Super easy. Just go to your Discord channel, choose "Edit Channel" > "Webhooks". You name the bot and set its profile picture. It will give you a webhook URL that already contains the authorization token. Then you just POST to that public URL. TADA, a message will appear in the given channel, sent by the bot.

function postMessageToDiscord(message) {

  message = message || "Hello World!";

  var discordUrl = 'https://discordapp.com/api/webhooks/labnol/123';
  var payload = JSON.stringify({content: message});

  var params = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    payload: payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(discordUrl, params);

  Logger.log(response.getContentText());

}

Source: https://ctrlq.org/code/20563-post-message-to-discord-webhooks

For anyone still looking and not having luck with previous answers, like me:

After making my message_string, this snippet successfully sent to my desired channel's webhook:

    // Send the message to the Discord channel webhook.
  let options = {
          "method": "post",
          "headers": {
              "Content-Type": "application/json",
          },
          "payload": JSON.stringify({
              "content": message_string
          })
      };
      Logger.log(options, null, 2);
      UrlFetchApp.fetch("your_webhook_url_here", options);

I can't make a comment yet, but if you are having issues with one of the above answers - try changing the content type from: 'Content-Type':"application/x-www-form-urlencoded" to 'Content-Type':"application/json" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM