有没有办法使用JavaScript发布webhook?

时间:2018-05-05 08:09:49

标签: javascript webhooks

我想使用Webhook在我的Discord服务器中发送消息。有没有办法使用JavaScript?

我尝试了这段代码,但它不起作用:

var url = // Webhook URL
var text = '{"content":"Hi"}' // Text to post
$.ajax({
   data: 'payload=' + JSON.stringify({
    "text": text
   }),
   dataType: 'json',
   processData: false,
   type: 'POST',
   url: url
});

Souce

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Discord Webhook Tutorial</title>
  </head>
  <body>
    <button onclick="sendMessage()">Send</button>
  </body>

  <script>
    function sendMessage() {
      var request = new XMLHttpRequest();
      request.open("POST", "https://discordapp.com/api/webhooks/676118118082281513/ZS5YcWhurzokBrKX9NgexqtxrJA5Pu2Bo4i7_JsIxC-JIbPBVhSZkcVVukGOro52rnQA");

      request.setRequestHeader('Content-type', 'application/json');

      var params = {
        username: "My Webhook Name",
        avatar_url: "",
        content: "The message to send"
      }

      request.send(JSON.stringify(params));
    }
  </script>
</html>