简体   繁体   English

通过java中的单个类文件发送Discord webhook

[英]Sending a Discord webhook through a single class file in java

I was wondering if it would be possible to send a discord webhook using java with the least api's and stuff possible.我想知道是否有可能使用最少的 api 和可能的东西使用 java 发送一个不和谐的 webhook。 Just basic java.只是基本的java。 (Also I only need text so it can be as simple as possible) (另外我只需要文字,所以它可以尽可能简单)

The reason I'm asking this is because I want it to be compiled into 1 class file, so that I can run said class file and have it working on its own outside of my IDE.我问这个的原因是因为我希望它被编译成 1 个类文件,这样我就可以运行所说的类文件并让它在我的 IDE 之外独立工作。 This was possible in unity using networking / web requests, so I assumed it would be something similar for java, however its more popular to import something which appears to be using maven or gradle (I'm really not familiar with either so I don't know.) I wrote a simple script for unity in order to send the most basic text webhook, and thats it.这可以统一使用网络/网络请求,所以我认为它与 java 类似,但是导入似乎使用 maven 或 gradle 的东西更受欢迎(我真的不熟悉,所以我不熟悉)我不知道。)为了发送最基本的文本 webhook,我写了一个简单的统一脚本,就是这样。

So if anyone knows how to use webhooks in one class please let me know!因此,如果有人知道如何在一节课中使用 webhooks,请告诉我!

Edit: I had found this code online somewhere but I don't think you can use it without external libraries because I was unable to import multiple things.编辑:我在网上某处找到了这段代码,但我认为没有外部库就不能使用它,因为我无法导入多个内容。

HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response;
HttpPost request = new HttpPost(webhook_url);
request.addHeader("Content-Type", "application/json");
String jsonMessage = "{\"content\": \"" + content + "\"}";
try {
    StringEntity params = new StringEntity(jsonMessage);
    request.setEntity(params);
    response = httpClient.execute(request);
} catch (IOException ex) {
    ex.printStackTrace();
    return;
}

I was looking for the same solution to send notifications from a gradle task.我正在寻找相同的解决方案来从 gradle 任务发送通知。 Here is what I found during my search这是我在搜索过程中发现的

see json structure of the post message and this snippet查看帖子消息的 json 结构和此代码段

So you have to build a valid JSON message and send it as a post message here is my minimal code to make the webhook work因此,您必须构建一个有效的 JSON 消息并将其作为发布消息发送,这是我使 webhook 工作的最小代码

import java.io.OutputStream; 
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class Test {

    public static void main(String[] args) {
        ///////////////////////////////////////////////
        // CONFIG
        String tokenWebhook = "YOUR TOKEN DISCORD WEBHOOK";
        String title = "Your Title";
        String message = "Your Message";
        ///////////////////////////////////////////////
        String jsonBrut = "";
        jsonBrut += "{\"embeds\": [{"
                + "\"title\": \""+ title +"\","
                + "\"description\": \""+ message +"\","
                + "\"color\": 15258703"
                + "}]}";
        try {
            URL url = new URL(tokenWebhook);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.addRequestProperty("Content-Type", "application/json");
            con.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            OutputStream stream = con.getOutputStream();
            stream.write(jsonBrut.getBytes());
            stream.flush();
            stream.close();
            con.getInputStream().close();
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Edit the tokenWebhook String and fill it with your token, and run the test编辑tokenWebhook字符串并用您的令牌填充它,然后运行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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